2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2021-02-20 23:21:19 +00:00
namespace Rector\Arguments\Rector\ClassMethod ;
2018-05-01 17:31:41 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\MethodCall ;
use PhpParser\Node\Expr\StaticCall ;
use PhpParser\Node\Stmt\ClassMethod ;
2021-06-10 10:46:24 +00:00
use Rector\Arguments\ArgumentDefaultValueReplacer ;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue ;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-12-28 12:08:43 +00:00
use RectorPrefix20211228\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-06-10 10:46:24 +00:00
* @ see \Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\ReplaceArgumentDefaultValueRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-06-10 10:46:24 +00:00
final class ReplaceArgumentDefaultValueRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2018-05-01 17:31:41 +00:00
{
2020-07-29 23:39:41 +00:00
/**
2021-12-10 00:25:09 +00:00
* @ deprecated
2020-07-29 23:39:41 +00:00
* @ var string
*/
2020-08-25 22:21:41 +00:00
public const REPLACED_ARGUMENTS = 'replaced_arguments' ;
2020-05-06 21:39:33 +00:00
/**
2021-06-10 10:46:24 +00:00
* @ var ReplaceArgumentDefaultValue []
2018-10-31 11:53:59 +00:00
*/
2020-08-25 22:21:41 +00:00
private $replacedArguments = [];
2021-05-17 23:17:47 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-17 23:17:47 +00:00
* @ var \Rector\Arguments\ArgumentDefaultValueReplacer
*/
private $argumentDefaultValueReplacer ;
public function __construct ( \Rector\Arguments\ArgumentDefaultValueReplacer $argumentDefaultValueReplacer )
{
$this -> argumentDefaultValueReplacer = $argumentDefaultValueReplacer ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-05-01 17:31:41 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Replaces defined map of arguments in defined methods and their calls.' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2018-05-04 12:55:47 +00:00
$someObject = new SomeClass ;
$someObject -> someMethod ( SomeClass :: OLD_CONSTANT );
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2018-05-04 12:55:47 +00:00
$someObject = new SomeClass ;
2021-07-28 12:55:48 +00:00
$someObject -> someMethod ( false );
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-12-06 21:12:03 +00:00
, [ new \Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue ( 'SomeClass' , 'someMethod' , 0 , 'SomeClass::OLD_CONSTANT' , \false )])]);
2018-05-01 17:31:41 +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
2018-05-01 17:31:41 +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 ];
2018-05-01 17:31:41 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param MethodCall | StaticCall | ClassMethod $node
2021-07-05 15:06:41 +00:00
* @ return \PhpParser\Node\Expr\MethodCall | \PhpParser\Node\Expr\StaticCall | \PhpParser\Node\Stmt\ClassMethod
2018-05-01 17:31:41 +00:00
*/
2021-12-10 10:22:23 +00:00
public function refactor ( \PhpParser\Node $node )
2018-05-01 17:31:41 +00:00
{
2020-08-25 22:21:41 +00:00
foreach ( $this -> replacedArguments as $replacedArgument ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeTypeResolver -> isMethodStaticCallOrClassMethodObjectType ( $node , $replacedArgument -> getObjectType ())) {
2018-10-21 19:43:47 +00:00
continue ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node -> name , $replacedArgument -> getMethod ())) {
2020-08-25 22:21:41 +00:00
continue ;
2018-10-21 19:43:47 +00:00
}
2021-05-17 23:17:47 +00:00
$this -> argumentDefaultValueReplacer -> processReplaces ( $node , $replacedArgument );
2018-10-21 19:43:47 +00:00
}
2018-05-01 17:31:41 +00:00
return $node ;
}
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-07-29 23:39:41 +00:00
{
2021-11-28 17:01:20 +00:00
$replacedArguments = $configuration [ self :: REPLACED_ARGUMENTS ] ? ? $configuration ;
2021-12-28 12:08:43 +00:00
\RectorPrefix20211228\Webmozart\Assert\Assert :: isArray ( $replacedArguments );
\RectorPrefix20211228\Webmozart\Assert\Assert :: allIsAOf ( $replacedArguments , \Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue :: class );
2020-08-25 22:21:41 +00:00
$this -> replacedArguments = $replacedArguments ;
2020-07-29 23:39:41 +00:00
}
2018-05-01 17:31:41 +00:00
}