Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;

final class SomeClass
{
public function getFloatItems()
{
$floats = [];
$floats[] = 1.5;
$floats[] = 2.5;
return $floats;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;

final class SomeClass
{
/**
* @return float[]
*/
public function getFloatItems()
{
$floats = [];
$floats[] = 1.5;
$floats[] = 2.5;
return $floats;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;

class NestedAssign
{
private function someMethod(string $path): array
{
$stringLength = strlen($path);

$items = [];
$currentdItem = '';

for ($i = 0; $i < $stringLength; $i++) {
$currentdItem = (string) $path[$i];
}

if ($currentdItem !== '') {
$items[] = $currentdItem;
}

return $items;
}

}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnDocblockForScalarArrayFromAssignsRector\Fixture;

class NestedAssign
{
/**
* @return string[]
*/
private function someMethod(string $path): array
{
$stringLength = strlen($path);

$items = [];
$currentdItem = '';

for ($i = 0; $i < $stringLength; $i++) {
$currentdItem = (string) $path[$i];
}

if ($currentdItem !== '') {
$items[] = $currentdItem;
}

return $items;
}

}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ class SomeClass
$numbers[] = 3;
return $numbers;
}

public function getFloatItems()
{
$floats = [];
$floats[] = 1.5;
$floats[] = 2.5;
return $floats;
}
}

function withNativeArrayType(): array
Expand Down Expand Up @@ -98,17 +90,6 @@ class SomeClass
$numbers[] = 3;
return $numbers;
}

/**
* @return float[]
*/
public function getFloatItems()
{
$floats = [];
$floats[] = 1.5;
$floats[] = 2.5;
return $floats;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function refactor(Node $node): ?Node
$scalarArrayTypes = [];
foreach ($returnedVariableNames as $returnedVariableName) {
$scalarType = $this->resolveScalarArrayTypeForVariable($node, $returnedVariableName);

if ($scalarType instanceof Type) {
$scalarArrayTypes[] = $scalarType;
} else {
Expand Down Expand Up @@ -219,6 +220,7 @@ private function resolveScalarArrayTypeForVariable(ClassMethod|Function_ $node,
$arrayHasDimAssigns = true;

$scalarType = $this->resolveScalarType($assign->expr);

if ($scalarType instanceof Type) {
$scalarTypes[] = $scalarType;
} else {
Expand Down Expand Up @@ -258,6 +260,11 @@ private function resolveScalarType(Expr $expr): ?Type
return new FloatType();
}

$exprType = $this->nodeTypeResolver->getNativeType($expr);
if ($exprType->isScalar()->yes()) {
return $exprType;
}

return null;
}
}
Loading