2022-03-27 18:45:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php55\Rector\FuncCall;
|
2022-03-27 18:45:00 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2023-06-09 16:23:18 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
2023-12-23 21:26:40 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\Reflection\ClassModifierChecker;
|
|
|
|
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;
|
2022-03-27 18:45:00 +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/GU9dP
|
2022-03-28 09:17:08 +00:00
|
|
|
* @see \Rector\Tests\Php55\Rector\FuncCall\GetCalledClassToSelfClassRector\GetCalledClassToSelfClassRectorTest
|
2022-03-27 18:45:00 +00:00
|
|
|
*/
|
2023-12-23 21:26:40 +00:00
|
|
|
final class GetCalledClassToSelfClassRector extends AbstractRector implements MinPhpVersionInterface
|
2022-03-27 18:45:00 +00:00
|
|
|
{
|
2023-12-23 21:26:40 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ClassModifierChecker
|
2023-12-23 21:26:40 +00:00
|
|
|
*/
|
|
|
|
private $classModifierChecker;
|
|
|
|
public function __construct(ClassModifierChecker $classModifierChecker)
|
|
|
|
{
|
|
|
|
$this->classModifierChecker = $classModifierChecker;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2022-03-27 18:45:00 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change get_called_class() to self::class on final class', [new CodeSample(<<<'CODE_SAMPLE'
|
2022-03-27 18:45:00 +00:00
|
|
|
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
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2022-03-27 18:45:00 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
2023-12-23 21:26:40 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2022-03-27 18:45:00 +00:00
|
|
|
{
|
|
|
|
if (!$this->isName($node, 'get_called_class')) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-23 21:26:40 +00:00
|
|
|
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
|
|
|
if (!$scope instanceof Scope) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 15:59:30 +00:00
|
|
|
if (!$scope->isInClass()) {
|
2022-03-27 18:45:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-12-23 21:26:40 +00:00
|
|
|
if ($this->classModifierChecker->isInsideFinalClass($node)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return $this->nodeFactory->createClassConstFetch(ObjectReference::SELF, 'class');
|
2022-03-27 18:45:00 +00:00
|
|
|
}
|
2023-12-23 21:26:40 +00:00
|
|
|
if ($scope->isInAnonymousFunction()) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return $this->nodeFactory->createClassConstFetch(ObjectReference::SELF, 'class');
|
2022-03-27 18:45:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::CLASSNAME_CONSTANT;
|
2022-03-27 18:45:00 +00:00
|
|
|
}
|
|
|
|
}
|