2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php55\Rector\FuncCall;
|
2018-12-31 09:25:07 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2023-06-09 15:59:30 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-31 09:25:07 +00:00
|
|
|
/**
|
2022-03-28 09:17:08 +00:00
|
|
|
* @changelog https://www.php.net/ChangeLog-5.php#5.5.0
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/dJgXd
|
2022-03-28 09:17:08 +00:00
|
|
|
* @see \Rector\Tests\Php55\Rector\FuncCall\GetCalledClassToStaticClassRector\GetCalledClassToStaticClassRectorTest
|
2018-12-31 09:25:07 +00:00
|
|
|
*/
|
2023-06-09 15:59:30 +00:00
|
|
|
final class GetCalledClassToStaticClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
2018-12-31 09:25:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-31 09:25:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change get_called_class() to static::class on non-final class', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-31 09:25:07 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function callOnMe()
|
|
|
|
{
|
2019-09-21 22:13:26 +00:00
|
|
|
var_dump(get_called_class());
|
2018-12-31 09:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-31 09:25:07 +00:00
|
|
|
class SomeClass
|
2019-09-21 22:13:26 +00:00
|
|
|
{
|
|
|
|
public function callOnMe()
|
|
|
|
{
|
|
|
|
var_dump(static::class);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-31 09:25:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-31 09:25:07 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-31 09:25:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2018-12-31 09:25:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param FuncCall $node
|
2018-12-31 09:25:07 +00:00
|
|
|
*/
|
2023-06-09 15:59:30 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2018-12-31 09:25:07 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'get_called_class')) {
|
2018-12-31 09:25:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 15:59:30 +00:00
|
|
|
if (!$scope->isInClass()) {
|
|
|
|
return null;
|
2022-03-27 18:45:00 +00:00
|
|
|
}
|
2023-06-09 15:59:30 +00:00
|
|
|
$classReflection = $scope->getClassReflection();
|
|
|
|
if ($classReflection->isAnonymous()) {
|
2022-03-27 18:45:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 15:59:30 +00:00
|
|
|
if (!$classReflection->isFinal()) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return $this->nodeFactory->createClassConstFetch(ObjectReference::STATIC, 'class');
|
2022-03-27 18:45:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
2018-12-31 09:25:07 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::CLASSNAME_CONSTANT;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2018-12-31 09:25:07 +00:00
|
|
|
}
|