[TypeDeclaration] Skip array shrapnels (#5016)

* [TypeDeclaration] Skip array shrapnels

* [ci-review] Rector Rectify

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2020-12-28 12:49:30 +01:00 committed by GitHub
parent 305388d45d
commit 96ad185d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,9 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantArrayType;
@ -158,6 +161,10 @@ CODE_SAMPLE
return true;
}
if ($this->hasArrayShapeNode($classMethod)) {
return true;
}
$currentPhpDocReturnType = $this->getNodeReturnPhpDocType($classMethod);
if ($currentPhpDocReturnType instanceof ArrayType && $currentPhpDocReturnType->getItemType() instanceof MixedType) {
return true;
@ -303,4 +310,24 @@ CODE_SAMPLE
$currentReturnType = $this->getNodeReturnPhpDocType($classMethod);
return $currentReturnType instanceof ArrayType;
}
private function hasArrayShapeNode(ClassMethod $classMethod): bool
{
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $classMethod->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}
$attributeAwareReturnTagValueNode = $phpDocInfo->getReturnTagValue();
if (! $attributeAwareReturnTagValueNode instanceof ReturnTagValueNode) {
return false;
}
if (! $attributeAwareReturnTagValueNode->type instanceof ArrayTypeNode) {
return false;
}
return $attributeAwareReturnTagValueNode->type->type instanceof ArrayShapeNode;
}
}