Skip to content

Commit 6e90294

Browse files
calebdwsamsonasik
andauthored
feat: add NarrowTooWideReturnTypeRector (#7150)
* feat: add TooWideReturnTypeRector * chore: rename TooWideReturnTypeRector to NarrowTooWideReturnTypeRector * chore: review updates * chore: add min PHP version to NarrowTooWideReturnTypeRector * chore: move base classes to Source * fix: add nullable edge case * chore: remove never/void handling from NarrowTooWideReturnTypeRector * chore: fix logic bug * chore: early return if not class * chore: revert changes to TerminatedNodeAnalyzer * test: skip unknown parameter * feat: add support for phpdoc return types * fix: don't add return type to phpdoc if it doesn't exist * tests: add requested fixture * tests: add test to remove unused phpdoc generic * chore: update fixtures * chore: skip function likes without parameter types * chore: skip methods with native mixed return types * chore: use native types * chore: rector fixes * chore: skip standalone null * test: add requested fixture * feat: support ArrowFunction in BetterNodeFinder, add tests for arrow function generator * chore: refactor NarrowTooWideReturnTypeRector * clean up unnecessry yield check * skip complex type sub type * skip complex type sub type * skip null: mostly placeholder * skip true/false to become bool * skip true/false to become bool * re-run rector * run cs fix * add fixture for parent and child combine --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
1 parent 9f717fb commit 6e90294

31 files changed

Lines changed: 1351 additions & 20 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
$simple = fn($x): string|int|bool => $x > 5 ? 'high' : 10;
6+
7+
$ternary = fn($value): string|int|float|null =>
8+
$value === null ? null : ($value > 0 ? 'positive' : -1);
9+
10+
$cast = fn($input): int|string|array => (int) $input;
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
17+
18+
$simple = fn($x): string|int => $x > 5 ? 'high' : 10;
19+
20+
$ternary = fn($value): string|int|null =>
21+
$value === null ? null : ($value > 0 ? 'positive' : -1);
22+
23+
$cast = fn($input): int => (int) $input;
24+
25+
?>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
$callback = function(): string|int|array|null {
6+
if (rand(0, 1)) {
7+
return 'result';
8+
}
9+
10+
if (rand(0, 1)) {
11+
return 42;
12+
}
13+
14+
return null;
15+
};
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
22+
23+
$callback = function(): string|int|null {
24+
if (rand(0, 1)) {
25+
return 'result';
26+
}
27+
28+
if (rand(0, 1)) {
29+
return 42;
30+
}
31+
32+
return null;
33+
};
34+
35+
?>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
final class EdgeCases
6+
{
7+
public function implicitNull(): string|int|null
8+
{
9+
if (rand(0, 1)) {
10+
return;
11+
}
12+
13+
return 'something';
14+
}
15+
16+
public function implicitReturn(): string|int|null
17+
{
18+
if (rand(0, 1)) {
19+
return 'something';
20+
}
21+
}
22+
23+
public function nullableReturn(): ?string
24+
{
25+
return 'something';
26+
}
27+
}
28+
29+
?>
30+
-----
31+
<?php
32+
33+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
34+
35+
final class EdgeCases
36+
{
37+
public function implicitNull(): ?string
38+
{
39+
if (rand(0, 1)) {
40+
return;
41+
}
42+
43+
return 'something';
44+
}
45+
46+
public function implicitReturn(): ?string
47+
{
48+
if (rand(0, 1)) {
49+
return 'something';
50+
}
51+
}
52+
53+
public function nullableReturn(): string
54+
{
55+
return 'something';
56+
}
57+
}
58+
59+
?>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
final class FinalClass
6+
{
7+
public function getData(): string|int|\DateTime
8+
{
9+
if (rand(0, 1)) {
10+
return 'text';
11+
}
12+
13+
if (rand(0, 1)) {
14+
return 'text';
15+
}
16+
17+
return 1000;
18+
}
19+
20+
public function getInfo(): string|int|bool|null
21+
{
22+
if (rand(0, 1)) {
23+
return 'info';
24+
}
25+
26+
if (rand(0, 1)) {
27+
return null;
28+
}
29+
30+
return 42;
31+
}
32+
33+
private function processData(): string|array|object
34+
{
35+
if (rand(0, 1)) {
36+
return 'processed';
37+
}
38+
39+
return ['data' => 'value'];
40+
}
41+
}
42+
43+
?>
44+
-----
45+
<?php
46+
47+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
48+
49+
final class FinalClass
50+
{
51+
public function getData(): int|string
52+
{
53+
if (rand(0, 1)) {
54+
return 'text';
55+
}
56+
57+
if (rand(0, 1)) {
58+
return 'text';
59+
}
60+
61+
return 1000;
62+
}
63+
64+
public function getInfo(): string|int|null
65+
{
66+
if (rand(0, 1)) {
67+
return 'info';
68+
}
69+
70+
if (rand(0, 1)) {
71+
return null;
72+
}
73+
74+
return 42;
75+
}
76+
77+
private function processData(): string|array
78+
{
79+
if (rand(0, 1)) {
80+
return 'processed';
81+
}
82+
83+
return ['data' => 'value'];
84+
}
85+
}
86+
87+
?>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
4+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
5+
6+
use Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Source\SomeInterface;
7+
use Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Sourcet\SomeAbstractClass;
8+
9+
final class FinalInheritance extends SomeAbstractClass
10+
{
11+
public function process(): string|int|array
12+
{
13+
if (rand(0, 1)) {
14+
return 'text';
15+
}
16+
17+
return 42;
18+
}
19+
}
20+
21+
final class FinalInterfaceImplementation implements SomeInterface
22+
{
23+
public function getData(): string|int|bool
24+
{
25+
return 'data';
26+
}
27+
}
28+
29+
?>
30+
-----
31+
<?php
32+
33+
34+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
35+
36+
use Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Source\SomeInterface;
37+
use Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Sourcet\SomeAbstractClass;
38+
39+
final class FinalInheritance extends SomeAbstractClass
40+
{
41+
public function process(): string|int
42+
{
43+
if (rand(0, 1)) {
44+
return 'text';
45+
}
46+
47+
return 42;
48+
}
49+
}
50+
51+
final class FinalInterfaceImplementation implements SomeInterface
52+
{
53+
public function getData(): string
54+
{
55+
return 'data';
56+
}
57+
}
58+
59+
?>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
class NonFinalClassWithFinalMethods
6+
{
7+
public function getData(): string|int|\DateTime
8+
{
9+
return 'hello';
10+
}
11+
12+
final public function getInfo(): string|int|bool
13+
{
14+
return 'info';
15+
}
16+
17+
final protected function processData(): string|array|object
18+
{
19+
return 'processed';
20+
}
21+
22+
private function helper(): string|int|float
23+
{
24+
return 'helper';
25+
}
26+
}
27+
28+
?>
29+
-----
30+
<?php
31+
32+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
33+
34+
class NonFinalClassWithFinalMethods
35+
{
36+
public function getData(): string|int|\DateTime
37+
{
38+
return 'hello';
39+
}
40+
41+
final public function getInfo(): string
42+
{
43+
return 'info';
44+
}
45+
46+
final protected function processData(): string
47+
{
48+
return 'processed';
49+
}
50+
51+
private function helper(): string
52+
{
53+
return 'helper';
54+
}
55+
}
56+
57+
?>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
4+
5+
function processValue(): string|int|float|bool
6+
{
7+
if (rand(0, 1)) {
8+
return 'text';
9+
}
10+
11+
if (rand(0, 1)) {
12+
return 123;
13+
}
14+
15+
return true;
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowTooWideReturnTypeRector\Fixture;
23+
24+
function processValue(): string|int|bool
25+
{
26+
if (rand(0, 1)) {
27+
return 'text';
28+
}
29+
30+
if (rand(0, 1)) {
31+
return 123;
32+
}
33+
34+
return true;
35+
}
36+
37+
?>

0 commit comments

Comments
 (0)