mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
[Php74] Add GetCalledClassToSelfClassRector (#1971)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
4562f4f988
commit
90bbd4e1a0
|
@ -1,4 +1,4 @@
|
|||
# 506 Rules Overview
|
||||
# 507 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -66,7 +66,7 @@
|
|||
|
||||
- [Php73](#php73) (9)
|
||||
|
||||
- [Php74](#php74) (15)
|
||||
- [Php74](#php74) (16)
|
||||
|
||||
- [Php80](#php80) (17)
|
||||
|
||||
|
@ -7837,9 +7837,28 @@ Change `filter_var()` with slash escaping to `addslashes()`
|
|||
|
||||
<br>
|
||||
|
||||
### GetCalledClassToSelfClassRector
|
||||
|
||||
Change `get_called_class()` to self::class on final class
|
||||
|
||||
- class: [`Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector`](../rules/Php74/Rector/FuncCall/GetCalledClassToSelfClassRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
- var_dump(get_called_class());
|
||||
+ var_dump(self::class);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### GetCalledClassToStaticClassRector
|
||||
|
||||
Change `get_called_class()` to static::class
|
||||
Change `get_called_class()` to static::class on non-final class
|
||||
|
||||
- class: [`Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector`](../rules/Php74/Rector/FuncCall/GetCalledClassToStaticClassRector.php)
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ use Rector\Php74\Rector\Double\RealToFloatTypeCastRector;
|
|||
use Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector;
|
||||
use Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
|
||||
use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
|
||||
use Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector;
|
||||
use Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector;
|
||||
use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector;
|
||||
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;
|
||||
|
@ -41,6 +42,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
$services->set(ExportToReflectionFunctionRector::class);
|
||||
|
||||
$services->set(GetCalledClassToSelfClassRector::class);
|
||||
|
||||
$services->set(GetCalledClassToStaticClassRector::class);
|
||||
|
||||
$services->set(MbStrrposEncodingArgumentPositionRector::class);
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\Fixture;
|
||||
|
||||
new class {
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class() );
|
||||
}
|
||||
};
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\Fixture;
|
||||
|
||||
new class {
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( self::class );
|
||||
}
|
||||
};
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\Fixture;
|
||||
|
||||
final class Fixture
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( self::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\Fixture;
|
||||
|
||||
class SkipOnNonFinalClass
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\Fixture;
|
||||
|
||||
trait SkipTrait
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class GetCalledClassToSelfClassRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(GetCalledClassToSelfClassRector::class);
|
||||
};
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector\Fixture;
|
||||
|
||||
new class {
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class() );
|
||||
}
|
||||
};
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector\Fixture;
|
||||
|
||||
final class SkipOnFinalClass
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector\Fixture;
|
||||
|
||||
trait SomeTrait
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( get_called_class());
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector\Fixture;
|
||||
|
||||
trait SomeTrait
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump( static::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php74\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\Enum\ObjectReference;
|
||||
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
||||
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/deprecations_php_7_4 (not confirmed yet)
|
||||
* @see https://3v4l.org/GU9dP
|
||||
* @see \Rector\Tests\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector\GetCalledClassToSelfClassRectorTest
|
||||
*/
|
||||
final class GetCalledClassToSelfClassRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function __construct(private readonly ClassAnalyzer $classAnalyzer)
|
||||
{
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change get_called_class() to self::class on final class', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function callOnMe()
|
||||
{
|
||||
var_dump(get_called_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 [FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isName($node, 'get_called_class')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
|
||||
if (! $class instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($class->isFinal()) {
|
||||
return $this->nodeFactory->createClassConstFetch(ObjectReference::SELF(), 'class');
|
||||
}
|
||||
|
||||
if ($this->classAnalyzer->isAnonymousClass($class)) {
|
||||
return $this->nodeFactory->createClassConstFetch(ObjectReference::SELF(), 'class');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function provideMinPhpVersion(): int
|
||||
{
|
||||
return PhpVersionFeature::CLASSNAME_CONSTANT;
|
||||
}
|
||||
}
|
|
@ -6,7 +6,9 @@ namespace Rector\Php74\Rector\FuncCall;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\Enum\ObjectReference;
|
||||
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
|
@ -20,9 +22,13 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class GetCalledClassToStaticClassRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function __construct(private readonly ClassAnalyzer $classAnalyzer)
|
||||
{
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change get_called_class() to static::class', [
|
||||
return new RuleDefinition('Change get_called_class() to static::class on non-final class', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
|
@ -64,7 +70,20 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
return $this->nodeFactory->createClassConstFetch(ObjectReference::STATIC(), 'class');
|
||||
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
if (! $class instanceof Class_) {
|
||||
return $this->nodeFactory->createClassConstFetch(ObjectReference::STATIC(), 'class');
|
||||
}
|
||||
|
||||
if ($this->classAnalyzer->isAnonymousClass($class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $class->isFinal()) {
|
||||
return $this->nodeFactory->createClassConstFetch(ObjectReference::STATIC(), 'class');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function provideMinPhpVersion(): int
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\GetCalledClassToSelfAndStatic\Fixture;
|
||||
|
||||
final class OnFinalClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
var_dump( get_called_class() );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\GetCalledClassToSelfAndStatic\Fixture;
|
||||
|
||||
final class OnFinalClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
var_dump( self::class );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\GetCalledClassToSelfAndStatic\Fixture;
|
||||
|
||||
class OnNonFinalClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
var_dump( get_called_class() );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\GetCalledClassToSelfAndStatic\Fixture;
|
||||
|
||||
class OnNonFinalClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
var_dump( static::class );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\GetCalledClassToSelfAndStatic;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class GetCalledClassToSelfAndStaticTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Php74\Rector\FuncCall\GetCalledClassToSelfClassRector;
|
||||
use Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(GetCalledClassToSelfClassRector::class);
|
||||
$services->set(GetCalledClassToStaticClassRector::class);
|
||||
};
|
Loading…
Reference in New Issue
Block a user