2020-09-10 07:27:03 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-09-10 07:27:03 +00:00
namespace Rector\RemovingStatic\Rector\ClassMethod ;
use PhpParser\Node ;
use PhpParser\Node\Expr\MethodCall ;
use PhpParser\Node\Expr\StaticCall ;
use PhpParser\Node\Expr\Variable ;
2021-06-23 17:04:26 +00:00
use PhpParser\Node\Stmt\ClassLike ;
2020-09-10 07:27:03 +00:00
use PhpParser\Node\Stmt\ClassMethod ;
2021-02-28 07:47:48 +00:00
use PHPStan\Reflection\ClassReflection ;
2021-06-23 17:04:26 +00:00
use PHPStan\Type\ObjectType ;
2020-09-10 07:27:03 +00:00
use Rector\Core\Rector\AbstractRector ;
use Rector\NodeTypeResolver\Node\AttributeKey ;
2021-01-23 09:46:25 +00:00
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2020-09-10 07:27:03 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\RemovingStatic\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector\LocallyCalledStaticMethodToNonStaticRectorTest
2020-09-10 07:27:03 +00:00
*/
2021-05-10 22:23:08 +00:00
final class LocallyCalledStaticMethodToNonStaticRector extends \Rector\Core\Rector\AbstractRector
2020-09-10 07:27:03 +00:00
{
2021-01-23 09:46:25 +00:00
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard
2021-01-23 09:46:25 +00:00
*/
private $classMethodVisibilityGuard ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard $classMethodVisibilityGuard )
2021-01-23 09:46:25 +00:00
{
$this -> classMethodVisibilityGuard = $classMethodVisibilityGuard ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-09-10 07:27:03 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change static method and local-only calls to non-static' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2020-09-10 07:27:03 +00:00
class SomeClass
{
public function run ()
{
self :: someStatic ();
}
private static function someStatic ()
{
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2020-09-10 07:27:03 +00:00
class SomeClass
{
public function run ()
{
$this -> someStatic ();
}
private function someStatic ()
{
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2020-09-10 07:27:03 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-09-10 07:27:03 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-09-10 07:27:03 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\ClassMethod :: class , \PhpParser\Node\Expr\StaticCall :: class ];
2020-09-10 07:27:03 +00:00
}
/**
* @ param ClassMethod | StaticCall $node
*/
2021-07-05 22:50:18 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2020-09-10 07:27:03 +00:00
{
2021-05-10 22:23:08 +00:00
if ( $node instanceof \PhpParser\Node\Stmt\ClassMethod ) {
2021-06-23 17:04:26 +00:00
if ( ! $node -> isPrivate ()) {
return null ;
}
2020-09-10 07:27:03 +00:00
return $this -> refactorClassMethod ( $node );
}
return $this -> refactorStaticCall ( $node );
}
2021-05-10 22:23:08 +00:00
private function refactorClassMethod ( \PhpParser\Node\Stmt\ClassMethod $classMethod ) : ? \PhpParser\Node\Stmt\ClassMethod
2020-09-10 07:27:03 +00:00
{
2021-05-09 20:15:43 +00:00
if ( ! $classMethod -> isStatic ()) {
2020-09-10 07:27:03 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
$scope = $classMethod -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: SCOPE );
2021-02-28 07:47:48 +00:00
$classReflection = $scope -> getClassReflection ();
2021-05-10 22:23:08 +00:00
if ( ! $classReflection instanceof \PHPStan\Reflection\ClassReflection ) {
2021-02-28 07:47:48 +00:00
return null ;
}
if ( $this -> classMethodVisibilityGuard -> isClassMethodVisibilityGuardedByParent ( $classMethod , $classReflection )) {
2021-01-23 09:46:25 +00:00
return null ;
}
2020-09-10 07:27:03 +00:00
// change static calls to non-static ones, but only if in non-static method!!!
2021-01-30 21:41:25 +00:00
$this -> visibilityManipulator -> makeNonStatic ( $classMethod );
2020-09-10 07:27:03 +00:00
return $classMethod ;
}
2021-05-10 22:23:08 +00:00
private function refactorStaticCall ( \PhpParser\Node\Expr\StaticCall $staticCall ) : ? \PhpParser\Node\Expr\MethodCall
2020-09-10 07:27:03 +00:00
{
2021-11-06 12:10:48 +00:00
$classLike = $this -> betterNodeFinder -> findParentType ( $staticCall , \PhpParser\Node\Stmt\ClassLike :: class );
if ( ! $classLike instanceof \PhpParser\Node\Stmt\ClassLike ) {
2020-09-10 07:27:03 +00:00
return null ;
}
2021-06-23 17:04:26 +00:00
/** @var ClassMethod[] $classMethods */
2021-11-06 12:10:48 +00:00
$classMethods = $this -> betterNodeFinder -> findInstanceOf ( $classLike , \PhpParser\Node\Stmt\ClassMethod :: class );
2021-06-23 17:04:26 +00:00
foreach ( $classMethods as $classMethod ) {
if ( ! $this -> isClassMethodMatchingStaticCall ( $classMethod , $staticCall )) {
continue ;
}
if ( $this -> isInStaticClassMethod ( $staticCall )) {
continue ;
}
$thisVariable = new \PhpParser\Node\Expr\Variable ( 'this' );
return new \PhpParser\Node\Expr\MethodCall ( $thisVariable , $staticCall -> name , $staticCall -> args );
2020-09-12 00:13:48 +00:00
}
2021-06-23 17:04:26 +00:00
return null ;
2020-09-10 07:27:03 +00:00
}
2021-05-10 22:23:08 +00:00
private function isInStaticClassMethod ( \PhpParser\Node\Expr\StaticCall $staticCall ) : bool
2020-09-12 00:13:48 +00:00
{
2021-11-06 14:25:01 +00:00
$locationClassMethod = $this -> betterNodeFinder -> findParentType ( $staticCall , \PhpParser\Node\Stmt\ClassMethod :: class );
2021-05-10 22:23:08 +00:00
if ( ! $locationClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-09-12 00:13:48 +00:00
}
return $locationClassMethod -> isStatic ();
}
2021-06-23 17:04:26 +00:00
private function isClassMethodMatchingStaticCall ( \PhpParser\Node\Stmt\ClassMethod $classMethod , \PhpParser\Node\Expr\StaticCall $staticCall ) : bool
{
2021-11-07 04:15:35 +00:00
$classLike = $this -> betterNodeFinder -> findParentType ( $classMethod , \PhpParser\Node\Stmt\ClassLike :: class );
if ( ! $classLike instanceof \PhpParser\Node\Stmt\ClassLike ) {
return \false ;
}
2021-11-09 13:36:07 +00:00
$className = ( string ) $this -> nodeNameResolver -> getName ( $classLike );
2021-06-23 17:04:26 +00:00
$objectType = new \PHPStan\Type\ObjectType ( $className );
2021-10-07 19:40:12 +00:00
$callerType = $this -> nodeTypeResolver -> getType ( $staticCall -> class );
2021-06-23 17:04:26 +00:00
return $objectType -> equals ( $callerType );
}
2020-09-10 07:27:03 +00:00
}