-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rectorphp/rector-src@90bbd4e [Php74] Add GetCalledClassToSelfClassRector (#1971)
- Loading branch information
1 parent
c6551c2
commit a641d86
Showing
11 changed files
with
156 additions
and
30 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
85 changes: 85 additions & 0 deletions
85
rules/Php74/Rector/FuncCall/GetCalledClassToSelfClassRector.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,85 @@ | ||
<?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 \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface | ||
{ | ||
/** | ||
* @readonly | ||
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer | ||
*/ | ||
private $classAnalyzer; | ||
public function __construct(\Rector\Core\NodeAnalyzer\ClassAnalyzer $classAnalyzer) | ||
{ | ||
$this->classAnalyzer = $classAnalyzer; | ||
} | ||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition | ||
{ | ||
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change get_called_class() to self::class on final class', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 [\PhpParser\Node\Expr\FuncCall::class]; | ||
} | ||
/** | ||
* @param FuncCall $node | ||
*/ | ||
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node | ||
{ | ||
if (!$this->isName($node, 'get_called_class')) { | ||
return null; | ||
} | ||
$class = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\Class_::class); | ||
if (!$class instanceof \PhpParser\Node\Stmt\Class_) { | ||
return null; | ||
} | ||
if ($class->isFinal()) { | ||
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::SELF(), 'class'); | ||
} | ||
if ($this->classAnalyzer->isAnonymousClass($class)) { | ||
return $this->nodeFactory->createClassConstFetch(\Rector\Core\Enum\ObjectReference::SELF(), 'class'); | ||
} | ||
return null; | ||
} | ||
public function provideMinPhpVersion() : int | ||
{ | ||
return \Rector\Core\ValueObject\PhpVersionFeature::CLASSNAME_CONSTANT; | ||
} | ||
} |
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
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
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
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