1616use PhpParser \NodeVisitor ;
1717use PHPStan \Reflection \ClassReflection ;
1818use Rector \NodeAnalyzer \ClassAnalyzer ;
19- use Rector \NodeTypeResolver \ Node \ AttributeKey ;
19+ use Rector \PHPUnit \ Enum \ PHPUnitClassName ;
2020use Rector \PHPUnit \NodeAnalyzer \SetUpMethodDecorator ;
2121use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
2222use Rector \Privatization \NodeManipulator \VisibilityManipulator ;
@@ -105,33 +105,42 @@ public function refactor(Node $node): ?Node
105105 return null ;
106106 }
107107
108- $ constructClassMethod = $ node ->getMethod (MethodName:: CONSTRUCT );
109- if (! $ constructClassMethod instanceof ClassMethod) {
110- return null ;
111- }
108+ foreach ( $ node ->stmts as $ stmtKey => $ classStmt ) {
109+ if (! $ classStmt instanceof ClassMethod) {
110+ continue ;
111+ }
112112
113- if ($ this ->shouldSkip ( $ node , $ constructClassMethod )) {
114- return null ;
115- }
113+ if (! $ this ->isName ( $ classStmt -> name , MethodName:: CONSTRUCT )) {
114+ continue ;
115+ }
116116
117- $ addedStmts = $ this ->resolveStmtsToAddToSetUp ($ constructClassMethod );
118- $ setUpClassMethod = $ node ->getMethod (MethodName::SET_UP );
119-
120- if (! $ setUpClassMethod instanceof ClassMethod) {
121- // no setUp() method yet, rename it to setUp :)
122- $ constructClassMethod ->name = new Identifier (MethodName::SET_UP );
123- $ constructClassMethod ->params = [];
124- $ constructClassMethod ->stmts = $ addedStmts ;
125- $ this ->setUpMethodDecorator ->decorate ($ constructClassMethod );
126- $ this ->visibilityManipulator ->makeProtected ($ constructClassMethod );
127- } else {
128- $ stmtKey = $ constructClassMethod ->getAttribute (AttributeKey::STMT_KEY );
117+ if ($ this ->shouldSkip ($ node , $ classStmt )) {
118+ return null ;
119+ }
120+
121+ $ addedStmts = $ this ->resolveStmtsToAddToSetUp ($ classStmt );
122+ $ setUpClassMethod = $ node ->getMethod (MethodName::SET_UP );
123+
124+ if (! $ setUpClassMethod instanceof ClassMethod) {
125+ // no setUp() method yet, rename it to setUp :)
126+ $ classStmt ->name = new Identifier (MethodName::SET_UP );
127+ $ classStmt ->params = [];
128+ $ classStmt ->stmts = $ addedStmts ;
129+ $ this ->setUpMethodDecorator ->decorate ($ classStmt );
130+ $ this ->visibilityManipulator ->makeProtected ($ classStmt );
131+
132+ return $ node ;
133+ }
134+
135+ // remove constructor and add stmts to already existing setUp() method
129136 unset($ node ->stmts [$ stmtKey ]);
130137
131- $ setUpClassMethod ->stmts = [...(array ) $ setUpClassMethod ->stmts , ...$ addedStmts ];
138+ $ setUpClassMethod ->stmts = array_merge ((array ) $ setUpClassMethod ->stmts , $ addedStmts );
139+
140+ return $ node ;
132141 }
133142
134- return $ node ;
143+ return null ;
135144 }
136145
137146 private function shouldSkip (Class_ $ class , ClassMethod $ classMethod ): bool
@@ -146,7 +155,7 @@ private function shouldSkip(Class_ $class, ClassMethod $classMethod): bool
146155 return true ;
147156 }
148157
149- if ($ currentParent ->getName () !== ' PHPUnit\Framework\TestCase ' ) {
158+ if ($ currentParent ->getName () !== PHPUnitClassName:: TEST_CASE ) {
150159 return true ;
151160 }
152161
0 commit comments