2019-10-13 05:59:52 +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\Transform\Rector\StaticCall ;
2018-08-11 10:22:29 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\FuncCall ;
use PhpParser\Node\Expr\StaticCall ;
use PhpParser\Node\Name\FullyQualified ;
2024-01-02 02:40:38 +00:00
use Rector\Contract\Rector\ConfigurableRectorInterface ;
use Rector\Rector\AbstractRector ;
2022-06-06 17:12:56 +00:00
use Rector\Transform\ValueObject\StaticCallToFuncCall ;
2022-06-07 09:18:30 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2024-05-02 02:21:56 +00:00
use RectorPrefix202405\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Transform\Rector\StaticCall\StaticCallToFuncCallRector\StaticCallToFuncCallRectorTest
2019-09-03 09:11:45 +00:00
*/
2022-06-07 08:22:29 +00:00
final class StaticCallToFuncCallRector extends AbstractRector implements ConfigurableRectorInterface
2018-08-11 10:22:29 +00:00
{
/**
2021-12-30 16:07:15 +00:00
* @ var StaticCallToFuncCall []
2018-08-11 10:22:29 +00:00
*/
2021-05-11 10:40:34 +00:00
private $staticCallsToFunctions = [];
2022-06-07 08:22:29 +00:00
public function getRuleDefinition () : RuleDefinition
2018-08-11 10:22:29 +00:00
{
2022-06-07 08:22:29 +00:00
return new RuleDefinition ( 'Turns static call to function call.' , [ new ConfiguredCodeSample ( 'OldClass::oldMethod("args");' , 'new_function("args");' , [ new StaticCallToFuncCall ( 'OldClass' , 'oldMethod' , 'new_function' )])]);
2018-08-11 10:22:29 +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-08-14 21:33:39 +00:00
{
2022-06-07 08:22:29 +00:00
return [ StaticCall :: class ];
2018-08-14 21:33:39 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param StaticCall $node
2018-08-14 21:33:39 +00:00
*/
2022-06-07 08:22:29 +00:00
public function refactor ( Node $node ) : ? Node
2018-08-11 10:22:29 +00:00
{
2021-03-05 10:55:40 +00:00
foreach ( $this -> staticCallsToFunctions as $staticCallToFunction ) {
2023-03-22 14:20:22 +00:00
if ( ! $this -> isName ( $node -> name , $staticCallToFunction -> getMethod ())) {
2018-10-31 16:39:39 +00:00
continue ;
2018-08-11 10:22:29 +00:00
}
2023-03-22 14:20:22 +00:00
if ( ! $this -> isObjectType ( $node -> class , $staticCallToFunction -> getObjectType ())) {
2020-08-26 10:54:53 +00:00
continue ;
2018-10-31 16:39:39 +00:00
}
2022-06-07 08:22:29 +00:00
return new FuncCall ( new FullyQualified ( $staticCallToFunction -> getFunction ()), $node -> args );
2018-10-31 16:39:39 +00:00
}
return null ;
2018-08-11 10:22:29 +00:00
}
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-06-07 08:22:29 +00:00
Assert :: allIsAOf ( $configuration , StaticCallToFuncCall :: class );
2022-02-18 01:46:17 +00:00
$this -> staticCallsToFunctions = $configuration ;
2020-07-29 23:39:41 +00:00
}
2018-08-11 10:22:29 +00:00
}