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 @@ +