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
|
|
|
|
2019-02-02 20:57:09 +00:00
|
|
|
use Nette\Utils\Strings;
|
2017-10-20 18:29:23 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
2018-10-31 20:43:43 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2019-02-02 20:57:09 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
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
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\Rector\Constant\RenameClassConstantRector\RenameClassConstantRectorTest
|
|
|
|
*/
|
2019-03-09 13:06:16 +00:00
|
|
|
final class RenameClassConstantRector extends AbstractRector
|
2017-10-20 18:29:23 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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 = [];
|
|
|
|
|
|
|
|
/**
|
2018-10-15 02:37:09 +00:00
|
|
|
* @param string[][] $oldToNewConstantsByClass
|
2017-10-20 18:29:23 +00:00
|
|
|
*/
|
2019-04-26 13:32:24 +00:00
|
|
|
public function __construct(array $oldToNewConstantsByClass = [])
|
2018-10-15 02:37:09 +00:00
|
|
|
{
|
2017-10-20 18:29:23 +00:00
|
|
|
$this->oldToNewConstantsByClass = $oldToNewConstantsByClass;
|
|
|
|
}
|
|
|
|
|
2018-04-08 11:51:26 +00:00
|
|
|
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(
|
2019-02-02 20:57:09 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$value = SomeClass::OLD_CONSTANT;
|
|
|
|
$value = SomeClass::OTHER_OLD_CONSTANT;
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$value = SomeClass::NEW_CONSTANT;
|
|
|
|
$value = DifferentClass::NEW_CONSTANT;
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
2019-05-28 16:28:20 +00:00
|
|
|
[
|
|
|
|
'SomeClass' => [
|
|
|
|
'OLD_CONSTANT' => 'NEW_CONSTANT',
|
|
|
|
'OTHER_OLD_CONSTANT' => 'DifferentClass::NEW_CONSTANT',
|
|
|
|
],
|
|
|
|
]
|
2018-08-01 18:34:58 +00:00
|
|
|
),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 04:36:58 +00:00
|
|
|
* @param ClassConstFetch $node
|
2017-10-20 18:29:23 +00:00
|
|
|
*/
|
2018-10-15 04:36:58 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2017-10-20 18:29:23 +00:00
|
|
|
{
|
2018-10-15 02:37:09 +00:00
|
|
|
foreach ($this->oldToNewConstantsByClass as $type => $oldToNewConstants) {
|
2019-09-04 12:10:29 +00:00
|
|
|
if (! $this->isObjectType($node, $type)) {
|
2018-10-15 02:37:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-10 00:28:51 +00:00
|
|
|
|
2018-10-15 02:37:09 +00:00
|
|
|
foreach ($oldToNewConstants as $oldConstant => $newConstant) {
|
2019-08-25 10:37:56 +00:00
|
|
|
if (! $this->isName($node->name, $oldConstant)) {
|
2018-10-15 02:37:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2019-02-02 20:57:09 +00:00
|
|
|
if (Strings::contains($newConstant, '::')) {
|
|
|
|
return $this->createClassConstantFetchNodeFromDoubleColonFormat($newConstant);
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:43:43 +00:00
|
|
|
$node->name = new Identifier($newConstant);
|
|
|
|
|
2018-10-15 04:36:58 +00:00
|
|
|
return $node;
|
2018-10-15 02:37:09 +00:00
|
|
|
}
|
2017-12-25 19:10:47 +00:00
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2018-10-15 04:36:58 +00:00
|
|
|
return $node;
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|
2019-02-02 20:57:09 +00:00
|
|
|
|
|
|
|
private function createClassConstantFetchNodeFromDoubleColonFormat(string $constant): ClassConstFetch
|
|
|
|
{
|
|
|
|
[$constantClass, $constantName] = explode('::', $constant);
|
|
|
|
|
|
|
|
return new ClassConstFetch(new FullyQualified($constantClass), new Identifier($constantName));
|
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|