Skip to content

Commit 4e5681d

Browse files
committed
[perf] optimize ParamTypeByMethodCallTypeRector for speed with early checks
1 parent 17e1297 commit 4e5681d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ final class ShortNameResolverTest extends AbstractLazyTestCase
1818

1919
protected function setUp(): void
2020
{
21-
// @todo let dynamic source locator know about parsed files
2221
parent::setUp();
2322

2423
$this->shortNameResolver = $this->make(ShortNameResolver::class);

rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ public function getNodeTypes(): array
106106
*/
107107
public function refactor(Node $node): ?Node
108108
{
109+
if ($node->params === []) {
110+
return null;
111+
}
112+
113+
// has params with at least one missing type
114+
if (! $this->hasAtLeastOneParamWithoutType($node)) {
115+
return null;
116+
}
117+
109118
if ($node instanceof ClassMethod && $this->shouldSkipClassMethod($node)) {
110119
return null;
111120
}
@@ -116,7 +125,16 @@ public function refactor(Node $node): ?Node
116125
[StaticCall::class, MethodCall::class, FuncCall::class]
117126
);
118127

119-
$hasChanged = $this->refactorFunctionLike($node, $callers);
128+
// keep only callers with args
129+
$callersWithArgs = array_filter($callers, function (StaticCall|MethodCall|FuncCall $caller): bool {
130+
return $caller->args !== [];
131+
});
132+
133+
if ($callersWithArgs === []) {
134+
return null;
135+
}
136+
137+
$hasChanged = $this->refactorFunctionLike($node, $callersWithArgs);
120138
if ($hasChanged) {
121139
return $node;
122140
}
@@ -126,10 +144,6 @@ public function refactor(Node $node): ?Node
126144

127145
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
128146
{
129-
if ($classMethod->params === []) {
130-
return true;
131-
}
132-
133147
$isMissingParameterTypes = false;
134148
foreach ($classMethod->params as $param) {
135149
if ($param->type instanceof Node) {
@@ -218,4 +232,15 @@ private function refactorFunctionLike(
218232

219233
return $hasChanged;
220234
}
235+
236+
private function hasAtLeastOneParamWithoutType(ClassMethod|Function_|Closure|ArrowFunction $functionLike): bool
237+
{
238+
foreach ($functionLike->params as $param) {
239+
if (! $param->type instanceof Node) {
240+
return true;
241+
}
242+
}
243+
244+
return false;
245+
}
221246
}

0 commit comments

Comments
 (0)