diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 6da13444..8fb0a9b5 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -52,6 +52,10 @@ jobs: name: 'Bench framework test cases' run: php bench/does_not_perform_assertion_framework_test_case_bench.php + - + name: 'Bench cache' + run: php bench/does_not_perform_assertion_cache_bench.php + name: ${{ matrix.actions.name }} runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/bench/does_not_perform_assertion_cache_bench.php b/bench/does_not_perform_assertion_cache_bench.php new file mode 100644 index 00000000..2d659a56 --- /dev/null +++ b/bench/does_not_perform_assertion_cache_bench.php @@ -0,0 +1,131 @@ + \$item + {$statement}, \$items);\n"; + } + + $methods .= " protected function helper{$helper}(array \$items): array\n {\n{$statements} return \$items;\n }\n\n"; + } + + $writeClass( + $parentClassName, + "abstract class {$parentClassName} extends \PHPUnit\Framework\TestCase\n{\n{$methods}}\n" + ); + + for ($test = 0; $test < TESTS_PER_PARENT; ++$test) { + $className = sprintf('CacheParent%dTest%d', $parent, $test); + + $methods = " public function testRestart(): void\n {\n parent::setUp();\n }\n\n"; + for ($testMethod = 0; $testMethod < TEST_METHODS; ++$testMethod) { + $methods .= " public function testSomething{$testMethod}(): void\n {\n \$value = \$this->helper{$testMethod}([1, 2, 3]);\n }\n\n"; + } + + $testFilePaths[] = $writeClass( + $className, + "final class {$className} extends {$parentClassName}\n{\n{$methods}}\n" + ); + } +} + +$rectorConfig = (new LazyContainerFactory())->create(); +$rectorConfig->boot(); + +/** @var TestingParser $testingParser */ +$testingParser = $rectorConfig->make(TestingParser::class); + +/** @var BetterNodeFinder $betterNodeFinder */ +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); + +$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class); + +// parse test files up front, parent files are left for the rule to resolve +$classes = []; +foreach ($testFilePaths as $testFilePath) { + $stmts = $testingParser->parseFileToDecoratedNodes($testFilePath); + $classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)]; +} + +$cpuSeconds = static function (): float { + $usage = getrusage(); + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; +}; + +$wallStart = microtime(true); +$cpuStart = $cpuSeconds(); + +$changedCount = 0; +foreach ($classes as $class) { + if ($rector->refactor($class) instanceof Class_) { + ++$changedCount; + } +} + +$cpu = $cpuSeconds() - $cpuStart; +$wall = microtime(true) - $wallStart; + +array_map('unlink', glob($directory . '/*.php')); +@rmdir($directory); + +printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL); +printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL); +printf('wall: %.4f s%s', $wall, PHP_EOL); + +if ($cpu > MAX_CPU_SECONDS) { + printf('CPU %.4f s is over the %.1f s limit%s', $cpu, MAX_CPU_SECONDS, PHP_EOL); + exit(1); +} diff --git a/src/NodeAnalyzer/AssertCallAnalyzer.php b/src/NodeAnalyzer/AssertCallAnalyzer.php index 649fc6fe..ee0a5e58 100644 --- a/src/NodeAnalyzer/AssertCallAnalyzer.php +++ b/src/NodeAnalyzer/AssertCallAnalyzer.php @@ -20,7 +20,6 @@ use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpParser\AstResolver; use Rector\PhpParser\Node\BetterNodeFinder; -use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\PHPUnit\Enum\PHPUnitClassName; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; @@ -28,6 +27,8 @@ final class AssertCallAnalyzer { private const int MAX_NESTED_METHOD_CALL_LEVEL = 5; + private const string CONTAINS_ASSERT_CALL_ATTRIBUTE = 'contains_assert_call'; + /** * @see https://docs.phpunit.de/en/12.4/assertions.html */ @@ -46,11 +47,6 @@ final class AssertCallAnalyzer 'should', ]; - /** - * @var array - */ - private array $containsAssertCallByClassMethod = []; - /** * This should prevent segfaults while going too deep into to parsed code. Without it, it might end-up with segfault */ @@ -58,7 +54,6 @@ final class AssertCallAnalyzer public function __construct( private readonly AstResolver $astResolver, - private readonly BetterStandardPrinter $betterStandardPrinter, private readonly BetterNodeFinder $betterNodeFinder, private readonly NodeNameResolver $nodeNameResolver, private readonly NodeTypeResolver $nodeTypeResolver, @@ -81,25 +76,22 @@ public function containsAssertCall(ClassMethod $classMethod): bool return false; } - // the same body means a different method per class, e.g. "$this->helper()" or "parent::test()" - $cacheHash = md5( - $this->betterStandardPrinter->prettyPrint([$classMethod]) . $this->resolveOwnClassName($classMethod) - ); - - if (isset($this->containsAssertCallByClassMethod[$cacheHash])) { - return $this->containsAssertCallByClassMethod[$cacheHash]; + // cached per node, as the same body means a different method per class, e.g. "$this->helper()" + $containsAssertCall = $classMethod->getAttribute(self::CONTAINS_ASSERT_CALL_ATTRIBUTE); + if (is_bool($containsAssertCall)) { + return $containsAssertCall; } // A. try "->assert" shallow search first for performance $hasDirectAssertOrMockCall = $this->hasDirectAssertOrMockCall($classMethod); if ($hasDirectAssertOrMockCall) { - $this->containsAssertCallByClassMethod[$cacheHash] = true; + $classMethod->setAttribute(self::CONTAINS_ASSERT_CALL_ATTRIBUTE, true); return true; } // B. look for nested calls $hasNestedAssertOrMockCall = $this->hasNestedAssertCall($classMethod); - $this->containsAssertCallByClassMethod[$cacheHash] = $hasNestedAssertOrMockCall; + $classMethod->setAttribute(self::CONTAINS_ASSERT_CALL_ATTRIBUTE, $hasNestedAssertOrMockCall); return $hasNestedAssertOrMockCall; } finally { @@ -249,16 +241,6 @@ private function isDeclaredInFrameworkTestCase(string $className, string $method return in_array($declaringClassName, PHPUnitClassName::FRAMEWORK_TEST_CASES, true); } - private function resolveOwnClassName(ClassMethod $classMethod): string - { - $classReflection = $this->resolveClassReflection($classMethod); - if (! $classReflection instanceof ClassReflection) { - return ''; - } - - return $classReflection->getName(); - } - private function resolveClassName(StaticCall|MethodCall $call): ?string { if ($call instanceof StaticCall && $this->nodeNameResolver->isName($call->class, ObjectReference::PARENT)) { @@ -282,9 +264,9 @@ private function resolveClassName(StaticCall|MethodCall $call): ?string return $objectType->getClassName(); } - private function resolveClassReflection(Node $node): ?ClassReflection + private function resolveClassReflection(StaticCall $staticCall): ?ClassReflection { - $scope = $node->getAttribute(AttributeKey::SCOPE); + $scope = $staticCall->getAttribute(AttributeKey::SCOPE); if (! $scope instanceof Scope) { return null; }