2020-08-12 09:44:34 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-10-27 00:42:04 +00:00
namespace Rector\Transform\Rector\MethodCall ;
2020-08-12 09:44:34 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\MethodCall ;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
use Rector\Core\Rector\AbstractRector ;
2020-09-12 21:19:08 +00:00
use Rector\Transform\ValueObject\ReplaceParentCallByPropertyCall ;
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 ;
2020-08-12 09:44:34 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Transform\Rector\MethodCall\ReplaceParentCallByPropertyCallRector\ReplaceParentCallByPropertyCallRectorTest
2020-08-12 09:44:34 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ReplaceParentCallByPropertyCallRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2020-08-12 09:44:34 +00:00
{
/**
2021-12-10 00:25:09 +00:00
* @ deprecated
2020-08-12 09:44:34 +00:00
* @ var string
*/
2020-08-26 10:54:53 +00:00
public const PARENT_CALLS_TO_PROPERTIES = 'parent_calls_to_properties' ;
2020-08-12 09:44:34 +00:00
/**
2020-09-12 21:19:08 +00:00
* @ var ReplaceParentCallByPropertyCall []
2020-08-12 09:44:34 +00:00
*/
2020-08-26 10:54:53 +00:00
private $parentCallToProperties = [];
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-08-12 09:44:34 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Changes method calls in child of specific types to defined property method call' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2020-08-12 09:44:34 +00:00
final class SomeClass
{
public function run ( SomeTypeToReplace $someTypeToReplace )
{
$someTypeToReplace -> someMethodCall ();
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2020-08-12 09:44:34 +00:00
final class SomeClass
{
public function run ( SomeTypeToReplace $someTypeToReplace )
{
$this -> someProperty -> someMethodCall ();
}
}
CODE_SAMPLE
2021-12-06 21:12:03 +00:00
, [ new \Rector\Transform\ValueObject\ReplaceParentCallByPropertyCall ( 'SomeTypeToReplace' , 'someMethodCall' , 'someProperty' )])]);
2020-08-12 09:44:34 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-08-12 09:44:34 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-08-12 09:44:34 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\MethodCall :: class ];
2020-08-12 09:44:34 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param MethodCall $node
2020-08-12 09:44:34 +00:00
*/
2021-12-10 10:22:23 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2020-08-12 09:44:34 +00:00
{
2020-08-26 10:54:53 +00:00
foreach ( $this -> parentCallToProperties as $parentCallToProperty ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> isObjectType ( $node -> var , $parentCallToProperty -> getObjectType ())) {
2020-08-12 09:44:34 +00:00
continue ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node -> name , $parentCallToProperty -> getMethod ())) {
2020-08-26 10:54:53 +00:00
continue ;
2020-08-12 09:44:34 +00:00
}
2021-01-30 21:41:25 +00:00
$node -> var = $this -> nodeFactory -> createPropertyFetch ( 'this' , $parentCallToProperty -> getProperty ());
2020-08-26 10:54:53 +00:00
return $node ;
2020-08-12 09:44:34 +00:00
}
return null ;
}
2021-05-02 10:46:55 +00:00
/**
2021-11-28 17:01:20 +00:00
* @ param mixed [] $configuration
2021-05-02 10:46:55 +00:00
*/
2021-12-10 10:22:23 +00:00
public function configure ( array $configuration ) : void
2020-08-12 09:44:34 +00:00
{
2021-11-28 17:01:20 +00:00
$parentCallToProperties = $configuration [ self :: PARENT_CALLS_TO_PROPERTIES ] ? ? $configuration ;
2022-02-18 00:42:23 +00:00
\RectorPrefix20220218\Webmozart\Assert\Assert :: allIsAOf ( $parentCallToProperties , \Rector\Transform\ValueObject\ReplaceParentCallByPropertyCall :: class );
2021-05-20 23:00:54 +00:00
$this -> parentCallToProperties = $parentCallToProperties ;
2020-08-12 09:44:34 +00:00
}
}