-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php80] Add ClassOnThisVariableObjectRector (#3093)
* [Php80] Add ClassOnThisVariableObjectRector * skip on trait test * [ci-review] Rector Rectify * clean up Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
8ef8087
commit 1e8ffcc
Showing
12 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...r/ClassConstFetch/ClassOnThisVariableObjectRector/ClassOnThisVariableObjectRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/final_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
...80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/non_final_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/skip_call_static_property.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/skip_class_constant_name.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Rector/ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/skip_dynamic_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...hp80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/skip_on_trait.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...r/ClassConstFetch/ClassOnThisVariableObjectRector/Fixture/skip_self_class_already.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
rules/Php80/Rector/ClassConstFetch/ClassOnThisVariableObjectRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters