Skip to content

Commit 83bd9a1

Browse files
committed
chore: skip methods with native mixed return types
1 parent a9e401f commit 83bd9a1

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

rules-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/arrow_function.php.inc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
44

5-
$simple = fn(int $x): string|int|bool => $x > 5 ? 'high' : 10;
5+
$simple = fn($x): string|int|bool => $x > 5 ? 'high' : 10;
66

7-
$ternary = fn(?int $value): string|int|float|null =>
7+
$ternary = fn($value): string|int|float|null =>
88
$value === null ? null : ($value > 0 ? 'positive' : -1);
99

10-
$cast = fn(mixed $input): int|string|array => (int) $input;
10+
$cast = fn($input): int|string|array => (int) $input;
1111

1212
?>
1313
-----
1414
<?php
1515

1616
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
1717

18-
$simple = fn(int $x): string|int => $x > 5 ? 'high' : 10;
18+
$simple = fn($x): string|int => $x > 5 ? 'high' : 10;
1919

20-
$ternary = fn(?int $value): string|int|null =>
20+
$ternary = fn($value): string|int|null =>
2121
$value === null ? null : ($value > 0 ? 'positive' : -1);
2222

23-
$cast = fn(mixed $input): int => (int) $input;
23+
$cast = fn($input): int => (int) $input;
2424

2525
?>

rules-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/skip_function_likes_without_parameter_types.php.inc renamed to rules-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/skip_native_mixed_returns.php.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ final class SkipFunctionLikesWithoutParameterTypes
1212
{
1313
return $class;
1414
}
15+
16+
/**
17+
* @return class-string<\stdClass>|int
18+
*/
19+
public function skipMixedByDoc(): string|int
20+
{
21+
/**
22+
* @var class-string<\stdClass> $class
23+
*/
24+
$class = get();
25+
return $class;
26+
}
1527
}
1628

1729
?>

rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\DeadCode\Rector\FunctionLike;
66

7+
use PHPStan\Type\MixedType;
78
use PhpParser\Node;
89
use PhpParser\Node\Expr\ArrowFunction;
910
use PhpParser\Node\Expr\Closure;
@@ -134,6 +135,11 @@ public function refactor(Node $node): ?Node
134135
}
135136

136137
$actualReturnTypes = $this->collectActualReturnTypes($node, $returnStatements, $isAlwaysTerminating);
138+
139+
if ($actualReturnTypes === null) {
140+
return null;
141+
}
142+
137143
$newReturnType = $this->narrowReturnType($returnType, $actualReturnTypes);
138144

139145
if ($newReturnType === null) {
@@ -164,12 +170,6 @@ private function shouldSkipNode(ClassMethod|Function_|Closure|ArrowFunction $nod
164170
return true;
165171
}
166172

167-
foreach ($node->params as $param) {
168-
if (! $param->type instanceof Node) {
169-
return true;
170-
}
171-
}
172-
173173
if (! $node instanceof ClassMethod) {
174174
return false;
175175
}
@@ -203,7 +203,7 @@ private function collectActualReturnTypes(
203203
ClassMethod|Function_|Closure|ArrowFunction $node,
204204
array $returnStatements,
205205
bool $isAlwaysTerminating,
206-
): array {
206+
): ?array {
207207
if ($node instanceof ArrowFunction) {
208208
return [$this->getType($node->expr)];
209209
}
@@ -215,6 +215,12 @@ private function collectActualReturnTypes(
215215
continue;
216216
}
217217

218+
$nativeReturnType = $this->nodeTypeResolver->getNativeType($returnStatement->expr);
219+
220+
if ($nativeReturnType instanceof MixedType) {
221+
return null;
222+
}
223+
218224
$returnTypes[] = $this->getType($returnStatement->expr);
219225
}
220226

0 commit comments

Comments
 (0)