Skip to content

Commit

Permalink
[Php80] Add ClassOnThisVariableObjectRector (#3093)
Browse files Browse the repository at this point in the history
* [Php80] Add ClassOnThisVariableObjectRector

* skip on trait test

* [ci-review] Rector Rectify

* clean up

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 25, 2022
1 parent 8ef8087 commit 1e8ffcc
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Php80\Rector\Class_\StringableForToStringRector;
use Rector\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector;
use Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;
use Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector;
use Rector\Php80\Rector\ClassMethod\SetStateToStaticRector;
Expand Down Expand Up @@ -107,4 +108,5 @@
$rectorConfig->rule(Php8ResourceReturnToObjectRector::class);
$rectorConfig->rule(AddParamBasedOnParentClassMethodRector::class);
$rectorConfig->rule(MixedTypeRector::class);
$rectorConfig->rule(ClassOnThisVariableObjectRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ClassOnThisVariableObjectRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

final class FinalClass
{
public function run()
{
return $this::class;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

final class FinalClass
{
public function run()
{
return self::class;
}
}

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

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

class NonFinalClass
{
public function run()
{
return $this::class;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

class NonFinalClass
{
public function run()
{
return static::class;
}
}

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

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

final class SkipCallStaticProperty
{
public static $bar = 'test';
public function run()
{
return $this::$bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

final class SkipClassConstantName
{
private const SOME_CONSTANT = 'test';
public function run()
{
return $this::SOME_CONSTANT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

final class SkipDynamicClass
{
public function run(object $object)
{
return $object::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

trait SkipOnTrait
{
public function run()
{
return $this::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\Fixture;

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ClassOnThisVariableObjectRector::class);
};
113 changes: 113 additions & 0 deletions rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Rector\Php80\Rector\ClassConstFetch;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* ::class introduced in php 5.5
* while $this::class introduced in php 8.0
*
* @changelog https://wiki.php.net/rfc/class_name_scalars
* @changelog https://wiki.php.net/rfc/class_name_literal_on_object
*
* @see \Rector\Tests\Php80\Rector\ClassConstFetch\ClassOnThisVariableObjectRector\ClassOnThisVariableObjectRectorTest
*/
final class ClassOnThisVariableObjectRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change $this::class to static::class or self::class depends on class modifier',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return $this::class;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($object)
{
return static::class;
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassConstFetch::class];
}

/**
* @param ClassConstFetch $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
return null;
}

$className = $class->isFinal() ? 'self' : 'static';
$node->class = new Name($className);

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::CLASS_ON_OBJECT;
}

private function shouldSkip(ClassConstFetch $classConstFetch): bool
{
if (! $classConstFetch->class instanceof Variable) {
return true;
}

if (! is_string($classConstFetch->class->name)) {
return true;
}

if (! $this->isName($classConstFetch->class, 'this')) {
return true;
}

if (! $classConstFetch->name instanceof Identifier) {
return true;
}

return ! $this->isName($classConstFetch->name, 'class');
}
}
2 changes: 1 addition & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ final public function enterNode(Node $node)
/** @var non-empty-array<Node>|Node $refactoredNode */
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);

$rectorWithLineChange = new RectorWithLineChange($this::class, $originalNode->getLine());
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
Expand Down

0 comments on commit 1e8ffcc

Please sign in to comment.