Skip to content

Commit 68093a9

Browse files
committed
Add strict scalar type support to AddReturnDocblockForScalarArrayFromAssignsRector
1 parent 06ed79e commit 68093a9

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForSc
44

55
class NestedAssign
66
{
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+
725
}
826

927
?>
@@ -14,6 +32,27 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForSc
1432

1533
class NestedAssign
1634
{
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+
1756
}
1857

1958
?>

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)