diff --git a/src/Application/FileProcessor.php b/src/Application/FileProcessor.php index bffc03fda1a..4b867050555 100644 --- a/src/Application/FileProcessor.php +++ b/src/Application/FileProcessor.php @@ -18,7 +18,7 @@ final class FileProcessor /** * @var FormatPerservingPrinter */ - private $codeStyledPrinter; + private $formatPerservingPrinter; /** * @var NodeTraverser @@ -37,7 +37,7 @@ final class FileProcessor NodeTraverser $nodeTraverser ) { $this->parser = $parser; - $this->codeStyledPrinter = $codeStyledPrinter; + $this->formatPerservingPrinter = $codeStyledPrinter; $this->nodeTraverser = $nodeTraverser; $this->lexer = $lexer; } @@ -66,7 +66,7 @@ final class FileProcessor $newStmts = $this->nodeTraverser->traverse($oldStmts); - $this->codeStyledPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens); + $this->formatPerservingPrinter->printToFile($file, $newStmts, $oldStmts, $oldTokens); } /** diff --git a/src/Builder/StatementGlue.php b/src/Builder/StatementGlue.php index 56979223555..e3d6dc067a0 100644 --- a/src/Builder/StatementGlue.php +++ b/src/Builder/StatementGlue.php @@ -38,17 +38,24 @@ final class StatementGlue $classNode->stmts[] = $node; } - public function addAsFirstTrait(Class_ $classNode, TraitUse $traitUse): void + public function addAsFirstTrait(Class_ $classNode, Node $node): void { - foreach ($classNode->stmts as $key => $classElementNode) { - if ($classElementNode instanceof TraitUse) { - $this->insertBefore($classNode, $traitUse, $key); + $this->addStatementToClassBeforeTypes($classNode, $node, TraitUse::class, Property::class); + } - return; + private function addStatementToClassBeforeTypes(Class_ $classNode, Node $node, string ...$types): void + { + foreach ($types as $type) { + foreach ($classNode->stmts as $key => $classElementNode) { + if (is_a($classElementNode, $type, true)) { + $this->insertBefore($classNode, $node, $key); + + return; + } } } - $classNode->stmts[] = $traitUse; + $classNode->stmts[] = $node; } /** diff --git a/src/NodeVisitor/UpgradeDeprecation/AbstraceReplaceDeprecatedConstantNodeVisitor.php b/src/NodeVisitor/UpgradeDeprecation/AbstraceReplaceDeprecatedConstantNodeVisitor.php index 013a6ba0a1d..e2182e9b7d3 100644 --- a/src/NodeVisitor/UpgradeDeprecation/AbstraceReplaceDeprecatedConstantNodeVisitor.php +++ b/src/NodeVisitor/UpgradeDeprecation/AbstraceReplaceDeprecatedConstantNodeVisitor.php @@ -15,19 +15,6 @@ abstract class AbstraceReplaceDeprecatedConstantNodeVisitor extends NodeVisitorA abstract public function getNewConstantName(): string; - /** - * Return value semantics: - * * null - * => $node stays as-is - * * NodeTraverser::DONT_TRAVERSE_CHILDREN - * => Children of $node are not traversed. $node stays as-is - * * NodeTraverser::STOP_TRAVERSAL - * => Traversal is aborted. $node stays as-is - * * otherwise - * => $node is set to the return value. - * - * @return null|int|Node - */ public function enterNode(Node $node): ?int { if ($this->isCandidate($node)) { diff --git a/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php index 1fbe121cd97..840aeeee6ff 100644 --- a/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php +++ b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/Test.php @@ -8,13 +8,17 @@ final class Test extends AbstractReconstructorTestCase { public function test(): void { - // $this->doTestFileMatchesExpectedContent( - // __DIR__ . '/wrong/wrong.php.inc', - // __DIR__ . '/correct/correct.php.inc' - // ); + $this->doTestFileMatchesExpectedContent( + __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' + ); } } diff --git a/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct3.php.inc b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct3.php.inc new file mode 100644 index 00000000000..3b9cd459f21 --- /dev/null +++ b/tests/NodeVisitor/UpgradeDeprecation/DeprecatedParentClassToTraitNodeVisitor/correct/correct3.php.inc @@ -0,0 +1,7 @@ +