Skip to content

Commit e5460ce

Browse files
committed
fix annotation command
1 parent b1ee31e commit e5460ce

File tree

5 files changed

+88
-19
lines changed

5 files changed

+88
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![Enum Helper-Dark](branding/laravel-dark.png#gh-dark-mode-only)![Enum Helper-Light](branding/laravel-light.png#gh-light-mode-only)
22
# Laravel Enum Helper
33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/datomatic/laravel-enum-helper.svg?style=for-the-badge)](https://packagist.org/packages/datomatic/laravel-enum-helper)
4-
[![Pest Tests number](https://img.shields.io/static/v1?label=%23tests&message=97&color=FF88FA&style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMVEUAAAD/iPv9yP3Xm+j/mP//wfVj67Je6bP/h/pVx6p6175d57WQycf+iPn/iPrsnezArd3+t/qpvNJd6LP/jPpu6rv/lPr/kPpc57T/rvtc57Np6rj3oPl37cL/tfn/wv9d6brX//L/g/rYn+n/gvrWm+di6LX+jPrskfGWzMpt6bln4bdd57Jk6LWSycj+vPquwNVo6rde6bP7nvvYnup91b/+vfv/lvtc57OqvNTFs9//t/td57L9t/r/iPpd6LPapej/ovp26bxy67v9lfld6LJr4Ljwsvb/xv3/jv39zv1t6buG5cTDreH5ivlc5rJy676V4cxb57D/y/h50MOy4OCUxcVa77X/iPpe6LP/jP+pu9L8t///tvuQycfArNxp6LzArd151r7/i/9n4bb/j/9e6rT/ifr7ifrskvLYnuhi87tg8blg7bf/vv//lP+wxNtj9b3/qv//oP/+ivz/l/r8ifryn/fvlPTfpPDeofDKtujHtOWX1NF/4seC3cR82sFu7cBo5LiMwPMrAAAAWHRSTlMA/Wv8FAIC/dME/Wj+3tEG/Pv798G1oHRjS0k1LBsWDgsJ/v36+fTy8ezn4+Lh29XNzMzLysLAwLSwr66opJqakY+Ni4J7end0bGlpY11XU048KicmIR8fizl+vwAAAVdJREFUKM9tz2VXAlEQgOFBBURpkE67u7u7E1YFYQl1SbvrlztDiLvss+fc/fCemXMvAEhhKqU759P1rLoxUDUyEh9fPH0z7ALiVrEY+SSNtxNS2upouYv7hOL191aKVsZHUTgbnQPQgDkq4ctHdoQmTWmW4WFzlVUDVpNKXf2fWpWbZIwUq/hcmjWGYnSa1pZZjEoomrEdVAisD7CX6GEb40rqTODxCj21OjDOvjRV8l2jhudBDchg/FUbDIZCITzwQyH6a9+2AMDbm9GfltFnxgAdtQWUgQJl4VQq37uPcSnsfYZzav6Ew18fQ4fUYPM7Qn4uSiIdyx5saJ6T+/3+5KSltshicwI2UpfAKE/aoARTvnn7KMYMdlAUyWRSyHN2JeU42HlCi4TszTHcmuj3iMVdP5JzoyAWNzi6T3ZGrMFCliK3BAqRSC/B2+6IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=)](https://github.com/datomatic/laravel-enum-helper/tree/main/tests)
4+
[![Pest Tests number](https://img.shields.io/static/v1?label=%23tests&message=99&color=FF88FA&style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMVEUAAAD/iPv9yP3Xm+j/mP//wfVj67Je6bP/h/pVx6p6175d57WQycf+iPn/iPrsnezArd3+t/qpvNJd6LP/jPpu6rv/lPr/kPpc57T/rvtc57Np6rj3oPl37cL/tfn/wv9d6brX//L/g/rYn+n/gvrWm+di6LX+jPrskfGWzMpt6bln4bdd57Jk6LWSycj+vPquwNVo6rde6bP7nvvYnup91b/+vfv/lvtc57OqvNTFs9//t/td57L9t/r/iPpd6LPapej/ovp26bxy67v9lfld6LJr4Ljwsvb/xv3/jv39zv1t6buG5cTDreH5ivlc5rJy676V4cxb57D/y/h50MOy4OCUxcVa77X/iPpe6LP/jP+pu9L8t///tvuQycfArNxp6LzArd151r7/i/9n4bb/j/9e6rT/ifr7ifrskvLYnuhi87tg8blg7bf/vv//lP+wxNtj9b3/qv//oP/+ivz/l/r8ifryn/fvlPTfpPDeofDKtujHtOWX1NF/4seC3cR82sFu7cBo5LiMwPMrAAAAWHRSTlMA/Wv8FAIC/dME/Wj+3tEG/Pv798G1oHRjS0k1LBsWDgsJ/v36+fTy8ezn4+Lh29XNzMzLysLAwLSwr66opJqakY+Ni4J7end0bGlpY11XU048KicmIR8fizl+vwAAAVdJREFUKM9tz2VXAlEQgOFBBURpkE67u7u7E1YFYQl1SbvrlztDiLvss+fc/fCemXMvAEhhKqU759P1rLoxUDUyEh9fPH0z7ALiVrEY+SSNtxNS2upouYv7hOL191aKVsZHUTgbnQPQgDkq4ctHdoQmTWmW4WFzlVUDVpNKXf2fWpWbZIwUq/hcmjWGYnSa1pZZjEoomrEdVAisD7CX6GEb40rqTODxCj21OjDOvjRV8l2jhudBDchg/FUbDIZCITzwQyH6a9+2AMDbm9GfltFnxgAdtQWUgQJl4VQq37uPcSnsfYZzav6Ew18fQ4fUYPM7Qn4uSiIdyx5saJ6T+/3+5KSltshicwI2UpfAKE/aoARTvnn7KMYMdlAUyWRSyHN2JeU42HlCi4TszTHcmuj3iMVdP5JzoyAWNzi6T3ZGrMFCliK3BAqRSC/B2+6IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=)](https://github.com/datomatic/laravel-enum-helper/tree/main/tests)
55
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/datomatic/laravel-enum-helper/run-tests.yml?branch=main&label=tests&color=5FE8B3&style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMVEUAAAD/iPv9yP3Xm+j/mP//wfVj67Je6bP/h/pVx6p6175d57WQycf+iPn/iPrsnezArd3+t/qpvNJd6LP/jPpu6rv/lPr/kPpc57T/rvtc57Np6rj3oPl37cL/tfn/wv9d6brX//L/g/rYn+n/gvrWm+di6LX+jPrskfGWzMpt6bln4bdd57Jk6LWSycj+vPquwNVo6rde6bP7nvvYnup91b/+vfv/lvtc57OqvNTFs9//t/td57L9t/r/iPpd6LPapej/ovp26bxy67v9lfld6LJr4Ljwsvb/xv3/jv39zv1t6buG5cTDreH5ivlc5rJy676V4cxb57D/y/h50MOy4OCUxcVa77X/iPpe6LP/jP+pu9L8t///tvuQycfArNxp6LzArd151r7/i/9n4bb/j/9e6rT/ifr7ifrskvLYnuhi87tg8blg7bf/vv//lP+wxNtj9b3/qv//oP/+ivz/l/r8ifryn/fvlPTfpPDeofDKtujHtOWX1NF/4seC3cR82sFu7cBo5LiMwPMrAAAAWHRSTlMA/Wv8FAIC/dME/Wj+3tEG/Pv798G1oHRjS0k1LBsWDgsJ/v36+fTy8ezn4+Lh29XNzMzLysLAwLSwr66opJqakY+Ni4J7end0bGlpY11XU048KicmIR8fizl+vwAAAVdJREFUKM9tz2VXAlEQgOFBBURpkE67u7u7E1YFYQl1SbvrlztDiLvss+fc/fCemXMvAEhhKqU759P1rLoxUDUyEh9fPH0z7ALiVrEY+SSNtxNS2upouYv7hOL191aKVsZHUTgbnQPQgDkq4ctHdoQmTWmW4WFzlVUDVpNKXf2fWpWbZIwUq/hcmjWGYnSa1pZZjEoomrEdVAisD7CX6GEb40rqTODxCj21OjDOvjRV8l2jhudBDchg/FUbDIZCITzwQyH6a9+2AMDbm9GfltFnxgAdtQWUgQJl4VQq37uPcSnsfYZzav6Ew18fQ4fUYPM7Qn4uSiIdyx5saJ6T+/3+5KSltshicwI2UpfAKE/aoARTvnn7KMYMdlAUyWRSyHN2JeU42HlCi4TszTHcmuj3iMVdP5JzoyAWNzi6T3ZGrMFCliK3BAqRSC/B2+6IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=)](https://github.com/datomatic/laravel-enum-helper/actions/workflows/run-tests.yml)
66
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/datomatic/laravel-enum-helper/pint.yml?label=code%20style&color=5FE8B3&style=for-the-badge)](https://github.com/datomatic/laravel-enum-helper/actions/workflows/php-cs-fixer.yml)
77
[![Total Downloads](https://img.shields.io/packagist/dt/datomatic/laravel-enum-helper.svg?style=for-the-badge)](https://packagist.org/packages/datomatic/laravel-enum-helper)

src/Commands/EnumAnnotateCommand.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Laminas\Code\Generator\DocBlock\Tag\TagInterface;
1414
use Laminas\Code\Generator\DocBlockGenerator;
1515
use Laminas\Code\Reflection\DocBlockReflection;
16-
use ReflectionClass;
16+
use ReflectionEnum;
1717
use ReflectionException;
1818
use UnitEnum;
1919

@@ -52,7 +52,7 @@ protected function annotateFolder(): int
5252

5353
if (count($searchDirectoryMap) > 0) {
5454
foreach ($searchDirectoryMap as $class => $_) {
55-
$reflection = new ReflectionClass($class);
55+
$reflection = new ReflectionEnum($class);
5656

5757
if ($reflection->isSubclassOf(UnitEnum::class)) {
5858
$this->annotate($reflection);
@@ -79,38 +79,38 @@ protected function annotateClass(string $className): int
7979
return self::FAILURE;
8080
}
8181

82-
$reflection = new ReflectionClass($className);
82+
$reflection = new ReflectionEnum($className);
8383
$this->annotate($reflection);
8484

8585
return self::SUCCESS;
8686
}
8787

8888
/**
89-
* @param ReflectionClass<UnitEnum> $reflectionClass
89+
* @param ReflectionEnum<UnitEnum> $reflectionEnum
9090
*
9191
* @throws FileNotFoundException
9292
*/
93-
protected function annotate(ReflectionClass $reflectionClass): void
93+
protected function annotate(ReflectionEnum $reflectionEnum): void
9494
{
9595
$docBlock = new DocBlockGenerator;
9696

97-
if ($reflectionClass->getDocComment()) {
97+
if ($reflectionEnum->getDocComment()) {
9898
$docBlock->setShortDescription(
99-
DocBlockGenerator::fromReflection(new DocBlockReflection($reflectionClass))
99+
DocBlockGenerator::fromReflection(new DocBlockReflection($reflectionEnum))
100100
->getShortDescription()
101101
);
102102
}
103103

104-
$this->updateClassDocblock($reflectionClass, $this->getDocBlock($reflectionClass));
104+
$this->updateClassDocblock($reflectionEnum, $this->getDocBlock($reflectionEnum));
105105
}
106106

107107
/**
108108
* @throws FileNotFoundException
109109
*/
110-
protected function updateClassDocblock(ReflectionClass $reflectionClass, DocBlockGenerator $docBlock): void
110+
protected function updateClassDocblock(ReflectionEnum $reflectionEnum, DocBlockGenerator $docBlock): void
111111
{
112-
$shortName = $reflectionClass->getShortName();
113-
$fileName = (string) $reflectionClass->getFileName();
112+
$shortName = $reflectionEnum->getShortName();
113+
$fileName = (string) $reflectionEnum->getFileName();
114114
$contents = $this->filesystem->get($fileName);
115115

116116
$enumDeclaration = "enum {$shortName}";
@@ -139,16 +139,16 @@ protected function updateClassDocblock(ReflectionClass $reflectionClass, DocBloc
139139
}
140140
}
141141

142-
protected function getDocBlock(ReflectionClass $reflectionClass): DocBlockGenerator
142+
protected function getDocBlock(ReflectionEnum $reflectionEnum): DocBlockGenerator
143143
{
144144
$docBlock = (new DocBlockGenerator)
145145
->setWordWrap(false);
146146

147147
$originalDocBlock = null;
148148

149-
if ($reflectionClass->getDocComment()) {
149+
if ($reflectionEnum->getDocComment()) {
150150
$originalDocBlock = DocBlockGenerator::fromReflection(
151-
new DocBlockReflection(ltrim($reflectionClass->getDocComment()))
151+
new DocBlockReflection(ltrim($reflectionEnum->getDocComment()))
152152
);
153153

154154
if ($originalDocBlock->getShortDescription()) {
@@ -162,7 +162,7 @@ protected function getDocBlock(ReflectionClass $reflectionClass): DocBlockGenera
162162

163163
$docBlock->setTags($this->getDocblockTags(
164164
$originalDocBlock,
165-
$reflectionClass
165+
$reflectionEnum
166166
));
167167

168168
return $docBlock;
@@ -173,15 +173,15 @@ protected function getDocBlock(ReflectionClass $reflectionClass): DocBlockGenera
173173
*/
174174
protected function getDocblockTags(
175175
?DocBlockGenerator $originalDocblock,
176-
ReflectionClass $reflectionClass
176+
ReflectionEnum $reflectionEnum
177177
): array {
178-
$constants = $reflectionClass->getConstants();
178+
$constants = $reflectionEnum->getConstants();
179179
$constantKeys = array_keys($constants);
180180

181181
$tags = array_map(
182182
static fn (mixed $value, string $constantName): MethodTag => new MethodTag(
183183
(new Convert($constantName))->toCamel(),
184-
['string'],
184+
$reflectionEnum->getBackingType()?->getName() === 'int' ? ['int'] : ['string'],
185185
null,
186186
true
187187
),

tests/Feature/EnumAnnotateCommandTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@
1313
__DIR__.'/../stubs/StatusWithoutDocBlock.stub',
1414
$this->withoutDocBlockEnumsFolder.'/StatusWithoutDocBlock.php'
1515
);
16+
copy(
17+
__DIR__.'/../stubs/StatusIntWithoutDocBlock.stub',
18+
$this->withoutDocBlockEnumsFolder.'/StatusIntWithoutDocBlock.php'
19+
);
20+
copy(
21+
__DIR__.'/../stubs/StatusStringWithoutDocBlock.stub',
22+
$this->withoutDocBlockEnumsFolder.'/StatusStringWithoutDocBlock.php'
23+
);
1624
});
1725

1826
afterEach(function () {
1927
if (file_exists($this->withoutDocBlockEnumsFolder.'/StatusWithoutDocBlock.php')) {
2028
unlink($this->withoutDocBlockEnumsFolder.'/StatusWithoutDocBlock.php');
29+
unlink($this->withoutDocBlockEnumsFolder.'/StatusIntWithoutDocBlock.php');
30+
unlink($this->withoutDocBlockEnumsFolder.'/StatusStringWithoutDocBlock.php');
2131
}
2232
rmdir($this->withoutDocBlockEnumsFolder);
2333
rmdir(app_path('Enums'));
@@ -33,6 +43,27 @@
3343
$this->assertEquals(1, substr_count($contents, '@method static string noResponse()'));
3444
});
3545

46+
47+
it('can be success single file int backed enum', function () {
48+
$this->artisan("enum:annotate --folder={$this->withoutDocBlockEnumsFolder} Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\WithoutDocBlockEnums\\\\StatusIntWithoutDocBlock")
49+
->assertSuccessful();
50+
$contents = file_get_contents($this->withoutDocBlockEnumsFolder.'/StatusIntWithoutDocBlock.php');
51+
$this->assertEquals(1, substr_count($contents, '@method static int pending()'));
52+
$this->assertEquals(1, substr_count($contents, '@method static int accepted()'));
53+
$this->assertEquals(1, substr_count($contents, '@method static int discarded()'));
54+
$this->assertEquals(1, substr_count($contents, '@method static int noResponse()'));
55+
});
56+
57+
it('can be success single file stribng backed enum', function () {
58+
$this->artisan("enum:annotate --folder={$this->withoutDocBlockEnumsFolder} Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\WithoutDocBlockEnums\\\\StatusStringWithoutDocBlock")
59+
->assertSuccessful();
60+
$contents = file_get_contents($this->withoutDocBlockEnumsFolder.'/StatusStringWithoutDocBlock.php');
61+
$this->assertEquals(1, substr_count($contents, '@method static string pending()'));
62+
$this->assertEquals(1, substr_count($contents, '@method static string accepted()'));
63+
$this->assertEquals(1, substr_count($contents, '@method static string discarded()'));
64+
$this->assertEquals(1, substr_count($contents, '@method static string noResponse()'));
65+
});
66+
3667
it('can be success single file with exists doc block', function () {
3768
$this->artisan("enum:annotate --folder={$this->enumsFolder} Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\Enums\\\\Status")
3869
->assertSuccessful();
@@ -77,6 +108,8 @@
77108

78109
it('can be failed with empty folder', function () {
79110
unlink($this->withoutDocBlockEnumsFolder.'/StatusWithoutDocBlock.php');
111+
unlink($this->withoutDocBlockEnumsFolder.'/StatusIntWithoutDocBlock.php');
112+
unlink($this->withoutDocBlockEnumsFolder.'/StatusStringWithoutDocBlock.php');
80113
$this->artisan("enum:annotate --folder={$this->withoutDocBlockEnumsFolder}")
81114
->assertFailed();
82115
});
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Datomatic\LaravelEnumHelper\Tests\Support\WithoutDocBlockEnums;
4+
5+
use Datomatic\LaravelEnumHelper\LaravelEnumHelper;
6+
7+
enum StatusIntWithoutDocBlock: int
8+
{
9+
use LaravelEnumHelper;
10+
11+
case PENDING = 1;
12+
13+
case ACCEPTED = 2;
14+
15+
case DISCARDED = 3;
16+
17+
case NO_RESPONSE = 4;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Datomatic\LaravelEnumHelper\Tests\Support\WithoutDocBlockEnums;
4+
5+
use Datomatic\LaravelEnumHelper\LaravelEnumHelper;
6+
7+
enum StatusStringWithoutDocBlock: string
8+
{
9+
use LaravelEnumHelper;
10+
11+
case PENDING = 'pending';
12+
13+
case ACCEPTED = 'accepted';
14+
15+
case DISCARDED = 'discarded';
16+
17+
case NO_RESPONSE = 'no_response';
18+
}

0 commit comments

Comments
 (0)