skip closure override

This commit is contained in:
Tomas Votruba 2019-10-23 14:07:49 +02:00
parent 813b78925a
commit d308da40b7
4 changed files with 63 additions and 8 deletions

View File

@ -6,8 +6,10 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -98,7 +100,7 @@ PHP
[ReturnTypeDeclarationReturnTypeInferer::class]
);
if ($this->shouldSkipType($inferedType)) {
if ($this->shouldSkipType($inferedType, $node)) {
return null;
}
@ -122,16 +124,47 @@ PHP
return false;
}
private function shouldSkipType(Type $type): bool
private function shouldSkipType(Type $newType, ClassMethod $classMethod): bool
{
if (! $type instanceof ConstantArrayType) {
return false;
if (! $newType instanceof ArrayType && ! $newType instanceof UnionType) {
return true;
}
if (count($type->getValueTypes()) > self::MAX_NUMBER_OF_TYPES) {
return true;
if ($newType instanceof ArrayType) {
if ($this->isNewAndCurrentTypeBothCallable($newType, $classMethod)) {
return true;
}
}
if ($newType instanceof ConstantArrayType) {
if (count($newType->getValueTypes()) > self::MAX_NUMBER_OF_TYPES) {
return true;
}
}
return false;
}
private function isNewAndCurrentTypeBothCallable(ArrayType $newArrayType, ClassMethod $classMethod): bool
{
$currentPhpDocInfo = $this->getPhpDocInfo($classMethod);
if ($currentPhpDocInfo === null) {
return false;
}
$currentReturnType = $currentPhpDocInfo->getReturnType();
if (! $currentReturnType instanceof ArrayType) {
return false;
}
if (! $newArrayType->getItemType()->isCallable()->yes()) {
return false;
}
if (! $currentReturnType->getItemType()->isCallable()->yes()) {
return false;
}
return true;
}
}

View File

@ -32,6 +32,7 @@ final class AddArrayReturnDocTypeRectorTest extends AbstractRectorTestCase
// skip
yield [__DIR__ . '/Fixture/skip_too_many.php.inc'];
yield [__DIR__ . '/Fixture/skip_closure_callable_override.php.inc'];
yield [__DIR__ . '/Fixture/skip_shorten_class_name.php.inc'];
yield [__DIR__ . '/Fixture/skip_constructor.php.inc'];
yield [__DIR__ . '/Fixture/skip_inner_function_return.php.inc'];

View File

@ -0,0 +1,21 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
class SkipClosureCallableOverride
{
/**
* @return callable[]
*/
public function provide(): array
{
return [
/** @var mixed $items */
'flatten_array_count' => function (array $items): int {
$flattenItems = Arrays::flatten($items);
return count($flattenItems);
},
];
}
}

View File

@ -2,7 +2,7 @@
namespace Rector\TypeDeclaration\Tests\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class CallableType
final class CallableJustForTestType
{
private $code;
/**
@ -27,7 +27,7 @@ final class CallableType
namespace Rector\TypeDeclaration\Tests\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;
final class CallableType
final class CallableJustForTestType
{
/**
* @var callable