2022-03-27 18:45:00 +00:00
< ? php
declare ( strict_types = 1 );
2022-03-28 09:17:08 +00:00
namespace Rector\Php55\Rector\FuncCall ;
2022-03-27 18:45:00 +00:00
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 ;
/**
2022-03-28 09:17:08 +00:00
* @ changelog https :// www . php . net / ChangeLog - 5. php #5.5.0
2022-03-27 18:45:00 +00:00
* @ see https :// 3 v4l . 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
*/
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 ;
}
}