2021-01-30 21:16:37 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\PhpDoc;
|
2021-01-30 21:16:37 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
2023-08-10 12:16:13 +00:00
|
|
|
use PHPStan\Type\IntersectionType;
|
2023-12-03 11:18:10 +00:00
|
|
|
use PHPStan\Type\TypeCombinator;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\UnionType;
|
|
|
|
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2021-01-30 21:16:37 +00:00
|
|
|
final class DeadVarTagValueNodeAnalyzer
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
2021-01-30 21:16:37 +00:00
|
|
|
*/
|
|
|
|
private $typeComparator;
|
2021-12-15 14:50:00 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-12-15 14:50:00 +00:00
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(TypeComparator $typeComparator, StaticTypeMapper $staticTypeMapper)
|
2021-01-30 21:16:37 +00:00
|
|
|
{
|
|
|
|
$this->typeComparator = $typeComparator;
|
2021-12-15 14:50:00 +00:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2021-01-30 21:16:37 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function isDead(VarTagValueNode $varTagValueNode, Property $property) : bool
|
2021-01-30 21:16:37 +00:00
|
|
|
{
|
|
|
|
if ($property->type === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-01-30 21:16:37 +00:00
|
|
|
}
|
2023-09-02 15:59:59 +00:00
|
|
|
if ($varTagValueNode->description !== '') {
|
|
|
|
return \false;
|
|
|
|
}
|
2021-12-15 14:50:00 +00:00
|
|
|
// is strict type superior to doc type? keep strict type only
|
|
|
|
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
|
|
|
|
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);
|
2023-02-12 18:36:15 +00:00
|
|
|
if ($propertyType instanceof UnionType && !$docType instanceof UnionType) {
|
2023-08-10 12:12:18 +00:00
|
|
|
return !$docType instanceof IntersectionType;
|
2021-12-15 14:50:00 +00:00
|
|
|
}
|
2023-12-03 11:18:10 +00:00
|
|
|
if ($this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual($property->type, $varTagValueNode->type, $property)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $docType instanceof UnionType && $this->typeComparator->areTypesEqual(TypeCombinator::removeNull($docType), $propertyType);
|
2021-01-30 21:16:37 +00:00
|
|
|
}
|
|
|
|
}
|