Skip to content

Commit aee6fc8

Browse files
authored
[perf] Speed up DeadCode rules by deferring docblock parse and parent reflection behind cheap guards (#8667)
1 parent 2326ffe commit aee6fc8

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingClassMethodRector.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ public function refactor(Node $node): ?int
9494
return null;
9595
}
9696

97-
if ($this->hasRefiningDocblock($node)) {
97+
if (! $this->isParentCallDelegatingParams($node, $node->stmts[0])) {
9898
return null;
9999
}
100100

101-
$parentMethodReflection = $this->matchParentMethodReflection($node);
102-
if (! $parentMethodReflection instanceof ExtendedMethodReflection) {
101+
if ($this->hasRefiningDocblock($node)) {
103102
return null;
104103
}
105104

106-
if (! $this->isParentCallDelegatingParams($node, $node->stmts[0])) {
105+
$parentMethodReflection = $this->matchParentMethodReflection($node);
106+
if (! $parentMethodReflection instanceof ExtendedMethodReflection) {
107107
return null;
108108
}
109109

‎rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,24 @@ public function refactor(Node $node): ?int
108108
return null;
109109
}
110110

111-
$parentMethodReflection = $this->matchParentConstructorReflection($node);
112-
if (! $parentMethodReflection instanceof ExtendedMethodReflection) {
111+
$soleStmt = $node->stmts[0];
112+
$parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt);
113+
if ($parentCallArgs === null) {
113114
return null;
114115
}
115116

116-
// removing public constructor would expose the narrower parent one
117-
if (! $parentMethodReflection->isPublic()) {
117+
// match count and order
118+
if (! $this->isParameterAndArgCountAndOrderIdentical($node)) {
118119
return null;
119120
}
120121

121-
$soleStmt = $node->stmts[0];
122-
$parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt);
123-
if ($parentCallArgs === null) {
122+
$parentMethodReflection = $this->matchParentConstructorReflection($node);
123+
if (! $parentMethodReflection instanceof ExtendedMethodReflection) {
124124
return null;
125125
}
126126

127-
// match count and order
128-
if (! $this->isParameterAndArgCountAndOrderIdentical($node)) {
127+
// removing public constructor would expose the narrower parent one
128+
if (! $parentMethodReflection->isPublic()) {
129129
return null;
130130
}
131131

‎rules/DeadCode/Rector/ClassMethod/RemoveVoidDocblockFromMagicMethodRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public function refactor(Node $node): ?Node
8585
return null;
8686
}
8787

88+
// early return if no @return tag is present
89+
if (! str_contains((string) $node->getDocComment(), '@return')) {
90+
return null;
91+
}
92+
8893
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
8994

9095
$returnTagValueNode = $phpDocInfo->getReturnTagValue();

‎rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public function refactor(Node $node): ?Node
100100
$stmt->keyVar = null;
101101
$hasChanged = true;
102102

103+
// no @var tag to clean up
104+
if (! str_contains((string) $stmt->getDocComment(), '@var')) {
105+
continue;
106+
}
107+
103108
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($stmt);
104109
if (! $phpDocInfo instanceof PhpDocInfo) {
105110
continue;

0 commit comments

Comments
 (0)