2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2019-09-23 14:36:58 +00:00
namespace Rector\Renaming\Rector\ConstFetch ;
2019-02-19 14:53:31 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\ConstFetch ;
use PhpParser\Node\Name ;
2020-07-29 13:55:33 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2022-01-01 11:02:34 +00:00
use RectorPrefix20220101\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Renaming\Rector\ConstFetch\RenameConstantRector\RenameConstantRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class RenameConstantRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2019-02-19 14:53:31 +00:00
{
/**
2021-12-10 00:25:09 +00:00
* @ deprecated
2020-07-29 13:55:33 +00:00
* @ var string
2019-02-19 14:53:31 +00:00
*/
2021-04-13 16:01:31 +00:00
public const OLD_TO_NEW_CONSTANTS = 'old_to_new_constants' ;
2019-02-19 14:53:31 +00:00
/**
2021-04-13 16:01:31 +00:00
* @ var array < string , string >
2019-02-19 14:53:31 +00:00
*/
2020-07-29 13:55:33 +00:00
private $oldToNewConstants = [];
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-02-19 14:53:31 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Replace constant by new ones' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2019-02-19 14:53:31 +00:00
final class SomeClass
{
public function run ()
{
return MYSQL_ASSOC ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-02-19 14:53:31 +00:00
final class SomeClass
{
public function run ()
{
return MYSQLI_ASSOC ;
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-12-06 21:12:03 +00:00
, [ 'MYSQL_ASSOC' => 'MYSQLI_ASSOC' , 'OLD_CONSTANT' => 'NEW_CONSTANT' ])]);
2019-02-19 14:53:31 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-02-19 14:53:31 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-02-19 14:53:31 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\ConstFetch :: class ];
2019-02-19 14:53:31 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param ConstFetch $node
2019-02-19 14:53:31 +00:00
*/
2021-12-10 10:22:23 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-02-19 14:53:31 +00:00
{
foreach ( $this -> oldToNewConstants as $oldConstant => $newConstant ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node -> name , $oldConstant )) {
2019-02-19 14:53:31 +00:00
continue ;
}
2021-05-10 22:23:08 +00:00
$node -> name = new \PhpParser\Node\Name ( $newConstant );
2021-04-13 16:01:31 +00:00
return $node ;
2019-02-19 14:53:31 +00:00
}
2021-04-13 16:01:31 +00:00
return null ;
2019-02-19 14:53:31 +00:00
}
2021-04-13 16:01:31 +00:00
/**
2021-11-27 13:43:28 +00:00
* @ param mixed [] $configuration
2021-04-13 16:01:31 +00:00
*/
2021-12-10 10:22:23 +00:00
public function configure ( array $configuration ) : void
2020-07-29 13:55:33 +00:00
{
2021-11-28 17:01:20 +00:00
$oldToNewConstants = $configuration [ self :: OLD_TO_NEW_CONSTANTS ] ? ? $configuration ;
2022-01-01 11:02:34 +00:00
\RectorPrefix20220101\Webmozart\Assert\Assert :: allString ( \array_keys ( $oldToNewConstants ));
\RectorPrefix20220101\Webmozart\Assert\Assert :: allString ( $oldToNewConstants );
2021-11-27 13:43:28 +00:00
/** @var array<string, string> $oldToNewConstants */
$this -> oldToNewConstants = $oldToNewConstants ;
2020-07-29 13:55:33 +00:00
}
2019-02-19 14:53:31 +00:00
}