From 58f638d104ddb94bb5a23dfd7730ab41d3c8a81a Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 6 Aug 2017 19:49:19 +0200 Subject: [PATCH] [NodeTraverser] add ParentClassToTrait refactor --- ...eprecatedParentClassToTraitNodeVisitor.php | 65 +++++++++++++++++++ .../Test.php | 16 +++++ .../correct/correct.php.inc | 6 ++ .../correct/correct2.php.inc | 7 ++ .../wrong/wrong.php.inc | 5 ++ .../wrong/wrong2.php.inc | 6 ++ 6 files changed, 105 insertions(+) create mode 100644 src/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor.php create mode 100644 tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php create mode 100644 tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct.php.inc create mode 100644 tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct2.php.inc create mode 100644 tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/wrong/wrong.php.inc create mode 100644 tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/wrong/wrong2.php.inc diff --git a/src/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor.php b/src/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor.php new file mode 100644 index 00000000000..5664082e8eb --- /dev/null +++ b/src/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor.php @@ -0,0 +1,65 @@ +isCandidate($node)) { + $this->refactor($node); + return NodeTraverser::DONT_TRAVERSE_CHILDREN; + } + + return null; + } + + private function isCandidate(Node $node): bool + { + if ($node instanceof Node\Stmt\Class_) { + if (! $node->extends) { + return false; + } + + $parentClassName = (string) $node->extends; + if ($parentClassName !== $this->getParentClassName()) { + return false; + } + + return true; + } + + return false; + } + + private function refactor(Node\Stmt\Class_ $classNode) + { + // remove parent class + $classNode->extends = null; + + // add new trait + $nameParts = explode('\\', $this->getTraitName()); + $classNode->stmts[] = new TraitUse([ + new FullyQualified($nameParts) + ]); + } +} diff --git a/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php new file mode 100644 index 00000000000..a7a8f29ac9a --- /dev/null +++ b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php @@ -0,0 +1,16 @@ +doTestFileMatchesExpectedContent( + __DIR__ . '/wrong/wrong.php.inc', + __DIR__ . '/correct/correct.php.inc' + ); + } +} diff --git a/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct.php.inc b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct.php.inc new file mode 100644 index 00000000000..10e922d412f --- /dev/null +++ b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct.php.inc @@ -0,0 +1,6 @@ +