2020-05-31 14:19:58 +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\FuncCall ;
2020-05-31 14:19:58 +00:00
use PhpParser\Node ;
2021-02-17 14:17:16 +00:00
use PhpParser\Node\Expr ;
2020-05-31 14:19:58 +00:00
use PhpParser\Node\Expr\FuncCall ;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
2020-05-31 14:19:58 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-01-29 10:38:59 +00:00
use Rector\Removing\ValueObject\RemoveFuncCallArg ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-11-28 00:36:13 +00:00
use RectorPrefix20211128\Webmozart\Assert\Assert ;
2020-05-31 14:19:58 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Removing\Rector\FuncCall\RemoveFuncCallArgRector\RemoveFuncCallArgRectorTest
2020-05-31 14:19:58 +00:00
*/
2021-05-10 22:23:08 +00:00
final class RemoveFuncCallArgRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2020-05-31 14:19:58 +00:00
{
/**
2020-07-29 23:39:41 +00:00
* @ var string
2020-05-31 14:19:58 +00:00
*/
2020-08-26 10:54:53 +00:00
public const REMOVED_FUNCTION_ARGUMENTS = 'removed_function_arguments' ;
2020-05-31 14:19:58 +00:00
/**
2020-09-12 21:19:08 +00:00
* @ var RemoveFuncCallArg []
2020-05-31 14:19:58 +00:00
*/
2020-08-26 10:54:53 +00:00
private $removedFunctionArguments = [];
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-05-31 14:19:58 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Remove argument by position by function name' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2020-05-31 14:19:58 +00:00
remove_last_arg ( 1 , 2 );
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2020-05-31 14:19:58 +00:00
remove_last_arg ( 1 );
CODE_SAMPLE
2021-05-10 22:23:08 +00:00
, [ self :: REMOVED_FUNCTION_ARGUMENTS => [ new \Rector\Removing\ValueObject\RemoveFuncCallArg ( 'remove_last_arg' , 1 )]])]);
2020-05-31 14:19:58 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-05-31 14:19:58 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-05-31 14:19:58 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\FuncCall :: class ];
2020-05-31 14:19:58 +00:00
}
/**
2021-07-05 22:50:18 +00:00
* @ param FuncCall $node
2020-05-31 14:19:58 +00:00
*/
2021-07-05 22:50:18 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2020-05-31 14:19:58 +00:00
{
2021-05-10 22:23:08 +00:00
if ( $node -> name instanceof \PhpParser\Node\Expr ) {
2021-02-17 14:17:16 +00:00
return null ;
}
2020-08-26 10:54:53 +00:00
foreach ( $this -> removedFunctionArguments as $removedFunctionArgument ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node -> name , $removedFunctionArgument -> getFunction ())) {
2020-05-31 14:19:58 +00:00
continue ;
}
2021-05-09 20:15:43 +00:00
foreach ( \array_keys ( $node -> args ) as $position ) {
2020-08-26 10:54:53 +00:00
if ( $removedFunctionArgument -> getArgumentPosition () !== $position ) {
2020-05-31 14:19:58 +00:00
continue ;
}
2021-02-09 03:55:21 +00:00
$this -> nodeRemover -> removeArg ( $node , $position );
2020-05-31 14:19:58 +00:00
}
}
return $node ;
}
2021-05-02 10:46:55 +00:00
/**
* @ param array < string , RemoveFuncCallArg [] > $configuration
*/
2021-05-09 20:15:43 +00:00
public function configure ( array $configuration ) : void
2020-07-29 23:39:41 +00:00
{
2021-11-27 13:43:28 +00:00
$removedFunctionArguments = $configuration [ self :: REMOVED_FUNCTION_ARGUMENTS ] ? ? ( $configuration ? : []);
2021-11-28 00:36:13 +00:00
\RectorPrefix20211128\Webmozart\Assert\Assert :: allIsInstanceOf ( $removedFunctionArguments , \Rector\Removing\ValueObject\RemoveFuncCallArg :: class );
2020-08-26 10:54:53 +00:00
$this -> removedFunctionArguments = $removedFunctionArguments ;
2020-07-29 23:39:41 +00:00
}
2020-05-31 14:19:58 +00:00
}