rector/rules/renaming/src/Rector/ClassConstFetch/RenameClassConstFetchRector.php

116 lines
3.5 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2017-10-20 18:29:23 +00:00
namespace Rector\Renaming\Rector\ClassConstFetch;
2017-10-20 18:29:23 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
2020-07-29 13:55:33 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Renaming\Contract\RenameClassConstFetchInterface;
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
use Rector\Renaming\ValueObject\RenameClassConstFetch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
2017-10-20 18:29:23 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Renaming\Tests\Rector\ClassConstFetch\RenameClassConstFetchRector\RenameClassConstFetchRectorTest
2019-09-03 09:11:45 +00:00
*/
final class RenameClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
2017-10-20 18:29:23 +00:00
{
2020-07-29 13:55:33 +00:00
/**
* @var string
*/
public const CLASS_CONSTANT_RENAME = 'constant_rename';
2020-07-29 13:55:33 +00:00
2017-10-20 18:29:23 +00:00
/**
* @var RenameClassConstFetchInterface[]
2017-10-20 18:29:23 +00:00
*/
private $renameClassConstFetches = [];
2017-10-20 18:29:23 +00:00
public function getRuleDefinition(): RuleDefinition
2018-04-08 11:51:26 +00:00
{
$configuration = [
self::CLASS_CONSTANT_RENAME => [
new RenameClassConstFetch('SomeClass', 'OLD_CONSTANT', 'NEW_CONSTANT'),
new RenameClassAndConstFetch('SomeClass', 'OTHER_OLD_CONSTANT', 'DifferentClass', 'NEW_CONSTANT'),
],
];
return new RuleDefinition(
'Replaces defined class constants in their calls.',
[
new ConfiguredCodeSample(
<<<'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
,
$configuration
2021-03-03 08:49:57 +00:00
),
]);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
* @return array<class-string<Node>>
2018-08-14 22:12:41 +00:00
*/
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 $node
2017-10-20 18:29:23 +00:00
*/
public function refactor(Node $node): ?Node
2017-10-20 18:29:23 +00:00
{
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
foreach ($this->renameClassConstFetches as $renameClassConstFetch) {
if (! $this->isObjectType($node->class, $renameClassConstFetch->getOldObjectType())) {
2018-10-15 02:37:09 +00:00
continue;
}
2017-12-10 00:28:51 +00:00
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
if (! $this->isName($node->name, $renameClassConstFetch->getOldConstant())) {
continue;
}
2017-10-20 18:29:23 +00:00
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
if ($renameClassConstFetch instanceof RenameClassAndConstFetch) {
return $this->createClassAndConstFetch($renameClassConstFetch);
}
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
$node->name = new Identifier($renameClassConstFetch->getNewConstant());
return $node;
}
2017-10-20 18:29:23 +00:00
return $node;
2017-10-20 18:29:23 +00:00
}
2020-07-29 13:55:33 +00:00
/**
* @param array<string, RenameClassConstFetchInterface[]> $configuration
2020-07-29 13:55:33 +00:00
*/
public function configure(array $configuration): void
{
$renameClassConstFetches = $configuration[self::CLASS_CONSTANT_RENAME] ?? [];
Assert::allIsInstanceOf($renameClassConstFetches, RenameClassConstFetchInterface::class);
$this->renameClassConstFetches = $renameClassConstFetches;
2020-07-29 13:55:33 +00:00
}
private function createClassAndConstFetch(RenameClassAndConstFetch $renameClassAndConstFetch): ClassConstFetch
{
return new ClassConstFetch(
new FullyQualified($renameClassAndConstFetch->getNewClass()),
new Identifier($renameClassAndConstFetch->getNewConstant())
);
}
2017-10-20 18:29:23 +00:00
}