-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new rectors for php8.3's deprecation of get_class()/get_parent_cl…
…ass() without arguments
- Loading branch information
Showing
14 changed files
with
358 additions
and
0 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
27 changes: 27 additions & 0 deletions
27
rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/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\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function test() | ||
{ | ||
echo get_class(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function test() | ||
{ | ||
echo __CLASS__; | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_different_func_call.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\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture; | ||
|
||
class SkipDifferentFuncCall | ||
{ | ||
public function run($a, $b) | ||
{ | ||
return str_contains($a, $b); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_with_args.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture; | ||
|
||
use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector; | ||
|
||
class SkipWithArgs | ||
{ | ||
public function test() | ||
{ | ||
$obj = new RemoveGetClassNoArgsRector(); | ||
echo get_class($obj); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/RemoveGetClassNoArgsRectorTest.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\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveGetClassNoArgsRectorTest 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'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector; | ||
use Rector\ValueObject\PhpVersion; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveGetClassNoArgsRector::class); | ||
|
||
$rectorConfig->phpVersion(PhpVersion::PHP_83); | ||
}; |
27 changes: 27 additions & 0 deletions
27
rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/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\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function test() | ||
{ | ||
echo get_parent_class(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function test() | ||
{ | ||
echo parent::class; | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_different_func_call.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\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture; | ||
|
||
class SkipDifferentFuncCall | ||
{ | ||
public function run($a, $b) | ||
{ | ||
return str_contains($a, $b); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...sts/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_with_args.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture; | ||
|
||
use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector; | ||
|
||
class SkipWithArgs | ||
{ | ||
public function test() | ||
{ | ||
$obj = new RemoveGetClassNoArgsRector(); | ||
echo get_parent_class($obj); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...Rector/FuncCall/RemoveGetParentClassNoArgsRector/RemoveGetParentClassNoArgsRectorTest.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\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveGetParentClassNoArgsRectorTest 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'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...s-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector; | ||
use Rector\ValueObject\PhpVersion; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveGetParentClassNoArgsRector::class); | ||
|
||
$rectorConfig->phpVersion(PhpVersion::PHP_83); | ||
}; |
81 changes: 81 additions & 0 deletions
81
rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php83\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see https://php.watch/versions/8.3/get_class-get_parent_class-parameterless-deprecated | ||
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.core.get-class | ||
* @see \Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\RemoveGetClassNoArgsRectorTest | ||
*/ | ||
final class RemoveGetClassNoArgsRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
$r = new RuleDefinition( | ||
'Replace calls to get_class() without arguments with __CLASS__ magic constant', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class Example { | ||
public function doWork() { | ||
return get_class(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class Example { | ||
public function doWork() { | ||
return __CLASS__; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
|
||
return $r; | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Node\Expr\FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param Node\Expr\FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if (! ($this->isName($node, 'get_class'))) { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) !== 0) { | ||
return null; | ||
} | ||
|
||
return new Node\Scalar\MagicConst\Class_(); | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATE_GET_CLASS_WITHOUT_ARGS; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php83\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see https://php.watch/versions/8.3/get_class-get_parent_class-parameterless-deprecated | ||
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.core.get-class | ||
* @see \Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\RemoveGetParentClassNoArgsRectorTest | ||
*/ | ||
final class RemoveGetParentClassNoArgsRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
$r = new RuleDefinition( | ||
'Replace calls to get_parent_class() without arguments with parent::class constant', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class Example extends StdClass { | ||
public function doWork() { | ||
return get_parent_class(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class Example extends StdClass { | ||
public function doWork() { | ||
return parent::class; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
|
||
return $r; | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Node\Expr\FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param Node\Expr\FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if (! ($this->isName($node, 'get_parent_class'))) { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) !== 0) { | ||
return null; | ||
} | ||
|
||
return new Node\Expr\ClassConstFetch(new Node\Name(['parent']), new Node\VarLikeIdentifier('class')); | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATE_GET_CLASS_WITHOUT_ARGS; | ||
} | ||
} |
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