Fixes #3930 : TypedPropertyRector should not remove DocBlock comment when it specifies the values contained in an array (#4102)

This commit is contained in:
Abdul Malik Ikhsan 2020-09-02 19:01:45 +07:00 committed by GitHub
parent 108c6fd52f
commit 9465173276
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ rector-recipe.php
# testing
abz
/rector-temp-phpstan*.neon

View File

@ -16,6 +16,8 @@ use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareArrayTypeNode;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
@ -250,6 +252,14 @@ PHP
private function isNonBasicArrayType(Property $property, VarTagValueNode $varTagValueNode): bool
{
if ($varTagValueNode->type instanceof AttributeAwareUnionTypeNode) {
foreach ($varTagValueNode->type->types as $type) {
if ($type instanceof AttributeAwareArrayTypeNode && class_exists((string) $type->type)) {
return true;
}
}
}
if (! $this->isArrayTypeNode($varTagValueNode)) {
return false;
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;
final class DonotRemoveClassDocblockArray
{
/**
* @var \DateTime[]|null
*/
private $times2;
}
?>
-----
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\Fixture;
final class DonotRemoveClassDocblockArray
{
/**
* @var \DateTime[]|null
*/
private ?array $times2 = null;
}
?>