Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php74] Do not remove Array Shape docblock on TypedPropertyRector #1466

Merged
merged 5 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Fixture;

final class DoNotRemoveArrayShapeDocblock
{
const FIRST = 'a';
const SECOND = 'b';

/**
* @var array{a: \stdClass[], b: \DateTime[]}
*/
private $property = [
self::FIRST => [],
self::SECOND => [],
];

public function getProperty()
{
if ($this->property['a'] === [] && $this->property['b'] === []) {
$this->property['a'] = [new stdClass()];
$this->property['b'] = [new \DateTime()];
}

return $this->property;
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\Fixture;

final class DoNotRemoveArrayShapeDocblock
{
const FIRST = 'a';
const SECOND = 'b';

/**
* @var array{a: \stdClass[], b: \DateTime[]}
*/
private array $property = [
self::FIRST => [],
self::SECOND => [],
];

public function getProperty()
{
if ($this->property['a'] === [] && $this->property['b'] === []) {
$this->property['a'] = [new stdClass()];
$this->property['b'] = [new \DateTime()];
}

return $this->property;
}
}

?>
8 changes: 6 additions & 2 deletions rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -103,7 +103,11 @@ private function isNonBasicArrayType(Expression | Param | Property $node, VarTag

private function isArrayTypeNode(VarTagValueNode $varTagValueNode): bool
{
return $varTagValueNode->type instanceof ArrayTypeNode;
return in_array(
$varTagValueNode->type::class,
[SpacingAwareArrayTypeNode::class, ArrayShapeNode::class],
true
);
}

private function isArrayOfClass(Node $node, SpacingAwareArrayTypeNode $spacingAwareArrayTypeNode): bool
Expand Down