2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2019-09-23 15:09:26 +00:00
namespace Rector\Renaming\Rector\MethodCall ;
2017-10-06 11:57:21 +00:00
2018-12-11 17:22:54 +00:00
use PhpParser\BuilderHelpers ;
2017-10-06 11:57:21 +00:00
use PhpParser\Node ;
2018-12-11 17:22:54 +00:00
use PhpParser\Node\Expr\ArrayDimFetch ;
2018-12-10 14:45:35 +00:00
use PhpParser\Node\Expr\MethodCall ;
use PhpParser\Node\Expr\StaticCall ;
2017-10-29 00:31:27 +00:00
use PhpParser\Node\Identifier ;
2020-07-13 21:55:06 +00:00
use PhpParser\Node\Stmt\ClassLike ;
2018-12-10 14:45:35 +00:00
use PhpParser\Node\Stmt\ClassMethod ;
2020-07-29 13:55:33 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
2021-02-15 16:21:29 +00:00
use Rector\Core\NodeManipulator\ClassManipulator ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-04-15 20:44:58 +00:00
use Rector\Renaming\Collector\MethodCallRenameCollector ;
2020-08-24 22:26:14 +00:00
use Rector\Renaming\Contract\MethodCallRenameInterface ;
use Rector\Renaming\ValueObject\MethodCallRename ;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2022-02-18 00:42:23 +00:00
use RectorPrefix20220218\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class RenameMethodRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2017-10-06 11:57:21 +00:00
{
2020-07-29 13:55:33 +00:00
/**
2021-12-10 00:25:09 +00:00
* @ deprecated
2020-07-29 13:55:33 +00:00
* @ var string
*/
2020-08-25 00:21:00 +00:00
public const METHOD_CALL_RENAMES = 'method_call_renames' ;
2017-10-06 11:57:21 +00:00
/**
2020-08-24 22:26:14 +00:00
* @ var MethodCallRenameInterface []
2017-10-06 11:57:21 +00:00
*/
2020-08-24 22:26:14 +00:00
private $methodCallRenames = [];
2021-02-15 16:21:29 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\NodeManipulator\ClassManipulator
2021-02-15 16:21:29 +00:00
*/
private $classManipulator ;
2021-04-15 20:44:58 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Renaming\Collector\MethodCallRenameCollector
2021-04-15 20:44:58 +00:00
*/
private $methodCallRenameCollector ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Core\NodeManipulator\ClassManipulator $classManipulator , \Rector\Renaming\Collector\MethodCallRenameCollector $methodCallRenameCollector )
2021-05-09 20:15:43 +00:00
{
2021-02-15 16:21:29 +00:00
$this -> classManipulator = $classManipulator ;
2021-04-15 20:44:58 +00:00
$this -> methodCallRenameCollector = $methodCallRenameCollector ;
2021-02-15 16:21:29 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-04-08 11:51:26 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Turns method names to new ones.' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2018-10-22 17:39:10 +00:00
$someObject = new SomeExampleClass ;
2018-04-09 12:48:15 +00:00
$someObject -> oldMethod ();
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2018-10-22 17:39:10 +00:00
$someObject = new SomeExampleClass ;
2018-04-09 12:48:15 +00:00
$someObject -> newMethod ();
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-12-06 21:12:03 +00:00
, [ new \Rector\Renaming\ValueObject\MethodCallRename ( 'SomeExampleClass' , 'oldMethod' , 'newMethod' )])]);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-08-14 22:12:41 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2017-10-06 11:57:21 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\MethodCall :: class , \PhpParser\Node\Expr\StaticCall :: class , \PhpParser\Node\Stmt\ClassMethod :: class ];
2017-10-06 11:57:21 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param MethodCall | StaticCall | ClassMethod $node
2017-10-06 11:57:21 +00:00
*/
2021-12-10 10:22:23 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2017-10-06 11:57:21 +00:00
{
2020-08-24 22:26:14 +00:00
foreach ( $this -> methodCallRenames as $methodCallRename ) {
2021-11-05 13:48:57 +00:00
$implementsInterface = $this -> classManipulator -> hasParentMethodOrInterface ( $methodCallRename -> getObjectType (), $methodCallRename -> getOldMethod (), $methodCallRename -> getNewMethod ());
2021-02-15 16:21:29 +00:00
if ( $implementsInterface ) {
continue ;
}
2021-11-02 09:33:40 +00:00
if ( ! $this -> nodeTypeResolver -> isMethodStaticCallOrClassMethodObjectType ( $node , $methodCallRename -> getObjectType ())) {
2018-10-22 17:17:17 +00:00
continue ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node -> name , $methodCallRename -> getOldMethod ())) {
2020-08-24 22:26:14 +00:00
continue ;
}
2021-07-25 16:57:05 +00:00
if ( $this -> shouldSkipClassMethod ( $node , $methodCallRename )) {
2020-08-24 22:26:14 +00:00
continue ;
}
2021-05-10 22:23:08 +00:00
$node -> name = new \PhpParser\Node\Identifier ( $methodCallRename -> getNewMethod ());
if ( $methodCallRename instanceof \Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey && ! $node instanceof \PhpParser\Node\Stmt\ClassMethod ) {
return new \PhpParser\Node\Expr\ArrayDimFetch ( $node , \PhpParser\BuilderHelpers :: normalizeValue ( $methodCallRename -> getArrayKey ()));
2018-10-22 17:17:17 +00:00
}
2020-08-24 22:26:14 +00:00
return $node ;
2018-08-14 22:12:41 +00:00
}
2018-12-11 17:22:54 +00:00
return null ;
2018-08-14 22:12:41 +00:00
}
2021-02-28 07:47:48 +00:00
/**
2021-11-28 17:01:20 +00:00
* @ param mixed [] $configuration
2021-02-28 07:47:48 +00:00
*/
2021-12-10 10:22:23 +00:00
public function configure ( array $configuration ) : void
2020-07-29 13:55:33 +00:00
{
2021-11-28 17:01:20 +00:00
$methodCallRenames = $configuration [ self :: METHOD_CALL_RENAMES ] ? ? $configuration ;
2022-02-18 00:42:23 +00:00
\RectorPrefix20220218\Webmozart\Assert\Assert :: allIsAOf ( $methodCallRenames , \Rector\Renaming\Contract\MethodCallRenameInterface :: class );
2020-08-24 22:37:12 +00:00
$this -> methodCallRenames = $methodCallRenames ;
2021-04-28 00:11:40 +00:00
$this -> methodCallRenameCollector -> addMethodCallRenames ( $methodCallRenames );
2020-07-29 13:55:33 +00:00
}
2020-08-05 20:45:36 +00:00
/**
2021-08-23 00:20:32 +00:00
* @ param \PhpParser\Node\Expr\MethodCall | \PhpParser\Node\Expr\StaticCall | \PhpParser\Node\Stmt\ClassMethod $node
2020-08-05 20:45:36 +00:00
*/
2021-07-25 16:57:05 +00:00
private function shouldSkipClassMethod ( $node , \Rector\Renaming\Contract\MethodCallRenameInterface $methodCallRename ) : bool
2020-08-05 20:45:36 +00:00
{
2021-05-10 22:23:08 +00:00
if ( ! $node instanceof \PhpParser\Node\Stmt\ClassMethod ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-08-05 20:45:36 +00:00
}
2021-06-23 17:04:26 +00:00
return $this -> shouldSkipForAlreadyExistingClassMethod ( $node , $methodCallRename );
2018-12-10 14:45:35 +00:00
}
2021-05-10 22:23:08 +00:00
private function shouldSkipForAlreadyExistingClassMethod ( \PhpParser\Node\Stmt\ClassMethod $classMethod , \Rector\Renaming\Contract\MethodCallRenameInterface $methodCallRename ) : bool
2021-05-09 20:15:43 +00:00
{
2021-11-06 12:10:48 +00:00
$classLike = $this -> betterNodeFinder -> findParentType ( $classMethod , \PhpParser\Node\Stmt\ClassLike :: class );
2021-05-10 22:23:08 +00:00
if ( ! $classLike instanceof \PhpParser\Node\Stmt\ClassLike ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-07-13 21:55:06 +00:00
}
2020-08-24 22:26:14 +00:00
return ( bool ) $classLike -> getMethod ( $methodCallRename -> getNewMethod ());
2020-07-13 21:55:06 +00:00
}
2017-10-06 11:57:21 +00:00
}