99use PhpParser \Node \AttributeGroup ;
1010use PhpParser \Node \Expr \MethodCall ;
1111use PhpParser \Node \Name \FullyQualified ;
12+ use PhpParser \Node \Stmt \Class_ ;
1213use PhpParser \Node \Stmt \ClassMethod ;
1314use PhpParser \Node \Stmt \Expression ;
1415use PhpParser \Node \Stmt \Nop ;
1516use PhpParser \NodeVisitor ;
17+ use PHPStan \Analyser \Scope ;
1618use PHPStan \PhpDocParser \Ast \PhpDoc \GenericTagValueNode ;
1719use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
1820use PHPStan \Reflection \ClassReflection ;
1921use PHPStan \Reflection \ReflectionProvider ;
2022use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
2123use Rector \Comments \NodeDocBlock \DocBlockUpdater ;
2224use Rector \Php80 \NodeAnalyzer \PhpAttributeAnalyzer ;
25+ use Rector \PHPStan \ScopeFetcher ;
2326use Rector \PHPUnit \Enum \PHPUnitAttribute ;
2427use Rector \PHPUnit \Enum \PHPUnitClassName ;
2528use Rector \PHPUnit \NodeAnalyzer \AssertCallAnalyzer ;
2629use Rector \PHPUnit \NodeAnalyzer \MockedVariableAnalyzer ;
2730use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
2831use Rector \Rector \AbstractRector ;
29- use Rector \Reflection \ReflectionResolver ;
3032use Rector \RuleDoc \CodeSample \CodeSample ;
3133use Rector \RuleDoc \RuleDefinition ;
3234use Rector \VersionBonding \Contract \ComposerPackageConstraintInterface ;
@@ -55,7 +57,6 @@ public function __construct(
5557 private readonly PhpAttributeAnalyzer $ phpAttributeAnalyzer ,
5658 private readonly DocBlockUpdater $ docBlockUpdater ,
5759 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
58- private readonly ReflectionResolver $ reflectionResolver ,
5960 private readonly ReflectionProvider $ reflectionProvider ,
6061 ) {
6162 }
@@ -100,43 +101,64 @@ public function test()
100101 */
101102 public function getNodeTypes (): array
102103 {
103- return [ClassMethod ::class];
104+ return [Class_ ::class];
104105 }
105106
106107 /**
107- * @param ClassMethod $node
108+ * @param Class_ $node
108109 */
109110 public function refactor (Node $ node ): ?Node
110111 {
111- if ($ this ->shouldSkipClassMethod ($ node )) {
112+ if (! $ this ->testsNodeAnalyzer -> isInTestClass ($ node )) {
112113 return null ;
113114 }
114115
115- $ this -> removeAddToAssertionCountCalls ($ node );
116+ $ scope = ScopeFetcher :: fetch ($ node );
116117
118+ // the parent test case asserts in its own integration methods
119+ if ($ this ->isInTwigIntegrationTestCase ($ scope )) {
120+ return null ;
121+ }
122+
123+ $ hasChanged = false ;
124+
125+ foreach ($ node ->getMethods () as $ classMethod ) {
126+ if ($ this ->shouldSkipClassMethod ($ classMethod )) {
127+ continue ;
128+ }
129+
130+ $ this ->removeAddToAssertionCountCalls ($ classMethod );
131+ $ this ->addDoesNotPerformAssertions ($ classMethod );
132+
133+ $ hasChanged = true ;
134+ }
135+
136+ if (! $ hasChanged ) {
137+ return null ;
138+ }
139+
140+ return $ node ;
141+ }
142+
143+ private function addDoesNotPerformAssertions (ClassMethod $ classMethod ): void
144+ {
117145 // the attribute is available since PHPUnit 10, prefer it over the annotation
118146 if ($ this ->reflectionProvider ->hasClass (PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS )) {
119- $ node ->attrGroups [] = new AttributeGroup ([
147+ $ classMethod ->attrGroups [] = new AttributeGroup ([
120148 new Attribute (new FullyQualified (PHPUnitAttribute::DOES_NOT_PERFORM_ASSERTIONS )),
121149 ]);
122150
123- return $ node ;
151+ return ;
124152 }
125153
126- $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ node );
154+ $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ classMethod );
127155 $ phpDocInfo ->addPhpDocTagNode (new PhpDocTagNode ('@doesNotPerformAssertions ' , new GenericTagValueNode ('' )));
128156
129- $ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ node );
130-
131- return $ node ;
157+ $ this ->docBlockUpdater ->updateRefactoredNodeWithPhpDocInfo ($ classMethod );
132158 }
133159
134160 private function shouldSkipClassMethod (ClassMethod $ classMethod ): bool
135161 {
136- if (! $ this ->testsNodeAnalyzer ->isInTestClass ($ classMethod )) {
137- return true ;
138- }
139-
140162 if (! $ this ->testsNodeAnalyzer ->isTestClassMethod ($ classMethod )) {
141163 return true ;
142164 }
@@ -145,16 +167,6 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
145167 return true ;
146168 }
147169
148- // we have no idea how the trait is used, the using class can assert on its own
149- if ($ this ->isInTrait ($ classMethod )) {
150- return true ;
151- }
152-
153- // the parent test case asserts in its own integration methods
154- if ($ this ->isInTwigIntegrationTestCase ($ classMethod )) {
155- return true ;
156- }
157-
158170 if ($ this ->hasAssertingAnnotationOrAttribute ($ classMethod )) {
159171 return true ;
160172 }
@@ -210,19 +222,9 @@ private function isAddToAssertionCountExpression(Node $node): bool
210222 return $ this ->isName ($ methodCall ->name , 'addToAssertionCount ' );
211223 }
212224
213- private function isInTrait (ClassMethod $ classMethod ): bool
214- {
215- $ classReflection = $ this ->reflectionResolver ->resolveClassReflection ($ classMethod );
216- if (! $ classReflection instanceof ClassReflection) {
217- return false ;
218- }
219-
220- return $ classReflection ->isTrait ();
221- }
222-
223- private function isInTwigIntegrationTestCase (ClassMethod $ classMethod ): bool
225+ private function isInTwigIntegrationTestCase (Scope $ scope ): bool
224226 {
225- $ classReflection = $ this -> reflectionResolver -> resolveClassReflection ( $ classMethod );
227+ $ classReflection = $ scope -> getClassReflection ( );
226228 if (! $ classReflection instanceof ClassReflection) {
227229 return false ;
228230 }
0 commit comments