Skip to content

Commit

Permalink
[TypeDeclaration] Skip nullable generic typed on ReturnTypeDeclaratio…
Browse files Browse the repository at this point in the history
…nRector (#985)

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Oct 10, 2021
1 parent 3c0cb22 commit f6d0037
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;

/** @return array<array<mixed>>|null */
function foo1(int $a, float $b)
{
$bar = null;

if ($a > 0) {
for ($i = 0; $i < $a; $i++) {
$bar[] = [
'a' => $a,
'b' => $b,
];
}
}

return $bar;
}

/** @return array{a: int, b: float}|null */
function foo2(int $a, float $b)
{
$bar = null;

if ($a > 0) {
$bar = [
'a' => $a,
'b' => $b,
];
}

return $bar;
}

/** @return array<array{a: int, b: float}>|null */
function foo3(int $a, float $b)
{
$bar = null;

if ($a > 0) {
for ($i = 0; $i < $a; $i++) {
$bar[] = [
'a' => $a,
'b' => $b,
];
}
}

return $bar;
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;

/** @return array<array<mixed>>|null */
function foo1(int $a, float $b): ?array
{
$bar = null;

if ($a > 0) {
for ($i = 0; $i < $a; $i++) {
$bar[] = [
'a' => $a,
'b' => $b,
];
}
}

return $bar;
}

/** @return array{a: int, b: float}|null */
function foo2(int $a, float $b): ?array
{
$bar = null;

if ($a > 0) {
$bar = [
'a' => $a,
'b' => $b,
];
}

return $bar;
}

/** @return array<array{a: int, b: float}>|null */
function foo3(int $a, float $b): ?array
{
$bar = null;

if ($a > 0) {
for ($i = 0; $i < $a; $i++) {
$bar[] = [
'a' => $a,
'b' => $b,
];
}
}

return $bar;
}

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

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;

/** @return array<array<mixed>>|null */
function foo1(int $a, float $b): ?array
{
$bar = null;

if ($a > 0) {
for ($i = 0; $i < $a; $i++) {
$bar[] = [
'a' => $a,
'b' => $b,
];
}
}

return $bar;
}

/** @return array{a: int, b: float}|null */
function foo2(int $a, float $b): ?array
{
$bar = null;

if ($a > 0) {
$bar = [
'a' => $a,
'b' => $b,
];
}

return $bar;
}

/** @return array<array{a: int, b: float}>|null */
function foo3(int $a, float $b): ?array
{
$bar = null;

if ($a > 0) {
for ($i = 0; $i < $a; $i++) {
$bar[] = [
'a' => $a,
'b' => $b,
];
}
}

return $bar;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function normalize(Type $type): ArrayType | UnionType | Type
return $this->resolveStringType($value);
});

if ($type instanceof UnionType) {
if ($type instanceof UnionType && ! $this->isNullableType($type)) {
return $this->resolveClassStringInUnionType($type);
}

Expand All @@ -72,6 +73,22 @@ public function isAllGenericClassStringType(UnionType $unionType): bool
return true;
}

private function isNullableType(UnionType $unionType): bool
{
$types = $unionType->getTypes();
if (count($types) > 2) {
return false;
}

foreach ($types as $type) {
if ($type instanceof NullType) {
return true;
}
}

return false;
}

private function resolveArrayTypeWithUnionKeyType(ArrayType $arrayType): ArrayType
{
$itemType = $arrayType->getItemType();
Expand Down

0 comments on commit f6d0037

Please sign in to comment.