2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2021-01-29 08:08:55 +00:00
namespace Rector\Removing\Rector\ClassMethod ;
2018-05-01 17:31:41 +00:00
use PhpParser\Node ;
2018-07-17 20:48:31 +00:00
use PhpParser\Node\Arg ;
2018-05-01 17:31:41 +00:00
use PhpParser\Node\Expr\MethodCall ;
use PhpParser\Node\Expr\StaticCall ;
use PhpParser\Node\Stmt\ClassMethod ;
2021-09-27 15:43:15 +00:00
use PhpParser\Node\VariadicPlaceholder ;
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 ;
2021-01-29 10:38:59 +00:00
use Rector\Removing\ValueObject\ArgumentRemover ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2022-04-25 07:10:07 +00:00
use RectorPrefix20220425\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Removing\Rector\ClassMethod\ArgumentRemoverRector\ArgumentRemoverRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ArgumentRemoverRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2018-05-01 17:31:41 +00:00
{
/**
2020-09-12 21:19:08 +00:00
* @ var ArgumentRemover []
2018-05-01 17:31:41 +00:00
*/
2020-08-25 19:05:58 +00:00
private $removedArguments = [];
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 ( 'Removes defined arguments in defined methods and their calls.' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2018-05-04 12:56:35 +00:00
$someObject = new SomeClass ;
$someObject -> someMethod ( true );
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2018-05-04 12:56:35 +00:00
$someObject = new SomeClass ;
2021-07-28 12:55:48 +00:00
$someObject -> someMethod ();
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-12-06 21:12:03 +00:00
, [ new \Rector\Removing\ValueObject\ArgumentRemover ( 'ExampleClass' , 'someMethod' , 0 , [ \true ])])]);
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 19:05:58 +00:00
foreach ( $this -> removedArguments as $removedArgument ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeTypeResolver -> isMethodStaticCallOrClassMethodObjectType ( $node , $removedArgument -> getObjectType ())) {
2018-10-21 19:43:47 +00:00
continue ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node -> name , $removedArgument -> getMethod ())) {
2020-08-25 19:05:58 +00:00
continue ;
2018-10-21 19:43:47 +00:00
}
2020-08-25 19:05:58 +00:00
$this -> processPosition ( $node , $removedArgument );
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
{
2022-04-25 07:10:07 +00:00
\RectorPrefix20220425\Webmozart\Assert\Assert :: allIsAOf ( $configuration , \Rector\Removing\ValueObject\ArgumentRemover :: class );
2022-02-18 01:46:17 +00:00
$this -> removedArguments = $configuration ;
2020-07-29 23:39:41 +00:00
}
2018-05-01 17:31:41 +00:00
/**
2021-10-30 14:18:31 +00:00
* @ param \PhpParser\Node\Expr\MethodCall | \PhpParser\Node\Expr\StaticCall | \PhpParser\Node\Stmt\ClassMethod $node
2018-05-01 17:31:41 +00:00
*/
2021-06-29 14:24:45 +00:00
private function processPosition ( $node , \Rector\Removing\ValueObject\ArgumentRemover $argumentRemover ) : void
2018-05-01 17:31:41 +00:00
{
2020-09-12 21:19:08 +00:00
if ( $argumentRemover -> getValue () === null ) {
2021-05-10 22:23:08 +00:00
if ( $node instanceof \PhpParser\Node\Expr\MethodCall || $node instanceof \PhpParser\Node\Expr\StaticCall ) {
2021-02-09 03:55:21 +00:00
$this -> nodeRemover -> removeArg ( $node , $argumentRemover -> getPosition ());
2018-12-16 23:00:16 +00:00
} else {
2021-02-09 03:55:21 +00:00
$this -> nodeRemover -> removeParam ( $node , $argumentRemover -> getPosition ());
2018-05-01 17:31:41 +00:00
}
2020-08-25 19:05:58 +00:00
return ;
2018-05-01 17:31:41 +00:00
}
2020-09-12 21:19:08 +00:00
$match = $argumentRemover -> getValue ();
2020-08-25 19:05:58 +00:00
if ( isset ( $match [ 'name' ])) {
2020-09-12 21:19:08 +00:00
$this -> removeByName ( $node , $argumentRemover -> getPosition (), $match [ 'name' ]);
2020-08-25 19:05:58 +00:00
return ;
}
// only argument specific value can be removed
2021-05-10 22:23:08 +00:00
if ( $node instanceof \PhpParser\Node\Stmt\ClassMethod ) {
2020-12-24 16:28:56 +00:00
return ;
}
2021-05-09 20:15:43 +00:00
if ( ! isset ( $node -> args [ $argumentRemover -> getPosition ()])) {
2020-08-25 19:05:58 +00:00
return ;
}
2020-09-12 21:19:08 +00:00
if ( $this -> isArgumentValueMatch ( $node -> args [ $argumentRemover -> getPosition ()], $match )) {
2021-02-09 03:55:21 +00:00
$this -> nodeRemover -> removeArg ( $node , $argumentRemover -> getPosition ());
2018-05-01 17:31:41 +00:00
}
}
2019-03-08 23:53:12 +00:00
/**
2021-10-30 14:18:31 +00:00
* @ param \PhpParser\Node\Expr\MethodCall | \PhpParser\Node\Expr\StaticCall | \PhpParser\Node\Stmt\ClassMethod $node
2019-03-08 23:53:12 +00:00
*/
2021-06-29 14:24:45 +00:00
private function removeByName ( $node , int $position , string $name ) : void
2019-03-08 23:53:12 +00:00
{
2021-05-10 22:23:08 +00:00
if ( $node instanceof \PhpParser\Node\Expr\MethodCall || $node instanceof \PhpParser\Node\Expr\StaticCall ) {
2019-03-08 23:53:54 +00:00
if ( isset ( $node -> args [ $position ]) && $this -> isName ( $node -> args [ $position ], $name )) {
2021-02-09 01:52:35 +00:00
$this -> nodeRemover -> removeArg ( $node , $position );
2019-03-08 23:53:12 +00:00
}
2019-03-08 23:53:54 +00:00
return ;
2019-03-08 23:53:12 +00:00
}
2021-05-09 20:15:43 +00:00
if ( ! ( isset ( $node -> params [ $position ]) && $this -> isName ( $node -> params [ $position ], $name ))) {
2019-03-08 23:53:12 +00:00
return ;
}
2021-02-09 01:52:35 +00:00
$this -> nodeRemover -> removeParam ( $node , $position );
2019-03-08 23:53:12 +00:00
}
2019-10-30 09:49:07 +00:00
/**
* @ param mixed [] $values
2021-09-27 15:43:15 +00:00
* @ param \PhpParser\Node\Arg | \PhpParser\Node\VariadicPlaceholder $arg
2019-10-30 09:49:07 +00:00
*/
2021-09-27 15:43:15 +00:00
private function isArgumentValueMatch ( $arg , array $values ) : bool
2019-10-30 09:49:07 +00:00
{
2021-09-27 15:43:15 +00:00
if ( ! $arg instanceof \PhpParser\Node\Arg ) {
return \false ;
}
2021-01-30 23:20:05 +00:00
$nodeValue = $this -> valueResolver -> getValue ( $arg -> value );
2021-05-09 20:15:43 +00:00
return \in_array ( $nodeValue , $values , \true );
2019-10-30 09:49:07 +00:00
}
2018-05-01 17:31:41 +00:00
}