2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2020-08-18 15:57:30 +00:00
|
|
|
namespace Rector\Renaming\Rector\ClassConstFetch;
|
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;
|
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;
|
|
|
|
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2020-09-12 21:19:08 +00:00
|
|
|
use Rector\Renaming\ValueObject\RenameClassConstant;
|
2020-08-26 10:54:53 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2020-08-18 15:57:30 +00:00
|
|
|
* @see \Rector\Renaming\Tests\Rector\ClassConstFetch\RenameClassConstantRector\RenameClassConstantRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2020-07-29 13:55:33 +00:00
|
|
|
final class RenameClassConstantRector extends AbstractRector implements ConfigurableRectorInterface
|
2017-10-20 18:29:23 +00:00
|
|
|
{
|
2020-07-29 13:55:33 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-08-26 10:54:53 +00:00
|
|
|
public const CLASS_CONSTANT_RENAME = 'constant_rename';
|
2020-07-29 13:55:33 +00:00
|
|
|
|
2017-10-20 18:29:23 +00:00
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var RenameClassConstant[]
|
2017-10-20 18:29:23 +00:00
|
|
|
*/
|
2020-08-26 10:54:53 +00:00
|
|
|
private $classConstantRenames = [];
|
2017-10-20 18:29:23 +00:00
|
|
|
|
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-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-02-02 20:57:09 +00:00
|
|
|
$value = SomeClass::OLD_CONSTANT;
|
|
|
|
$value = SomeClass::OTHER_OLD_CONSTANT;
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-02-02 20:57:09 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-02-02 20:57:09 +00:00
|
|
|
$value = SomeClass::NEW_CONSTANT;
|
|
|
|
$value = DifferentClass::NEW_CONSTANT;
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-02-02 20:57:09 +00:00
|
|
|
,
|
2019-05-28 16:28:20 +00:00
|
|
|
[
|
2020-08-26 10:54:53 +00:00
|
|
|
self::CLASS_CONSTANT_RENAME => [
|
2020-09-12 21:19:08 +00:00
|
|
|
new RenameClassConstant('SomeClass', 'OLD_CONSTANT', 'NEW_CONSTANT'),
|
|
|
|
new RenameClassConstant('SomeClass', 'OTHER_OLD_CONSTANT', 'DifferentClass::NEW_CONSTANT'),
|
2019-05-28 16:28:20 +00:00
|
|
|
],
|
|
|
|
]
|
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
|
|
|
{
|
2020-08-26 10:54:53 +00:00
|
|
|
foreach ($this->classConstantRenames as $classConstantRename) {
|
|
|
|
if (! $this->isObjectType($node, $classConstantRename->getOldClass())) {
|
2018-10-15 02:37:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-10 00:28:51 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
if (! $this->isName($node->name, $classConstantRename->getOldConstant())) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
if (Strings::contains($classConstantRename->getNewConstant(), '::')) {
|
|
|
|
return $this->createClassConstantFetchNodeFromDoubleColonFormat($classConstantRename->getNewConstant());
|
|
|
|
}
|
2019-02-02 20:57:09 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
$node->name = new Identifier($classConstantRename->getNewConstant());
|
2018-10-31 20:43:43 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
return $node;
|
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
|
|
|
|
2020-07-29 13:55:33 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $configuration
|
|
|
|
*/
|
|
|
|
public function configure(array $configuration): void
|
|
|
|
{
|
2020-08-26 10:54:53 +00:00
|
|
|
$classConstantRenames = $configuration[self::CLASS_CONSTANT_RENAME] ?? [];
|
2020-09-12 21:19:08 +00:00
|
|
|
Assert::allIsInstanceOf($classConstantRenames, RenameClassConstant::class);
|
2020-08-26 10:54:53 +00:00
|
|
|
$this->classConstantRenames = $classConstantRenames;
|
2020-07-29 13:55:33 +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
|
|
|
}
|