From d55c97be88b6eec5f05825d5336b347d796e3aa4 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sat, 18 Nov 2017 03:26:25 +0100 Subject: [PATCH] [PseudoNamespaceToNamespaceRector] fix class rename --- .../Dynamic/PseudoNamespaceToNamespaceRector.php | 12 ++++++------ .../PseudoNamespaceToNamespaceRector/Test.php | 6 ++++-- .../correct/correct4.php.inc | 13 +++++++++++++ .../wrong/wrong4.php.inc | 13 +++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/correct/correct4.php.inc create mode 100644 tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/wrong/wrong4.php.inc diff --git a/src/Rector/Dynamic/PseudoNamespaceToNamespaceRector.php b/src/Rector/Dynamic/PseudoNamespaceToNamespaceRector.php index b06cc783a11..d351d9aad3b 100644 --- a/src/Rector/Dynamic/PseudoNamespaceToNamespaceRector.php +++ b/src/Rector/Dynamic/PseudoNamespaceToNamespaceRector.php @@ -13,11 +13,6 @@ use Rector\Node\Attribute; use Rector\Node\NodeFactory; use Rector\Rector\AbstractRector; -/** - * Basically inversion of https://github.com/nikic/PHP-Parser/blob/master/doc/2_Usage_of_basic_components.markdown#example-converting-namespaced-code-to-pseudo-namespaces - * - * Requested on SO: https://stackoverflow.com/questions/29014957/converting-pseudo-namespaced-classes-to-use-real-namespace - */ final class PseudoNamespaceToNamespaceRector extends AbstractRector { /** @@ -81,6 +76,11 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector $parentNode = $nameOrIdentifierNode->getAttribute(Attribute::PARENT_NODE); $lastNewNamePart = $newNameParts[count($newNameParts) - 1]; + // do not rename classes + if ($parentNode instanceof Class_) { + return null; + } + if ($nameOrIdentifierNode instanceof Name) { if ($parentNode instanceof UseUse) { $this->oldToNewUseStatements[$oldName] = $lastNewNamePart; @@ -93,7 +93,7 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector return $nameOrIdentifierNode; } - if ($nameOrIdentifierNode instanceof Identifier && $parentNode instanceof Class_) { + if ($nameOrIdentifierNode instanceof Identifier) { $namespaceParts = $newNameParts; array_pop($namespaceParts); diff --git a/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/Test.php b/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/Test.php index fb4838e1433..9eef16b394d 100644 --- a/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/Test.php +++ b/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/Test.php @@ -13,16 +13,18 @@ final class Test extends AbstractConfigurableRectorTestCase __DIR__ . '/wrong/wrong.php.inc', __DIR__ . '/correct/correct.php.inc' ); - $this->doTestFileMatchesExpectedContent( __DIR__ . '/wrong/wrong2.php.inc', __DIR__ . '/correct/correct2.php.inc' ); - $this->doTestFileMatchesExpectedContent( __DIR__ . '/wrong/wrong3.php.inc', __DIR__ . '/correct/correct3.php.inc' ); + $this->doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong4.php.inc', + __DIR__ . '/correct/correct4.php.inc' + ); } protected function provideConfig(): string diff --git a/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/correct/correct4.php.inc b/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/correct/correct4.php.inc new file mode 100644 index 00000000000..79bb1e937cf --- /dev/null +++ b/tests/Rector/Dynamic/PseudoNamespaceToNamespaceRector/correct/correct4.php.inc @@ -0,0 +1,13 @@ +