Skip to content

Commit

Permalink
[DeadCode] Add fixture to remove already known more specific type (#6309
Browse files Browse the repository at this point in the history
)

* [DeadCode] Add fixture to remove already known more specific type

* Remove more specific property type
  • Loading branch information
TomasVotruba authored Sep 16, 2024
1 parent 04bd2bb commit bbdeb0f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeConstantFloatType;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeMoreSpecificObject;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeObject;

final class RemoveSpecificTypedProperty
{
/**
* @var SomeObject
*/
public SomeMoreSpecificObject $name;
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeConstantFloatType;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeMoreSpecificObject;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeObject;

final class RemoveSpecificTypedProperty
{
public SomeMoreSpecificObject $name;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\DifferentTypeProperty;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeMoreSpecificObject;

final class SkipDifferentTypedProperty
{
/**
* @var DifferentTypeProperty
*/
public SomeMoreSpecificObject $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\DifferentTypeProperty;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeMoreSpecificObject;
use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\SomeObject;

final class SkipMoreSpecificTypeInDocblock
{
/**
* @var SomeMoreSpecificObject
*/
public SomeObject $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source;

final class DifferentTypeProperty
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source;

final class SomeMoreSpecificObject extends SomeObject
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source;

class SomeObject
{

}
7 changes: 7 additions & 0 deletions rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
Expand Down Expand Up @@ -38,6 +39,12 @@ public function isDead(VarTagValueNode $varTagValueNode, Property $property): bo
return ! $docType instanceof IntersectionType;
}

if ($propertyType instanceof ObjectType && $docType instanceof ObjectType) {
// more specific type is already in the property
return $docType->isSuperTypeOf($propertyType)
->yes();
}

if ($this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual(
$property->type,
$varTagValueNode->type,
Expand Down

0 comments on commit bbdeb0f

Please sign in to comment.