Skip to content

Commit 08f39c3

Browse files
imliamsamsonasik
andauthored
feat: Add AddReturnDocblockForScalarArrayFromAssignsRector (#7151)
* Add a more specific return docblock for scalar lists when we are certain of the types inside the array (eg. when they were initialised and returned within the same function) * Fixes for PHPStan * Update rules/TypeDeclaration/Rector/ClassMethod/AddReturnDocblockForScalarArrayFromAssignsRector.php Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> --------- Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
1 parent ae34cd4 commit 08f39c3

File tree

5 files changed

+524
-0
lines changed

5 files changed

+524
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddReturnDocblockForScalarArrayFromAssignsRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
4+
5+
function getSomeItems()
6+
{
7+
$items = [];
8+
$items[] = 'hey';
9+
$items[] = 'hello';
10+
return $items;
11+
}
12+
13+
class SomeClass
14+
{
15+
public function getStringItems()
16+
{
17+
$items = [];
18+
$items[] = 'first';
19+
$items[] = 'second';
20+
return $items;
21+
}
22+
23+
public function getIntItems()
24+
{
25+
$numbers = [];
26+
$numbers[] = 1;
27+
$numbers[] = 2;
28+
$numbers[] = 3;
29+
return $numbers;
30+
}
31+
32+
public function getFloatItems()
33+
{
34+
$floats = [];
35+
$floats[] = 1.5;
36+
$floats[] = 2.5;
37+
return $floats;
38+
}
39+
}
40+
41+
function withNativeArrayType(): array
42+
{
43+
$items = [];
44+
$items[] = 'native';
45+
$items[] = 'array';
46+
return $items;
47+
}
48+
49+
class WithNativeArrayReturn
50+
{
51+
public function getNumbers(): array
52+
{
53+
$numbers = [];
54+
$numbers[] = 42;
55+
$numbers[] = 100;
56+
return $numbers;
57+
}
58+
}
59+
60+
?>
61+
-----
62+
<?php
63+
64+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
65+
66+
/**
67+
* @return string[]
68+
*/
69+
function getSomeItems()
70+
{
71+
$items = [];
72+
$items[] = 'hey';
73+
$items[] = 'hello';
74+
return $items;
75+
}
76+
77+
class SomeClass
78+
{
79+
/**
80+
* @return string[]
81+
*/
82+
public function getStringItems()
83+
{
84+
$items = [];
85+
$items[] = 'first';
86+
$items[] = 'second';
87+
return $items;
88+
}
89+
90+
/**
91+
* @return int[]
92+
*/
93+
public function getIntItems()
94+
{
95+
$numbers = [];
96+
$numbers[] = 1;
97+
$numbers[] = 2;
98+
$numbers[] = 3;
99+
return $numbers;
100+
}
101+
102+
/**
103+
* @return float[]
104+
*/
105+
public function getFloatItems()
106+
{
107+
$floats = [];
108+
$floats[] = 1.5;
109+
$floats[] = 2.5;
110+
return $floats;
111+
}
112+
}
113+
114+
/**
115+
* @return string[]
116+
*/
117+
function withNativeArrayType(): array
118+
{
119+
$items = [];
120+
$items[] = 'native';
121+
$items[] = 'array';
122+
return $items;
123+
}
124+
125+
class WithNativeArrayReturn
126+
{
127+
/**
128+
* @return int[]
129+
*/
130+
public function getNumbers(): array
131+
{
132+
$numbers = [];
133+
$numbers[] = 42;
134+
$numbers[] = 100;
135+
return $numbers;
136+
}
137+
}
138+
139+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
4+
5+
class SkipAlreadyHasReturnType
6+
{
7+
/**
8+
* @return array
9+
*/
10+
public function getItems()
11+
{
12+
$items = [];
13+
$items[] = 'value';
14+
return $items;
15+
}
16+
}
17+
18+
class SkipIterableReturnType
19+
{
20+
public function getItems(): iterable
21+
{
22+
$items = [];
23+
$items[] = 'value';
24+
return $items;
25+
}
26+
}
27+
28+
class SkipMixedTypes
29+
{
30+
public function getMixedItems()
31+
{
32+
$items = [];
33+
$items[] = 'string';
34+
$items[] = 123;
35+
return $items;
36+
}
37+
}
38+
39+
class SkipNoArrayInit
40+
{
41+
public function getItems()
42+
{
43+
$items[] = 'value';
44+
return $items;
45+
}
46+
}
47+
48+
class SkipNoReturnVariable
49+
{
50+
public function getItems()
51+
{
52+
$items = [];
53+
$items[] = 'value';
54+
return [];
55+
}
56+
}
57+
58+
class SkipExplicitKey
59+
{
60+
public function getItems()
61+
{
62+
$items = [];
63+
$items['key'] = 'value';
64+
return $items;
65+
}
66+
}
67+
68+
class SkipMixedTypesInMultipleVariables
69+
{
70+
public function getItems()
71+
{
72+
$strings = [];
73+
$strings[] = 'string';
74+
75+
$mixed = [];
76+
$mixed[] = 'string';
77+
$mixed[] = 123;
78+
79+
if (rand(0, 1)) {
80+
return $strings;
81+
}
82+
83+
return $mixed;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([AddReturnDocblockForScalarArrayFromAssignsRector::class]);

0 commit comments

Comments
 (0)