Skip to content

Commit 33771c6

Browse files
authored
[type-declaration-docblocks] Add strict scalar type support to AddReturnDocblockForScalarArrayFromAssignsRector (#7389)
* add nested assign fixture * Add strict scalar type support to AddReturnDocblockForScalarArrayFromAssignsRector
1 parent edadebb commit 33771c6

File tree

4 files changed

+101
-19
lines changed

4 files changed

+101
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
4+
5+
final class SomeClass
6+
{
7+
public function getFloatItems()
8+
{
9+
$floats = [];
10+
$floats[] = 1.5;
11+
$floats[] = 2.5;
12+
return $floats;
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
21+
22+
final class SomeClass
23+
{
24+
/**
25+
* @return float[]
26+
*/
27+
public function getFloatItems()
28+
{
29+
$floats = [];
30+
$floats[] = 1.5;
31+
$floats[] = 2.5;
32+
return $floats;
33+
}
34+
}
35+
36+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
4+
5+
class NestedAssign
6+
{
7+
private function someMethod(string $path): array
8+
{
9+
$stringLength = strlen($path);
10+
11+
$items = [];
12+
$currentdItem = '';
13+
14+
for ($i = 0; $i < $stringLength; $i++) {
15+
$currentdItem = (string) $path[$i];
16+
}
17+
18+
if ($currentdItem !== '') {
19+
$items[] = $currentdItem;
20+
}
21+
22+
return $items;
23+
}
24+
25+
}
26+
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;
32+
33+
class NestedAssign
34+
{
35+
/**
36+
* @return string[]
37+
*/
38+
private function someMethod(string $path): array
39+
{
40+
$stringLength = strlen($path);
41+
42+
$items = [];
43+
$currentdItem = '';
44+
45+
for ($i = 0; $i < $stringLength; $i++) {
46+
$currentdItem = (string) $path[$i];
47+
}
48+
49+
if ($currentdItem !== '') {
50+
$items[] = $currentdItem;
51+
}
52+
53+
return $items;
54+
}
55+
56+
}
57+
58+
?>

rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnDocblockForScalarArrayFromAssignsRector/Fixture/simple_array_assigns.php.inc

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ class SomeClass
2828
$numbers[] = 3;
2929
return $numbers;
3030
}
31-
32-
public function getFloatItems()
33-
{
34-
$floats = [];
35-
$floats[] = 1.5;
36-
$floats[] = 2.5;
37-
return $floats;
38-
}
3931
}
4032

4133
function withNativeArrayType(): array
@@ -98,17 +90,6 @@ class SomeClass
9890
$numbers[] = 3;
9991
return $numbers;
10092
}
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-
}
11293
}
11394

11495
/**

rules/TypeDeclaration/Rector/ClassMethod/AddReturnDocblockForScalarArrayFromAssignsRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function refactor(Node $node): ?Node
135135
$scalarArrayTypes = [];
136136
foreach ($returnedVariableNames as $returnedVariableName) {
137137
$scalarType = $this->resolveScalarArrayTypeForVariable($node, $returnedVariableName);
138+
138139
if ($scalarType instanceof Type) {
139140
$scalarArrayTypes[] = $scalarType;
140141
} else {
@@ -219,6 +220,7 @@ private function resolveScalarArrayTypeForVariable(ClassMethod|Function_ $node,
219220
$arrayHasDimAssigns = true;
220221

221222
$scalarType = $this->resolveScalarType($assign->expr);
223+
222224
if ($scalarType instanceof Type) {
223225
$scalarTypes[] = $scalarType;
224226
} else {
@@ -258,6 +260,11 @@ private function resolveScalarType(Expr $expr): ?Type
258260
return new FloatType();
259261
}
260262

263+
$exprType = $this->nodeTypeResolver->getNativeType($expr);
264+
if ($exprType->isScalar()->yes()) {
265+
return $exprType;
266+
}
267+
261268
return null;
262269
}
263270
}

0 commit comments

Comments
 (0)