From 2ffb04a229a893f5c201ee7dcad7e10bfa2cb175 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 5 Oct 2018 23:34:25 +0800 Subject: [PATCH] [PHP] Add ReservedObjectRector --- config/level/php/php71.yml | 3 + .../src/Rector/Name/ReservedObjectRector.php | 110 ++++++++++++++++++ .../Correct/correct.php.inc | 28 +++++ .../ReservedObjectRectorTest.php | 31 +++++ .../Wrong/ReservedObject.php | 28 +++++ .../Name/ReservedObjectRector/config.yml | 5 + 6 files changed, 205 insertions(+) create mode 100644 packages/Php/src/Rector/Name/ReservedObjectRector.php create mode 100644 packages/Php/tests/Rector/Name/ReservedObjectRector/Correct/correct.php.inc create mode 100644 packages/Php/tests/Rector/Name/ReservedObjectRector/ReservedObjectRectorTest.php create mode 100644 packages/Php/tests/Rector/Name/ReservedObjectRector/Wrong/ReservedObject.php create mode 100644 packages/Php/tests/Rector/Name/ReservedObjectRector/config.yml diff --git a/config/level/php/php71.yml b/config/level/php/php71.yml index d5721c8d625..9db33e8700a 100644 --- a/config/level/php/php71.yml +++ b/config/level/php/php71.yml @@ -1,2 +1,5 @@ services: Rector\Php\Rector\BinaryOp\IsIterableRector: ~ + Rector\Php\Rector\Name\ReservedObjectRector: + $reservedKeywordsToReplacements: + Object: 'BaseObject' diff --git a/packages/Php/src/Rector/Name/ReservedObjectRector.php b/packages/Php/src/Rector/Name/ReservedObjectRector.php new file mode 100644 index 00000000000..fa08c480dfe --- /dev/null +++ b/packages/Php/src/Rector/Name/ReservedObjectRector.php @@ -0,0 +1,110 @@ +reservedKeywordsToReplacements = $reservedKeywordsToReplacements; + } + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition( + 'Changes reserved "Object" name to "Object" where can be configured', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class Object +{ +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SmartObject +{ +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [Identifier::class, Name::class]; + } + + /** + * @param Identifier|Name $node + */ + public function refactor(Node $node): ?Node + { + if ($node instanceof Identifier) { + return $this->processIdentifier($node); + } + + if ($node instanceof Name) { + return $this->processName($node); + } + + return $node; + } + + private function processIdentifier(Identifier $identifierNode): Identifier + { + foreach ($this->reservedKeywordsToReplacements as $reservedKeyword => $replacement) { + if (strtolower($identifierNode->name) === strtolower($reservedKeyword)) { + $identifierNode->name = $replacement; + } + } + + return $identifierNode; + } + + private function processName(Name $nameNode): Name + { + // we look for "extends " + $parentNode = $nameNode->getAttribute(Attribute::PARENT_NODE); + // "Object" can part of namespace name + if ($parentNode instanceof Namespace_) { + return $nameNode; + } + + // process lass part + foreach ($this->reservedKeywordsToReplacements as $reservedKeyword => $replacement) { + if (strtolower($nameNode->getLast()) === strtolower($reservedKeyword)) { + $nameNode->parts[count($nameNode->parts) - 1] = $replacement; + $nameNode->setAttribute(Attribute::ORIGINAL_NODE, null); + } + } + + return $nameNode; + } +} diff --git a/packages/Php/tests/Rector/Name/ReservedObjectRector/Correct/correct.php.inc b/packages/Php/tests/Rector/Name/ReservedObjectRector/Correct/correct.php.inc new file mode 100644 index 00000000000..2986ce9a860 --- /dev/null +++ b/packages/Php/tests/Rector/Name/ReservedObjectRector/Correct/correct.php.inc @@ -0,0 +1,28 @@ +doTestFileMatchesExpectedContent($wrong, $fixed); + } + + public function provideWrongToFixedFiles(): Iterator + { + // it needs to have different name, since in PHP 7.1 it already reserved + yield [__DIR__ . '/Wrong/ReservedObject.php', __DIR__ . '/Correct/correct.php.inc']; + } + + protected function provideConfig(): string + { + return __DIR__ . '/config.yml'; + } +} diff --git a/packages/Php/tests/Rector/Name/ReservedObjectRector/Wrong/ReservedObject.php b/packages/Php/tests/Rector/Name/ReservedObjectRector/Wrong/ReservedObject.php new file mode 100644 index 00000000000..c9356c85509 --- /dev/null +++ b/packages/Php/tests/Rector/Name/ReservedObjectRector/Wrong/ReservedObject.php @@ -0,0 +1,28 @@ +