1414use PhpParser \Node \Stmt \Expression ;
1515use PhpParser \Node \Stmt \Nop ;
1616use PhpParser \NodeVisitor ;
17+ use PHPStan \Analyser \Scope ;
1718use PHPStan \PhpDocParser \Ast \PhpDoc \GenericTagValueNode ;
1819use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
1920use PHPStan \Reflection \ClassReflection ;
2021use PHPStan \Reflection \ReflectionProvider ;
2122use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
2223use Rector \Comments \NodeDocBlock \DocBlockUpdater ;
2324use Rector \Php80 \NodeAnalyzer \PhpAttributeAnalyzer ;
24- use Rector \PhpParser \ Node \ BetterNodeFinder ;
25+ use Rector \PHPStan \ ScopeFetcher ;
2526use Rector \PHPUnit \Enum \PHPUnitAttribute ;
2627use Rector \PHPUnit \Enum \PHPUnitClassName ;
2728use Rector \PHPUnit \NodeAnalyzer \AssertCallAnalyzer ;
2829use Rector \PHPUnit \NodeAnalyzer \LifecycleMethodAssertAnalyzer ;
2930use Rector \PHPUnit \NodeAnalyzer \MockedVariableAnalyzer ;
3031use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
3132use Rector \Rector \AbstractRector ;
32- use Rector \Reflection \ReflectionResolver ;
3333use Rector \RuleDoc \CodeSample \CodeSample ;
3434use Rector \RuleDoc \RuleDefinition ;
3535use Rector \VersionBonding \Contract \ComposerPackageConstraintInterface ;
@@ -58,10 +58,8 @@ public function __construct(
5858 private readonly PhpAttributeAnalyzer $ phpAttributeAnalyzer ,
5959 private readonly DocBlockUpdater $ docBlockUpdater ,
6060 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
61- private readonly ReflectionResolver $ reflectionResolver ,
6261 private readonly ReflectionProvider $ reflectionProvider ,
6362 private readonly LifecycleMethodAssertAnalyzer $ lifecycleMethodAssertAnalyzer ,
64- private readonly BetterNodeFinder $ betterNodeFinder ,
6563 ) {
6664 }
6765
@@ -105,43 +103,74 @@ public function test()
105103 */
106104 public function getNodeTypes (): array
107105 {
108- return [ClassMethod ::class];
106+ return [Class_ ::class];
109107 }
110108
111109 /**
112- * @param ClassMethod $node
110+ * @param Class_ $node
113111 */
114112 public function refactor (Node $ node ): ?Node
115113 {
116- if ($ this ->shouldSkipClassMethod ($ node )) {
114+ if (! $ this ->testsNodeAnalyzer -> isInTestClass ($ node )) {
117115 return null ;
118116 }
119117
120- $ this -> removeAddToAssertionCountCalls ($ node );
118+ $ scope = ScopeFetcher :: fetch ($ node );
121119
120+ // a child class can add asserting lifecycle methods, that count toward the inherited tests
121+ if ($ node ->isAbstract ()) {
122+ return null ;
123+ }
124+
125+ // the parent test case asserts in its own integration methods
126+ if ($ this ->isInTwigIntegrationTestCase ($ scope )) {
127+ return null ;
128+ }
129+
130+ // assertions in lifecycle methods count toward every test of the class
131+ if ($ this ->lifecycleMethodAssertAnalyzer ->hasAssertingLifecycleMethod ($ node )) {
132+ return null ;
133+ }
134+
135+ $ hasChanged = false ;
136+
137+ foreach ($ node ->getMethods () as $ classMethod ) {
138+ if ($ this ->shouldSkipClassMethod ($ classMethod )) {
139+ continue ;
140+ }
141+
142+ $ this ->removeAddToAssertionCountCalls ($ classMethod );
143+ $ this ->addDoesNotPerformAssertions ($ classMethod );
144+
145+ $ hasChanged = true ;
146+ }
147+
148+ if (! $ hasChanged ) {
149+ return null ;
150+ }
151+
152+ return $ node ;
153+ }
154+
155+ private function addDoesNotPerformAssertions (ClassMethod $ classMethod ): void
156+ {
122157 // the attribute is available since PHPUnit 10, prefer it over the annotation
123158 if ($ this ->reflectionProvider ->hasClass (PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS )) {
124- $ node ->attrGroups [] = new AttributeGroup ([
159+ $ classMethod ->attrGroups [] = new AttributeGroup ([
125160 new Attribute (new FullyQualified (PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS )),
126161 ]);
127162
128- return $ node ;
163+ return ;
129164 }
130165
131- $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ node );
166+ $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ classMethod );
132167 $ phpDocInfo ->addPhpDocTagNode (new PhpDocTagNode ('@doesNotPerformAssertions ' , new GenericTagValueNode ('' )));
133168
134- $ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ node );
135-
136- return $ node ;
169+ $ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ classMethod );
137170 }
138171
139172 private function shouldSkipClassMethod (ClassMethod $ classMethod ): bool
140173 {
141- if (! $ this ->testsNodeAnalyzer ->isInTestClass ($ classMethod )) {
142- return true ;
143- }
144-
145174 if (! $ this ->testsNodeAnalyzer ->isTestClassMethod ($ classMethod )) {
146175 return true ;
147176 }
@@ -150,21 +179,6 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
150179 return true ;
151180 }
152181
153- // we have no idea how the trait is used, the using class can assert on its own
154- if ($ this ->isInTrait ($ classMethod )) {
155- return true ;
156- }
157-
158- // a child class can add asserting lifecycle methods, that count toward the inherited tests
159- if ($ this ->isInAbstractClass ($ classMethod )) {
160- return true ;
161- }
162-
163- // the parent test case asserts in its own integration methods
164- if ($ this ->isInTwigIntegrationTestCase ($ classMethod )) {
165- return true ;
166- }
167-
168182 if ($ this ->hasAssertingAnnotationOrAttribute ($ classMethod )) {
169183 return true ;
170184 }
@@ -174,12 +188,6 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
174188 return true ;
175189 }
176190
177- // assertions in lifecycle methods count toward every test of the class
178- $ class = $ this ->resolveCurrentClass ($ classMethod );
179- if ($ class instanceof Class_ && $ this ->lifecycleMethodAssertAnalyzer ->hasAssertingLifecycleMethod ($ class )) {
180- return true ;
181- }
182-
183191 return $ this ->mockedVariableAnalyzer ->containsMockAsUsedVariable ($ classMethod );
184192 }
185193
@@ -226,40 +234,9 @@ private function isAddToAssertionCountExpression(Node $node): bool
226234 return $ this ->isName ($ methodCall ->name , 'addToAssertionCount ' );
227235 }
228236
229- private function isInTrait (ClassMethod $ classMethod ): bool
230- {
231- $ classReflection = $ this ->reflectionResolver ->resolveClassReflection ($ classMethod );
232- if (! $ classReflection instanceof ClassReflection) {
233- return false ;
234- }
235-
236- return $ classReflection ->isTrait ();
237- }
238-
239- private function isInAbstractClass (ClassMethod $ classMethod ): bool
240- {
241- $ classReflection = $ this ->reflectionResolver ->resolveClassReflection ($ classMethod );
242- if (! $ classReflection instanceof ClassReflection) {
243- return false ;
244- }
245-
246- return $ classReflection ->isAbstract ();
247- }
248-
249- private function resolveCurrentClass (ClassMethod $ classMethod ): ?Class_
250- {
251- $ class = $ this ->betterNodeFinder ->findFirst (
252- $ this ->getFile ()
253- ->getNewStmts (),
254- fn (Node $ node ): bool => $ node instanceof Class_ && in_array ($ classMethod , $ node ->getMethods (), true )
255- );
256-
257- return $ class instanceof Class_ ? $ class : null ;
258- }
259-
260- private function isInTwigIntegrationTestCase (ClassMethod $ classMethod ): bool
237+ private function isInTwigIntegrationTestCase (Scope $ scope ): bool
261238 {
262- $ classReflection = $ this -> reflectionResolver -> resolveClassReflection ( $ classMethod );
239+ $ classReflection = $ scope -> getClassReflection ( );
263240 if (! $ classReflection instanceof ClassReflection) {
264241 return false ;
265242 }
0 commit comments