Skip to content

Commit

Permalink
fix casing of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 13, 2022
1 parent 6f164da commit 332aad3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8724,9 +8724,9 @@ Refactor Spatie enum class to native Enum
-class StatusEnum extends Enum
+enum StatusEnum : string
{
+ case draft = 'draft';
+ case published = 'published';
+ case archived = 'archived';
+ case DRAFT = 'draft';
+ case PUBLISHED = 'published';
+ case ARCHIVED = 'archived';
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use Spatie\Enum\Enum;

enum StatusEnum : string
{
case draft = 'draft';
case published = 'published';
case archived = 'archived';
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}

?>
12 changes: 8 additions & 4 deletions rules/Php81/NodeFactory/EnumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public function createFromSpatieClass(Class_ $class): Enum_
$enum = new Enum_($shortClassName);

// constant to cases
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);

$docBlockMethods = $phpDocInfo?->getTagsByName('@method');
if ($docBlockMethods !== null) {
$docBlockMethods = $phpDocInfo->getTagsByName('@method');
if ($docBlockMethods !== []) {
$enum->scalarType = new Identifier('string');

foreach ($docBlockMethods as $docBlockMethod) {
Expand All @@ -85,6 +85,10 @@ private function createEnumCaseFromDocComment(PhpDocTagNode $phpDocTagNode): Enu
{
/** @var MethodTagValueNode $nodeValue */
$nodeValue = $phpDocTagNode->value;
return new EnumCase($nodeValue->methodName, $this->builderFactory->val($nodeValue->methodName));

$enumName = strtoupper($nodeValue->methodName);
$enumExpr = $this->builderFactory->val($nodeValue->methodName);

return new EnumCase($enumName, $enumExpr);
}
}
6 changes: 3 additions & 3 deletions rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class StatusEnum extends Enum
<<<'CODE_SAMPLE'
enum StatusEnum : string
{
case draft = 'draft';
case published = 'published';
case archived = 'archived';
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
CODE_SAMPLE
),
Expand Down

0 comments on commit 332aad3

Please sign in to comment.