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
Expand Up @@ -4,13 +4,13 @@ declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Fixture;

use Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithConstants;
use Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithFinalConstant;

final class SomeClass
final class AllowFinalClassConstant
{
public function run(ClassWithConstants $classWithConstants)
public function run(ClassWithFinalConstant $classWithFinalConstant)
{
return $classWithConstants::NAME;
return $classWithFinalConstant::NAME;
}
}

Expand All @@ -22,13 +22,13 @@ declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Fixture;

use Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithConstants;
use Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithFinalConstant;

final class SomeClass
final class AllowFinalClassConstant
{
public function run(ClassWithConstants $classWithConstants)
public function run(ClassWithFinalConstant $classWithFinalConstant)
{
return \Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithConstants::NAME;
return \Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithFinalConstant::NAME;
}
}

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

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Fixture;

use Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source\ClassWithConstants;

final class SkipClassConstantFromNonFinalClass
{
public function run(ClassWithConstants $classWithConstants)
{
return $classWithConstants::NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\VariableConstFetchToClassConstFetchRector\Source;

class ClassWithFinalConstant
{
public final const NAME = 'SomeName';
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -19,13 +20,22 @@
*/
final class VariableConstFetchToClassConstFetchRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change variable class constant fetch to direct class constant fetch',
'Change variable class constant fetch to direct class constant fetch when class or constant target is final',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class AnotherClass
{
}

class SomeClass
{
public function run(AnotherClass $anotherClass)
Expand All @@ -36,6 +46,10 @@ public function run(AnotherClass $anotherClass)
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class AnotherClass
{
}

class SomeClass
{
public function run(AnotherClass $anotherClass)
Expand Down Expand Up @@ -84,6 +98,22 @@ public function refactor(Node $node): ?ClassConstFetch
return null;
}

if (! $this->reflectionProvider->hasClass($classObjectType->getClassName())) {
return null;
}

$classReflection = $this->reflectionProvider->getClass($classObjectType->getClassName());
if (! $classReflection->isFinalByKeyword()) {
if (! $classReflection->hasConstant($constantName)) {
return null;
}

$constant = $classReflection->getConstant($constantName);
if (! $constant->isFinal()) {
return null;
}
}

$node->class = new FullyQualified($classObjectType->getClassName());

return $node;
Expand Down