Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Fixture;

enum CaseOnSelf: string
{
case PENDING = 'pending';

function isPending(): bool
{
return $this === self::PENDING;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Fixture;

enum CaseOnSelf: string
{
case Pending = 'pending';

function isPending(): bool
{
return $this === self::Pending;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Fixture;

enum SkipEnumConst
{
const FOO = 'foo';

case Pending;

function test(): void
{
echo self::FOO;
}
}
66 changes: 38 additions & 28 deletions rules/CodingStyle/Rector/Enum_/EnumCaseToPascalCaseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace Rector\CodingStyle\Rector\Enum_;

use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
use PHPStan\Reflection\ClassReflection;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\EnumCase;
use PHPStan\BetterReflection\Reflection\ReflectionEnum;
use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
Expand All @@ -24,11 +23,11 @@

/**
* @see \Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\EnumCaseToPascalCaseRectorTest
* @see \Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\WithAutoloadPathsTest
*/
final class EnumCaseToPascalCaseRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider,
) {
}
Expand Down Expand Up @@ -122,43 +121,54 @@ private function refactorClassConstFetch(ClassConstFetch $classConstFetch): ?Nod
return null;
}

if ($this->nodeTypeResolver->getType($classConstFetch->class)->isEnum()->no()) {
$constName = $classConstFetch->name->toString();
$pascalCaseName = $this->convertToPascalCase($constName);

// short circuit if already in pascal case
if ($constName === $pascalCaseName) {
return null;
}

$constName = $classConstFetch->name->toString();
$classReflection = $this->nodeTypeResolver->getType($classConstFetch->class)
->getObjectClassReflections()[0] ?? null;

if ($classReflection === null || ! $classReflection->isEnum()) {
return null;
}

// Skip "class" constant
if ($constName === 'class') {
if (! $this->isEnumCase($classReflection, $constName, $pascalCaseName)) {
return null;
}

$enumClassName = $classConstFetch->class->toString();
if (! $this->reflectionProvider->hasClass($enumClassName)) {
if ($this->isUsedOutsideOfProject($classConstFetch->class)) {
Copy link
Member

@samsonasik samsonasik Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self should can be recefenced to current classreflection name, if it is ThisType, should be utilze $type->getStaticObjectType() so can look up correct target name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self should can be recefenced to current classreflection name

If the rector is processing self then it should be guaranteed to be a path used in withPaths

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let see it in the wild :),

/cc @marcelthole @boesing in case you have a chance to test latest "rector/rector": "dev-main" for EnumCaseToPascalCaseRector rule :)

return null;
}

$classConstFetch->name = new Identifier($pascalCaseName);
return $classConstFetch;
}

private function isUsedOutsideOfProject(Name $name): bool
{
if (in_array($name->toString(), ['self', 'static'], true)) {
return false;
}

$sourceLocator = $this->dynamicSourceLocatorProvider->provide();
$defaultReflector = new DefaultReflector($sourceLocator);

try {
$classIdentifier = $defaultReflector->reflectClass($classConstFetch->class->toString());
$classIdentifier = $defaultReflector->reflectClass($name->toString());
} catch (IdentifierNotFound) {
// source is outside the paths defined in withPaths(), eg: vendor
return null;
}

// ensure exactly ReflectionEnum
if (! $classIdentifier instanceof ReflectionEnum) {
return null;
return true;
}

// ensure not part of definition in ->withAutoloadPaths()
$fileTarget = $classIdentifier->getFileName();

// possibly native
if ($fileTarget === null) {
return null;
return true;
}

$autoloadPaths = SimpleParameterProvider::provideArrayParameter(Option::AUTOLOAD_PATHS);
Expand All @@ -168,21 +178,21 @@ private function refactorClassConstFetch(ClassConstFetch $classConstFetch): ?Nod
$normalizedAutoloadPath = PathNormalizer::normalize($autoloadPath);

if ($autoloadPath === $fileTarget) {
return null;
return true;
}

if (str_starts_with($normalizedFileTarget, $normalizedAutoloadPath . '/')) {
return null;
return true;
}
}

$pascalCaseName = $this->convertToPascalCase($constName);
if ($constName !== $pascalCaseName) {
$classConstFetch->name = new Identifier($pascalCaseName);
return $classConstFetch;
}
return false;
}

return null;
private function isEnumCase(ClassReflection $classReflection, string $name, string $pascalName): bool
{
// the enum case might have already been renamed, need to check both
return $classReflection->hasEnumCase($name) || $classReflection->hasEnumCase($pascalName);
}

private function convertToPascalCase(string $name): string
Expand Down
Loading