diff --git a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/keep_deep_calls_before_assert_helper.php.inc b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/keep_deep_calls_before_assert_helper.php.inc new file mode 100644 index 000000000..b9f7c775e --- /dev/null +++ b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/keep_deep_calls_before_assert_helper.php.inc @@ -0,0 +1,47 @@ +prepare(); + + $this->checkResult(true); + } + + private function prepare(): void + { + $this->stepTwo(); + } + + private function stepTwo(): void + { + $this->stepThree(); + } + + private function stepThree(): void + { + $this->stepFour(); + } + + private function stepFour(): void + { + $this->stepFive(); + } + + private function stepFive(): void + { + // no assertion here + } + + private function checkResult(bool $bool): void + { + self::assertTrue($bool); + } +} diff --git a/src/NodeAnalyzer/AssertCallAnalyzer.php b/src/NodeAnalyzer/AssertCallAnalyzer.php index 7cd318d6d..dcdc9f300 100644 --- a/src/NodeAnalyzer/AssertCallAnalyzer.php +++ b/src/NodeAnalyzer/AssertCallAnalyzer.php @@ -61,29 +61,34 @@ public function containsAssertCall(ClassMethod $classMethod): bool { ++$this->classMethodNestingLevel; - // probably no assert method in the end - if ($this->classMethodNestingLevel > self::MAX_NESTED_METHOD_CALL_LEVEL) { - return false; - } + try { + // probably no assert method in the end + if ($this->classMethodNestingLevel > self::MAX_NESTED_METHOD_CALL_LEVEL) { + return false; + } - $cacheHash = md5($this->betterStandardPrinter->prettyPrint([$classMethod])); + $cacheHash = md5($this->betterStandardPrinter->prettyPrint([$classMethod])); - if (isset($this->containsAssertCallByClassMethod[$cacheHash])) { - return $this->containsAssertCallByClassMethod[$cacheHash]; - } + if (isset($this->containsAssertCallByClassMethod[$cacheHash])) { + return $this->containsAssertCallByClassMethod[$cacheHash]; + } - // A. try "->assert" shallow search first for performance - $hasDirectAssertOrMockCall = $this->hasDirectAssertOrMockCall($classMethod); - if ($hasDirectAssertOrMockCall) { - $this->containsAssertCallByClassMethod[$cacheHash] = $hasDirectAssertOrMockCall; - return true; - } + // A. try "->assert" shallow search first for performance + $hasDirectAssertOrMockCall = $this->hasDirectAssertOrMockCall($classMethod); + if ($hasDirectAssertOrMockCall) { + $this->containsAssertCallByClassMethod[$cacheHash] = $hasDirectAssertOrMockCall; + return true; + } - // B. look for nested calls - $hasNestedAssertOrMockCall = $this->hasNestedAssertCall($classMethod); - $this->containsAssertCallByClassMethod[$cacheHash] = $hasNestedAssertOrMockCall; + // B. look for nested calls + $hasNestedAssertOrMockCall = $this->hasNestedAssertCall($classMethod); + $this->containsAssertCallByClassMethod[$cacheHash] = $hasNestedAssertOrMockCall; - return $hasNestedAssertOrMockCall; + return $hasNestedAssertOrMockCall; + } finally { + // restore depth so sibling calls in the same DFS keep their full budget + --$this->classMethodNestingLevel; + } } public function isAssertMethodCall(MethodCall|StaticCall $call): bool