diff --git a/bench/remove_unused_constructor_param_attribute_bench.php b/bench/remove_unused_constructor_param_attribute_bench.php new file mode 100644 index 00000000000..d73632e5977 --- /dev/null +++ b/bench/remove_unused_constructor_param_attribute_bench.php @@ -0,0 +1,98 @@ +create(); +$rectorConfig->boot(); + +/** @var TestingParser $testingParser */ +$testingParser = $rectorConfig->make(TestingParser::class); + +/** @var BetterNodeFinder $betterNodeFinder */ +$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); + +$rector = $rectorConfig->make(RemoveUnusedConstructorParamRector::class); + +// generate a file of attribute marker classes, each with a doc-commented constructor whose param +// looks unused - the rule must skip them because they are markers, not dead code +$source = "parseFileToDecoratedNodes($sourceFilePath); + +/** @var Class_[] $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; +}; + +$run = static function () use ($rector, $classes): void { + foreach ($classes as $class) { + $rector->refactor($class); + } +}; + +// warm up autoloading/reflection caches once, excluded from timing +$run(); + +$wallStart = microtime(true); +$cpuStart = $cpuSeconds(); + +for ($repeat = 0; $repeat < REPEAT; ++$repeat) { + $run(); +} + +$cpu = $cpuSeconds() - $cpuStart; +$wall = microtime(true) - $wallStart; + +@unlink($sourceFilePath); + +$totalCalls = REPEAT * count($classes); + +printf('classes: %d, repeats: %d, refactor() calls: %d%s', count($classes), REPEAT, $totalCalls, PHP_EOL); +printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); +printf('wall: %.4f s%s', $wall, PHP_EOL); diff --git a/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector/Fixture/skip_imported_attribute_marker.php.inc b/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector/Fixture/skip_imported_attribute_marker.php.inc new file mode 100644 index 00000000000..2bd81aa2000 --- /dev/null +++ b/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector/Fixture/skip_imported_attribute_marker.php.inc @@ -0,0 +1,17 @@ +isAttributeClass($node) && $constructorClassMethod->getDocComment() instanceof Doc) { + return null; + } + $classReflection = $this->reflectionResolver->resolveClassReflection($node); if (! $classReflection instanceof ClassReflection) { return null; @@ -98,11 +103,6 @@ public function refactor(Node $node): ?Node } } - // attributes can be used as markers - if ($classReflection->isAttributeClass() && $constructorClassMethod->getDocComment() instanceof Doc) { - return null; - } - $changedConstructorClassMethod = $this->classMethodParamRemover->processRemoveParams($constructorClassMethod); if (! $changedConstructorClassMethod instanceof ClassMethod) { return null; @@ -110,4 +110,17 @@ public function refactor(Node $node): ?Node return $node; } + + private function isAttributeClass(Class_ $class): bool + { + foreach ($class->attrGroups as $attrGroup) { + foreach ($attrGroup->attrs as $attr) { + if ($this->isName($attr->name, 'Attribute')) { + return true; + } + } + } + + return false; + } }