From 6fdbf973f4a8e8b0a31a74ef223acb2283dc8d2f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 15 Oct 2026 21:21:09 +0200 Subject: [PATCH] [perf] Speed up DeadCode rules by deferring docblock parse and parent reflection behind cheap guards --- ...RemoveParentDelegatingClassMethodRector.php | 8 ++++---- ...RemoveParentDelegatingConstructorRector.php | 18 +++++++++--------- ...RemoveVoidDocblockFromMagicMethodRector.php | 5 +++++ .../Foreach_/RemoveUnusedForeachKeyRector.php | 5 +++++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingClassMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingClassMethodRector.php index 104f783c21b..d41c24010b5 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingClassMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingClassMethodRector.php @@ -94,16 +94,16 @@ public function refactor(Node $node): ?int return null; } - if ($this->hasRefiningDocblock($node)) { + if (! $this->isParentCallDelegatingParams($node, $node->stmts[0])) { return null; } - $parentMethodReflection = $this->matchParentMethodReflection($node); - if (! $parentMethodReflection instanceof ExtendedMethodReflection) { + if ($this->hasRefiningDocblock($node)) { return null; } - if (! $this->isParentCallDelegatingParams($node, $node->stmts[0])) { + $parentMethodReflection = $this->matchParentMethodReflection($node); + if (! $parentMethodReflection instanceof ExtendedMethodReflection) { return null; } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php index b3b4f2c5449..e452c8289d5 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php @@ -108,24 +108,24 @@ public function refactor(Node $node): ?int return null; } - $parentMethodReflection = $this->matchParentConstructorReflection($node); - if (! $parentMethodReflection instanceof ExtendedMethodReflection) { + $soleStmt = $node->stmts[0]; + $parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt); + if ($parentCallArgs === null) { return null; } - // removing public constructor would expose the narrower parent one - if (! $parentMethodReflection->isPublic()) { + // match count and order + if (! $this->isParameterAndArgCountAndOrderIdentical($node)) { return null; } - $soleStmt = $node->stmts[0]; - $parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt); - if ($parentCallArgs === null) { + $parentMethodReflection = $this->matchParentConstructorReflection($node); + if (! $parentMethodReflection instanceof ExtendedMethodReflection) { return null; } - // match count and order - if (! $this->isParameterAndArgCountAndOrderIdentical($node)) { + // removing public constructor would expose the narrower parent one + if (! $parentMethodReflection->isPublic()) { return null; } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveVoidDocblockFromMagicMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveVoidDocblockFromMagicMethodRector.php index 7e9cf877c29..e725ba64c33 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveVoidDocblockFromMagicMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveVoidDocblockFromMagicMethodRector.php @@ -85,6 +85,11 @@ public function refactor(Node $node): ?Node return null; } + // early return if no @return tag is present + if (! str_contains((string) $node->getDocComment(), '@return')) { + return null; + } + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); $returnTagValueNode = $phpDocInfo->getReturnTagValue(); diff --git a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php index 60aae36fabe..fbdd06b1847 100644 --- a/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php +++ b/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php @@ -100,6 +100,11 @@ public function refactor(Node $node): ?Node $stmt->keyVar = null; $hasChanged = true; + // no @var tag to clean up + if (! str_contains((string) $stmt->getDocComment(), '@var')) { + continue; + } + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($stmt); if (! $phpDocInfo instanceof PhpDocInfo) { continue;