2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2019-09-25 21:08:32 +00:00
namespace Rector\CodingStyle\Rector\FuncCall ;
use PhpParser\Node ;
use PhpParser\Node\Arg ;
2021-05-03 19:03:16 +00:00
use PhpParser\Node\Expr ;
use PhpParser\Node\Expr\Array_ ;
2019-09-25 21:08:32 +00:00
use PhpParser\Node\Expr\FuncCall ;
2021-05-03 19:03:16 +00:00
use PhpParser\Node\Expr\MethodCall ;
use PhpParser\Node\Scalar\String_ ;
2021-05-04 14:59:15 +00:00
use Rector\CodingStyle\NodeFactory\ArrayCallableToMethodCallFactory ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-05-03 21:27:20 +00:00
use Rector\Core\ValueObject\PhpVersionFeature ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-09-25 21:08:32 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// www . php . net / manual / en / function . call - user - func - array . php #117655
2021-05-03 19:03:16 +00:00
* @ changelog https :// 3 v4l . org / CBWt9
2019-09-25 21:08:32 +00:00
*
2021-05-04 14:59:15 +00:00
* @ see \Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector\CallUserFuncArrayToVariadicRectorTest
2019-09-25 21:08:32 +00:00
*/
2021-05-10 22:23:08 +00:00
final class CallUserFuncArrayToVariadicRector extends \Rector\Core\Rector\AbstractRector
2019-09-25 21:08:32 +00:00
{
2021-05-04 14:59:15 +00:00
/**
* @ var ArrayCallableToMethodCallFactory
*/
private $arrayCallableToMethodCallFactory ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\CodingStyle\NodeFactory\ArrayCallableToMethodCallFactory $arrayCallableToMethodCallFactory )
2021-05-04 14:59:15 +00:00
{
$this -> arrayCallableToMethodCallFactory = $arrayCallableToMethodCallFactory ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-09-25 21:08:32 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Replace call_user_func_array() with variadic' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2019-09-25 21:08:32 +00:00
class SomeClass
{
public function run ()
{
call_user_func_array ( 'some_function' , $items );
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-09-25 21:08:32 +00:00
class SomeClass
{
public function run ()
{
some_function ( ... $items );
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-09-25 21:08:32 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-09-25 21:08:32 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-09-25 21:08:32 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\FuncCall :: class ];
2019-09-25 21:08:32 +00:00
}
/**
* @ param FuncCall $node
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-09-25 21:08:32 +00:00
{
2021-05-10 22:23:08 +00:00
if ( ! $this -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: ARRAY_SPREAD )) {
2021-05-03 21:27:20 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node , 'call_user_func_array' )) {
2019-09-25 21:08:32 +00:00
return null ;
}
2021-05-03 19:03:16 +00:00
$firstArgValue = $node -> args [ 0 ] -> value ;
$secondArgValue = $node -> args [ 1 ] -> value ;
2021-05-10 22:23:08 +00:00
if ( $firstArgValue instanceof \PhpParser\Node\Scalar\String_ ) {
2021-05-03 19:03:16 +00:00
$functionName = $this -> valueResolver -> getValue ( $firstArgValue );
return $this -> createFuncCall ( $secondArgValue , $functionName );
}
// method call
2021-05-10 22:23:08 +00:00
if ( $firstArgValue instanceof \PhpParser\Node\Expr\Array_ ) {
2021-05-03 19:03:16 +00:00
return $this -> createMethodCall ( $firstArgValue , $secondArgValue );
2019-12-15 19:41:41 +00:00
}
2021-05-03 19:03:16 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
private function createFuncCall ( \PhpParser\Node\Expr $expr , string $functionName ) : \PhpParser\Node\Expr\FuncCall
2021-05-03 19:03:16 +00:00
{
2019-09-25 21:08:32 +00:00
$args = [];
2021-05-03 21:27:20 +00:00
$args [] = $this -> createUnpackedArg ( $expr );
2021-01-30 21:41:25 +00:00
return $this -> nodeFactory -> createFuncCall ( $functionName , $args );
2019-09-25 21:08:32 +00:00
}
2021-05-10 22:23:08 +00:00
private function createMethodCall ( \PhpParser\Node\Expr\Array_ $array , \PhpParser\Node\Expr $secondExpr ) : ? \PhpParser\Node\Expr\MethodCall
2021-05-03 19:03:16 +00:00
{
2021-05-04 14:59:15 +00:00
$methodCall = $this -> arrayCallableToMethodCallFactory -> create ( $array );
2021-05-10 22:23:08 +00:00
if ( ! $methodCall instanceof \PhpParser\Node\Expr\MethodCall ) {
2021-05-03 19:03:16 +00:00
return null ;
}
2021-05-04 14:59:15 +00:00
$methodCall -> args [] = $this -> createUnpackedArg ( $secondExpr );
return $methodCall ;
2021-05-03 19:03:16 +00:00
}
2021-05-10 22:23:08 +00:00
private function createUnpackedArg ( \PhpParser\Node\Expr $expr ) : \PhpParser\Node\Arg
2021-05-03 21:27:20 +00:00
{
2021-05-10 22:23:08 +00:00
return new \PhpParser\Node\Arg ( $expr , \false , \true );
2021-05-03 21:27:20 +00:00
}
2019-09-25 21:08:32 +00:00
}