Skip to content

Commit 64b4ec5

Browse files
authored
[perf] optimize ParamTypeByMethodCallTypeRector for speed with early checks (#7831)
1 parent 17e1297 commit 64b4ec5

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-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: 31 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,17 @@ 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(
130+
$callers,
131+
fn (StaticCall|MethodCall|FuncCall $caller): bool => $caller->args !== []
132+
);
133+
134+
if ($callersWithArgs === []) {
135+
return null;
136+
}
137+
138+
$hasChanged = $this->refactorFunctionLike($node, $callersWithArgs);
120139
if ($hasChanged) {
121140
return $node;
122141
}
@@ -126,10 +145,6 @@ public function refactor(Node $node): ?Node
126145

127146
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
128147
{
129-
if ($classMethod->params === []) {
130-
return true;
131-
}
132-
133148
$isMissingParameterTypes = false;
134149
foreach ($classMethod->params as $param) {
135150
if ($param->type instanceof Node) {
@@ -218,4 +233,15 @@ private function refactorFunctionLike(
218233

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

0 commit comments

Comments
 (0)