[AddArrayReturnDocTypeRector] Allow mixed[] and iterable<mixed>… (#2630)

[AddArrayReturnDocTypeRector] Allow mixed[] and iterable<mixed> in place of Rector's setting wrong infered types
This commit is contained in:
Tomas Votruba 2020-01-18 18:42:01 +01:00 committed by GitHub
commit 35cbc9f36a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 14 deletions

View File

@ -12,7 +12,6 @@ use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -131,7 +130,13 @@ PHP
return false;
}
return $this->isSpecificIterableType($currentPhpDocInfo);
$returnType = $currentPhpDocInfo->getReturnType();
if ($returnType instanceof ArrayType && $returnType->getItemType() instanceof MixedType) {
return true;
}
return $returnType instanceof IterableType;
}
private function shouldSkipType(Type $newType, ClassMethod $classMethod): bool
@ -209,16 +214,4 @@ PHP
return false;
}
private function isSpecificIterableType(PhpDocInfo $currentPhpDocInfo): bool
{
if (! $currentPhpDocInfo->getReturnType() instanceof IterableType) {
return false;
}
/** @var IterableType $iterableType */
$iterableType = $currentPhpDocInfo->getReturnType();
return ! $iterableType->getItemType() instanceof MixedType;
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
final class SkipMixedArray
{
/**
* @return mixed[]
*/
public function someMethod(): array
{
return [
42,
[42],
];
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
final class SkipMixedIterable
{
/**
* @return iterable<mixed>
*/
public function someDataProvider(): iterable
{
yield [42];
yield [[42]];
}
}