2017-10-20 18:29:23 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2018-08-01 18:34:58 +00:00
|
|
|
namespace Rector\Rector\Constant;
|
2017-10-20 18:29:23 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
2018-04-28 19:43:35 +00:00
|
|
|
use Rector\Builder\IdentifierRenamer;
|
2017-10-20 18:29:23 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2018-08-01 18:34:58 +00:00
|
|
|
use Rector\RectorDefinition\ConfiguredCodeSample;
|
2018-04-08 11:51:26 +00:00
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
2017-10-20 18:29:23 +00:00
|
|
|
|
|
|
|
final class ClassConstantReplacerRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* class => [
|
|
|
|
* OLD_CONSTANT => NEW_CONSTANT
|
|
|
|
* ]
|
|
|
|
*
|
2018-10-15 02:37:09 +00:00
|
|
|
* @var string[][]
|
2017-10-20 18:29:23 +00:00
|
|
|
*/
|
|
|
|
private $oldToNewConstantsByClass = [];
|
|
|
|
|
2017-12-25 19:10:47 +00:00
|
|
|
/**
|
2017-12-26 21:42:49 +00:00
|
|
|
* @var IdentifierRenamer
|
2017-12-25 19:10:47 +00:00
|
|
|
*/
|
2017-12-26 21:42:49 +00:00
|
|
|
private $identifierRenamer;
|
2017-12-25 19:10:47 +00:00
|
|
|
|
2017-10-20 18:29:23 +00:00
|
|
|
/**
|
2018-10-15 02:37:09 +00:00
|
|
|
* @param string[][] $oldToNewConstantsByClass
|
2017-10-20 18:29:23 +00:00
|
|
|
*/
|
2018-10-15 02:37:09 +00:00
|
|
|
public function __construct(array $oldToNewConstantsByClass, IdentifierRenamer $identifierRenamer)
|
|
|
|
{
|
2017-10-20 18:29:23 +00:00
|
|
|
$this->oldToNewConstantsByClass = $oldToNewConstantsByClass;
|
2017-12-26 21:42:49 +00:00
|
|
|
$this->identifierRenamer = $identifierRenamer;
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 11:51:26 +00:00
|
|
|
/**
|
|
|
|
* @todo complete list with all possibilities
|
|
|
|
*/
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
2018-08-01 13:34:55 +00:00
|
|
|
return new RectorDefinition('Replaces defined class constants in their calls.', [
|
2018-08-01 18:34:58 +00:00
|
|
|
new ConfiguredCodeSample(
|
|
|
|
'$value = SomeClass::OLD_CONSTANT;',
|
|
|
|
'$value = SomeClass::NEW_CONSTANT;',
|
|
|
|
[
|
|
|
|
'$oldToNewConstantsByClass' => [
|
|
|
|
'SomeClass' => [
|
|
|
|
'OLD_CONSTANT' => 'NEW_CONSTANT',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
),
|
2018-04-08 11:51:26 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
2017-10-20 18:29:23 +00:00
|
|
|
{
|
2018-08-14 22:12:41 +00:00
|
|
|
return [ClassConstFetch::class];
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassConstFetch $classConstFetchNode
|
|
|
|
*/
|
|
|
|
public function refactor(Node $classConstFetchNode): ?Node
|
|
|
|
{
|
2018-10-15 02:37:09 +00:00
|
|
|
$currentConstantName = (string) $classConstFetchNode->name;
|
2017-12-10 00:28:51 +00:00
|
|
|
|
2018-10-15 02:37:09 +00:00
|
|
|
foreach ($this->oldToNewConstantsByClass as $type => $oldToNewConstants) {
|
|
|
|
if (! $this->isType($classConstFetchNode, $type)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-10 00:28:51 +00:00
|
|
|
|
2018-10-15 02:37:09 +00:00
|
|
|
foreach ($oldToNewConstants as $oldConstant => $newConstant) {
|
|
|
|
if ($currentConstantName !== $oldConstant) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2018-10-15 02:37:09 +00:00
|
|
|
$this->identifierRenamer->renameNode($classConstFetchNode, $newConstant);
|
|
|
|
return $classConstFetchNode;
|
|
|
|
}
|
2017-12-25 19:10:47 +00:00
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2017-12-25 19:37:09 +00:00
|
|
|
return $classConstFetchNode;
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|
|
|
|
}
|