mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-25 04:03:55 +01:00
skip closure override
This commit is contained in:
parent
813b78925a
commit
d308da40b7
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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'];
|
||||
|
@ -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);
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user