2021-05-04 18:07:22 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\CodeQuality\Rector\FuncCall ;
2021-05-04 18:07:22 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Arg ;
use PhpParser\Node\Expr\ArrowFunction ;
use PhpParser\Node\Expr\Closure ;
use PhpParser\Node\Expr\FuncCall ;
use Rector\Core\Rector\AbstractRector ;
use Rector\Php74\NodeAnalyzer\ClosureArrowFunctionAnalyzer ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-05-04 18:07:22 +00:00
/**
* @ changelog https :// 3 v4l . org / fuuEF
*
* @ see \Rector\Tests\CodeQuality\Rector\FuncCall\CallUserFuncWithArrowFunctionToInlineRector\CallUserFuncWithArrowFunctionToInlineRectorTest
*/
2022-06-06 17:12:56 +00:00
final class CallUserFuncWithArrowFunctionToInlineRector extends \Rector\Core\Rector\AbstractRector
2021-05-04 18:07:22 +00:00
{
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Php74\NodeAnalyzer\ClosureArrowFunctionAnalyzer
2021-05-04 18:07:22 +00:00
*/
private $closureArrowFunctionAnalyzer ;
2022-06-06 17:12:56 +00:00
public function __construct ( \Rector\Php74\NodeAnalyzer\ClosureArrowFunctionAnalyzer $closureArrowFunctionAnalyzer )
2021-05-04 18:40:08 +00:00
{
2021-05-04 18:07:22 +00:00
$this -> closureArrowFunctionAnalyzer = $closureArrowFunctionAnalyzer ;
}
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2021-05-04 18:07:22 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Refactor call_user_func() with arrow function to direct call' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2021-05-04 18:07:22 +00:00
final class SomeClass
{
public function run ()
{
$result = \call_user_func ( fn () => 100 );
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2021-05-04 18:07:22 +00:00
final class SomeClass
{
public function run ()
{
$result = 100 ;
}
}
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2021-05-04 18:07:22 +00:00
}
/**
* @ return array < class - string < Node >>
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2021-05-04 18:07:22 +00:00
{
2022-06-06 17:12:56 +00:00
return [ \PhpParser\Node\Expr\FuncCall :: class ];
2021-05-04 18:07:22 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param FuncCall $node
2021-05-04 18:07:22 +00:00
*/
2022-06-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2021-05-04 18:07:22 +00:00
{
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node , 'call_user_func' )) {
2021-05-04 18:07:22 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
if ( \count ( $node -> args ) !== 1 ) {
2021-05-04 18:07:22 +00:00
return null ;
}
// change the node
2021-09-27 15:43:15 +00:00
if ( ! isset ( $node -> args [ 0 ])) {
return null ;
}
$firstArg = $node -> args [ 0 ];
2022-06-06 17:12:56 +00:00
if ( ! $firstArg instanceof \PhpParser\Node\Arg ) {
2021-09-27 15:43:15 +00:00
return null ;
}
$firstArgValue = $firstArg -> value ;
2022-06-06 17:12:56 +00:00
if ( $firstArgValue instanceof \PhpParser\Node\Expr\ArrowFunction ) {
2021-05-04 18:07:22 +00:00
return $firstArgValue -> expr ;
}
2022-06-06 17:12:56 +00:00
if ( $firstArgValue instanceof \PhpParser\Node\Expr\Closure ) {
2021-05-04 18:07:22 +00:00
return $this -> closureArrowFunctionAnalyzer -> matchArrowFunctionExpr ( $firstArgValue );
}
return null ;
}
}