Skip to content

Commit 441b483

Browse files
committed
chore: skip function likes without parameter types
1 parent c16f49a commit 441b483

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

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

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

10-
$cast = fn($input): int|string|array => (int) $input;
10+
$cast = fn(mixed $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($x): string|int => $x > 5 ? 'high' : 10;
18+
$simple = fn(int $x): string|int => $x > 5 ? 'high' : 10;
1919

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

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

2525
?>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class PhpDocs
4646
/**
4747
* @param int $a
4848
*/
49-
function quux($a): int|string
49+
function quux(int $a): int|string
5050
{
5151
return $a;
5252
}
@@ -55,7 +55,7 @@ final class PhpDocs
5555
* @param int $a
5656
* @return int|string
5757
*/
58-
function mixedReturn($a): int|string
58+
function mixedReturn(int $a): int|string
5959
{
6060
return $a;
6161
}
@@ -111,7 +111,7 @@ final class PhpDocs
111111
/**
112112
* @param int $a
113113
*/
114-
function quux($a): int
114+
function quux(int $a): int
115115
{
116116
return $a;
117117
}
@@ -120,7 +120,7 @@ final class PhpDocs
120120
* @param int $a
121121
* @return int
122122
*/
123-
function mixedReturn($a): int
123+
function mixedReturn(int $a): int
124124
{
125125
return $a;
126126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
final class skip_function_likes_without_parameter_types
6+
{
7+
/**
8+
* @param class-string<SomeInterface> $class
9+
* @return class-string<SomeInterface>|int
10+
*/
11+
public function bar($class): string|int
12+
{
13+
return $class;
14+
}
15+
}
16+
17+
?>

rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ private function shouldSkipNode(ClassMethod|Function_|Closure|ArrowFunction $nod
164164
return true;
165165
}
166166

167+
foreach ($node->params as $param) {
168+
if (! $param->type instanceof Node) {
169+
return true;
170+
}
171+
}
172+
167173
if (! $node instanceof ClassMethod) {
168174
return false;
169175
}

0 commit comments

Comments
 (0)