-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php55] Add StaticToSelfOnFinalClassRector (#3629)
* [Php55] Add StaticToSelfRector * skip dynamic * skip on trait * skip in abstract class * [ci-review] Rector Rectify * skip on constant call * generate docs * register to config set php 5.5 * skip already self * rename to StaticToSelfOnFinalClassRector * [ci-review] Rector Rectify * fix namespace --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
14cc7d4
commit 581fba0
Showing
12 changed files
with
271 additions
and
3 deletions.
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
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
27 changes: 27 additions & 0 deletions
27
...tests/Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/fixture.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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
final class Fixture | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(static::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
final class Fixture | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(self::class); | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...5/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/skip_already_self.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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
final class SkipAlreadySelf | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(self::class); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/skip_call_constant.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
final class SkipCallConstant | ||
{ | ||
private const BAR = 'test'; | ||
|
||
public function callOnMe() | ||
{ | ||
var_dump(static::BAR); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/skip_dynamic.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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
final class SkipDynamic | ||
{ | ||
public function callOnMe($variable) | ||
{ | ||
var_dump($variable::class); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/skip_dynamic2.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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
final class SkipDynamic2 | ||
{ | ||
public function callOnMe($variable) | ||
{ | ||
var_dump(static::$variable); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...tor/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/skip_in_abstract_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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
abstract class SkipInAbstractClass | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(static::class); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/Fixture/skip_in_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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\Fixture; | ||
|
||
trait SkipInTrait | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(static::class); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...tor/ClassConstFetch/StaticToSelfOnFinalClassRector/StaticToSelfOnFinalClassRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class StaticToSelfOnFinalClassRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ts/Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector/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\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(StaticToSelfOnFinalClassRector::class); | ||
}; |
98 changes: 98 additions & 0 deletions
98
rules/Php55/Rector/ClassConstFetch/StaticToSelfOnFinalClassRector.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,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php55\Rector\ClassConstFetch; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
use Rector\Core\Enum\ObjectReference; | ||
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; | ||
|
||
/** | ||
* @changelog https://wiki.php.net/rfc/class_name_scalars | ||
* @changelog https://3v4l.org/AHr9C#v5.5.0 | ||
* @see \Rector\Tests\Php55\Rector\ClassConstFetch\StaticToSelfOnFinalClassRector\StaticToSelfOnFinalClassRectorTest | ||
*/ | ||
final class StaticToSelfOnFinalClassRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change `static::class` to `self::class` on final class', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(static::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function callOnMe() | ||
{ | ||
var_dump(self::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassConstFetch::class]; | ||
} | ||
|
||
/** | ||
* @param ClassConstFetch $node | ||
*/ | ||
public function refactor(Node $node): ?ClassConstFetch | ||
{ | ||
if (! $node->class instanceof Name) { | ||
return null; | ||
} | ||
|
||
if (! $node->name instanceof Identifier) { | ||
return null; | ||
} | ||
|
||
if ($node->class->toString() !== ObjectReference::STATIC) { | ||
return null; | ||
} | ||
|
||
if ($node->name->toString() !== 'class') { | ||
return null; | ||
} | ||
|
||
$class = $this->betterNodeFinder->findParentType($node, Class_::class); | ||
if (! $class instanceof Class_) { | ||
return null; | ||
} | ||
|
||
if (! $class->isFinal()) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createSelfFetchConstant('class'); | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::CLASSNAME_CONSTANT; | ||
} | ||
} |