Skip to content
Closed
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

use Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Source\StatusEnum;
use Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Source\Autoload\StatusEnum;

class SkipEnumUsageInOtherPackage {
public function isValid(StatusEnum $status): bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Source;
namespace Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\Source\Autoload;

enum StatusEnum
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([EnumCaseToPascalCaseRector::class]);
->withRules([EnumCaseToPascalCaseRector::class])
Copy link
Member

Choose a reason for hiding this comment

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

Like other your PR, Please don't change existing test config, use different class & config on purpose, with use of dynamic provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case there's really no need for a separate config and test case just to test one fixture

Copy link
Member

Choose a reason for hiding this comment

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

No, the test include existing Fixture in autoloaded, see my next comment on removed test class

#7272 (review)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you really insist then i can keep both, both the autoload fixture will have to be moved to a separate directory because the test cases were conflicting

Copy link
Member

Choose a reason for hiding this comment

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

->withAutoloadPaths([__DIR__ . '/../Source/Autoload']);

This file was deleted.

69 changes: 30 additions & 39 deletions rules/CodingStyle/Rector/Enum_/EnumCaseToPascalCaseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

namespace Rector\CodingStyle\Rector\Enum_;

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;
use Rector\Rector\AbstractRector;
use Rector\Skipper\FileSystem\PathNormalizer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -27,12 +23,6 @@
*/
final class EnumCaseToPascalCaseRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -122,67 +112,68 @@ private function refactorClassConstFetch(ClassConstFetch $classConstFetch): ?Nod
return null;
}

if ($this->nodeTypeResolver->getType($classConstFetch->class)->isEnum()->no()) {
return null;
}

$constName = $classConstFetch->name->toString();
$pascalCaseName = $this->convertToPascalCase($constName);

// Skip "class" constant
if ($constName === 'class') {
// short circuit if already in pascal case
if ($constName === $pascalCaseName) {
return null;
}

$enumClassName = $classConstFetch->class->toString();
if (! $this->reflectionProvider->hasClass($enumClassName)) {
$classReflection = $this->nodeTypeResolver->getType($classConstFetch->class)
->getObjectClassReflections()[0] ?? null;

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

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

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

// ensure exactly ReflectionEnum
if (! $classIdentifier instanceof ReflectionEnum) {
if ($this->isUsedOutsideOfProject($classReflection)) {
return null;
}

// ensure not part of definition in ->withAutoloadPaths()
$fileTarget = $classIdentifier->getFileName();
$classConstFetch->name = new Identifier($pascalCaseName);
return $classConstFetch;
}

private function isUsedOutsideOfProject(ClassReflection $classReflection): bool
{
$fileTarget = $classReflection->getFileName();

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

$autoloadPaths = SimpleParameterProvider::provideArrayParameter(Option::AUTOLOAD_PATHS);
$normalizedFileTarget = PathNormalizer::normalize((string) realpath($fileTarget));

if (str_contains($normalizedFileTarget, '/vendor/')) {
return true;
}

foreach ($autoloadPaths as $autoloadPath) {
$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