1919use Rector \NodeTypeResolver \NodeTypeResolver ;
2020use Rector \PhpParser \AstResolver ;
2121use Rector \PhpParser \Node \BetterNodeFinder ;
22- use Rector \PhpParser \Printer \BetterStandardPrinter ;
2322use Rector \PHPUnit \Enum \PHPUnitClassName ;
2423use Rector \StaticTypeMapper \ValueObject \Type \FullyQualifiedObjectType ;
2524
2625final class AssertCallAnalyzer
2726{
2827 private const int MAX_NESTED_METHOD_CALL_LEVEL = 5 ;
2928
29+ private const string CONTAINS_ASSERT_CALL_ATTRIBUTE = 'contains_assert_call ' ;
30+
3031 /**
3132 * @see https://docs.phpunit.de/en/12.4/assertions.html
3233 */
@@ -45,19 +46,13 @@ final class AssertCallAnalyzer
4546 'should ' ,
4647 ];
4748
48- /**
49- * @var array<string, bool>
50- */
51- private array $ containsAssertCallByClassMethod = [];
52-
5349 /**
5450 * This should prevent segfaults while going too deep into to parsed code. Without it, it might end-up with segfault
5551 */
5652 private int $ classMethodNestingLevel = 0 ;
5753
5854 public function __construct (
5955 private readonly AstResolver $ astResolver ,
60- private readonly BetterStandardPrinter $ betterStandardPrinter ,
6156 private readonly BetterNodeFinder $ betterNodeFinder ,
6257 private readonly NodeNameResolver $ nodeNameResolver ,
6358 private readonly NodeTypeResolver $ nodeTypeResolver ,
@@ -79,25 +74,22 @@ public function containsAssertCall(ClassMethod $classMethod): bool
7974 return false ;
8075 }
8176
82- // the same body means a different method per class, e.g. "$this->helper()" or "parent::test()"
83- $ cacheHash = md5 (
84- $ this ->betterStandardPrinter ->prettyPrint ([$ classMethod ]) . $ this ->resolveOwnClassName ($ classMethod )
85- );
86-
87- if (isset ($ this ->containsAssertCallByClassMethod [$ cacheHash ])) {
88- return $ this ->containsAssertCallByClassMethod [$ cacheHash ];
77+ // cached per node, as the same body means a different method per class, e.g. "$this->helper()"
78+ $ containsAssertCall = $ classMethod ->getAttribute (self ::CONTAINS_ASSERT_CALL_ATTRIBUTE );
79+ if (is_bool ($ containsAssertCall )) {
80+ return $ containsAssertCall ;
8981 }
9082
9183 // A. try "->assert" shallow search first for performance
9284 $ hasDirectAssertOrMockCall = $ this ->hasDirectAssertOrMockCall ($ classMethod );
9385 if ($ hasDirectAssertOrMockCall ) {
94- $ this -> containsAssertCallByClassMethod [ $ cacheHash ] = true ;
86+ $ classMethod -> setAttribute ( self :: CONTAINS_ASSERT_CALL_ATTRIBUTE , true ) ;
9587 return true ;
9688 }
9789
9890 // B. look for nested calls
9991 $ hasNestedAssertOrMockCall = $ this ->hasNestedAssertCall ($ classMethod );
100- $ this -> containsAssertCallByClassMethod [ $ cacheHash ] = $ hasNestedAssertOrMockCall ;
92+ $ classMethod -> setAttribute ( self :: CONTAINS_ASSERT_CALL_ATTRIBUTE , $ hasNestedAssertOrMockCall) ;
10193
10294 return $ hasNestedAssertOrMockCall ;
10395 } finally {
@@ -216,25 +208,15 @@ private function resolveClassMethodFromCall(StaticCall|MethodCall $call): ?Class
216208 return null ;
217209 }
218210
219- $ className = $ this ->resolveClassName ($ call );
211+ $ className = $ this ->resolveClassName ($ call, $ methodName );
220212 if ($ className === null ) {
221213 return null ;
222214 }
223215
224216 return $ this ->astResolver ->resolveClassMethod ($ className , $ methodName );
225217 }
226218
227- private function resolveOwnClassName (ClassMethod $ classMethod ): string
228- {
229- $ classReflection = $ this ->resolveClassReflection ($ classMethod );
230- if (! $ classReflection instanceof ClassReflection) {
231- return '' ;
232- }
233-
234- return $ classReflection ->getName ();
235- }
236-
237- private function resolveClassName (StaticCall |MethodCall $ call ): ?string
219+ private function resolveClassName (StaticCall |MethodCall $ call , string $ methodName ): ?string
238220 {
239221 if ($ call instanceof StaticCall && $ this ->nodeNameResolver ->isName ($ call ->class , ObjectReference::PARENT )) {
240222 // "parent" resolves to a union of all ancestors in type resolver, so use the direct parent class instead
@@ -243,12 +225,21 @@ private function resolveClassName(StaticCall|MethodCall $call): ?string
243225 return null ;
244226 }
245227
228+ if (! $ parentClassReflection ->hasNativeMethod ($ methodName )) {
229+ return null ;
230+ }
231+
232+ // the method can be inherited from any ancestor
233+ $ declaringClassName = $ parentClassReflection ->getNativeMethod ($ methodName )
234+ ->getDeclaringClass ()
235+ ->getName ();
236+
246237 // PHPUnit itself has no asserting test methods, no need to parse its huge classes
247- if ($ parentClassReflection -> getName () === PHPUnitClassName::TEST_CASE ) {
238+ if ($ declaringClassName === PHPUnitClassName::TEST_CASE ) {
248239 return null ;
249240 }
250241
251- return $ parentClassReflection -> getName () ;
242+ return $ declaringClassName ;
252243 }
253244
254245 $ objectType = $ call instanceof MethodCall
@@ -262,9 +253,9 @@ private function resolveClassName(StaticCall|MethodCall $call): ?string
262253 return $ objectType ->getClassName ();
263254 }
264255
265- private function resolveClassReflection (Node $ node ): ?ClassReflection
256+ private function resolveClassReflection (StaticCall $ staticCall ): ?ClassReflection
266257 {
267- $ scope = $ node ->getAttribute (AttributeKey::SCOPE );
258+ $ scope = $ staticCall ->getAttribute (AttributeKey::SCOPE );
268259 if (! $ scope instanceof Scope) {
269260 return null ;
270261 }
0 commit comments