Skip to content

Commit 856c8f3

Browse files
committed
feat: add TooWideReturnTypeRector
1 parent 3882cb5 commit 856c8f3

17 files changed

Lines changed: 924 additions & 3 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\RemoveUnusedReturnTypeRector\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\RemoveUnusedReturnTypeRector\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\RemoveUnusedReturnTypeRector\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\RemoveUnusedReturnTypeRector\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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveUnusedReturnTypeRector\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 noReturn(): string|int|null
24+
{
25+
if (rand(0, 1)) {
26+
echo 'something';
27+
}
28+
}
29+
30+
public function withMixed(): mixed|string|int
31+
{
32+
return 'text';
33+
}
34+
}
35+
36+
?>
37+
-----
38+
<?php
39+
40+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveUnusedReturnTypeRector\Fixture;
41+
42+
final class EdgeCases
43+
{
44+
public function implicitNull(): ?string
45+
{
46+
if (rand(0, 1)) {
47+
return;
48+
}
49+
50+
return 'something';
51+
}
52+
53+
public function implicitReturn(): ?string
54+
{
55+
if (rand(0, 1)) {
56+
return 'something';
57+
}
58+
}
59+
60+
public function noReturn(): void
61+
{
62+
if (rand(0, 1)) {
63+
echo 'something';
64+
}
65+
}
66+
67+
public function withMixed(): mixed
68+
{
69+
return 'text';
70+
}
71+
}
72+
73+
?>
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\RemoveUnusedReturnTypeRector\Fixture;
4+
5+
final class ClassMethodCase
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\RemoveUnusedReturnTypeRector\Fixture;
48+
49+
final class ClassMethodCase
50+
{
51+
public function getData(): string|int
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: 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\RemoveUnusedReturnTypeRector\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\RemoveUnusedReturnTypeRector\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\RemoveUnusedReturnTypeRector\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\RemoveUnusedReturnTypeRector\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+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveUnusedReturnTypeRector\Fixture;
4+
5+
abstract class SkipAbstractClass
6+
{
7+
abstract public function process(): string|int|array;
8+
9+
public function getData(): string|int|bool
10+
{
11+
return 'data';
12+
}
13+
}
14+
15+
interface SkipInterface
16+
{
17+
public function handle(): string|int|null;
18+
}
19+
20+
trait SkipTrait
21+
{
22+
public function process(): string|array|null
23+
{
24+
return 'processed';
25+
}
26+
}
27+
28+
?>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FunctionLike\RemoveUnusedReturnTypeRector\Fixture;
4+
5+
class SkipGenerator
6+
{
7+
public function generate(): \Generator|array|null
8+
{
9+
if (rand(0, 1)) {
10+
yield 1;
11+
yield 2;
12+
}
13+
14+
return null;
15+
}
16+
17+
public function delegate(): \Generator|array
18+
{
19+
yield from [1, 2, 3];
20+
}
21+
}
22+
23+
?>

0 commit comments

Comments
 (0)