Skip to content

Commit

Permalink
Updated Rector to commit 61a787e62541281bd5f3678fb2819d645c8f0055
Browse files Browse the repository at this point in the history
rectorphp/rector-src@61a787e [DeadCode] Check TemplateType instance check on Dead*TagValueNodeAnalyzer (#6400)
  • Loading branch information
TomasVotruba committed Oct 26, 2024
1 parent 6e934f1 commit 8baae8f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
14 changes: 13 additions & 1 deletion rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use PhpParser\Node\Param;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\Generic\TemplateType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\DeadCode\PhpDoc\Guard\StandaloneTypeRemovalGuard;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\DeadCode\TypeNodeAnalyzer\MixedArrayTypeNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\NodeAnalyzer\ParamAnalyzer;
final class DeadParamTagValueNodeAnalyzer
{
Expand Down Expand Up @@ -53,7 +55,12 @@ final class DeadParamTagValueNodeAnalyzer
* @var \Rector\DeadCode\PhpDoc\Guard\StandaloneTypeRemovalGuard
*/
private $standaloneTypeRemovalGuard;
public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard)
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard, StaticTypeMapper $staticTypeMapper)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->typeComparator = $typeComparator;
Expand All @@ -62,6 +69,7 @@ public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $
$this->paramAnalyzer = $paramAnalyzer;
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->standaloneTypeRemovalGuard = $standaloneTypeRemovalGuard;
$this->staticTypeMapper = $staticTypeMapper;
}
public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $functionLike) : bool
{
Expand All @@ -75,6 +83,10 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
if ($paramTagValueNode->description !== '') {
return \false;
}
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($paramTagValueNode->type, $functionLike);
if ($docType instanceof TemplateType) {
return \false;
}
if ($param->type instanceof Name && $this->nodeNameResolver->isName($param->type, 'object')) {
return $paramTagValueNode->type instanceof IdentifierTypeNode && (string) $paramTagValueNode->type === 'object';
}
Expand Down
5 changes: 5 additions & 0 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
Expand Down Expand Up @@ -74,6 +75,10 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, $functionLike) :
if ($returnTagValueNode->description !== '') {
return \false;
}
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($returnTagValueNode->type, $functionLike);
if ($docType instanceof TemplateType) {
return \false;
}
$scope = $functionLike->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope && $scope->isInTrait() && $returnTagValueNode->type instanceof ThisTypeNode) {
return \false;
Expand Down
4 changes: 4 additions & 0 deletions rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -39,6 +40,9 @@ public function isDead(VarTagValueNode $varTagValueNode, Property $property) : b
// is strict type superior to doc type? keep strict type only
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);
if ($docType instanceof TemplateType) {
return \false;
}
if ($propertyType instanceof UnionType && !$docType instanceof UnionType) {
return !$docType instanceof IntersectionType;
}
Expand Down
4 changes: 0 additions & 4 deletions rules/Php80/Rector/FunctionLike/MixedTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Stmt\Function_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand Down Expand Up @@ -141,9 +140,6 @@ private function refactorParamTypes($functionLike, PhpDocInfo $phpDocInfo) : voi
if (!$paramType instanceof MixedType) {
continue;
}
if ($paramType instanceof TemplateType) {
continue;
}
$this->hasChanged = \true;
$param->type = new Identifier('mixed');
if ($param->flags !== 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'aa7c931b47d3983290d2bec9916862099752e834';
public const PACKAGE_VERSION = '61a787e62541281bd5f3678fb2819d645c8f0055';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-10-26 19:06:05';
public const RELEASE_DATE = '2024-10-26 22:09:03';
/**
* @var int
*/
Expand Down
15 changes: 12 additions & 3 deletions src/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function __construct(TypeHasher $typeHasher, TypeNormalizer $typeNormaliz
}
public function areTypesEqual(Type $firstType, Type $secondType) : bool
{
$firstType = $this->normalizeTemplateType($firstType);
$secondType = $this->normalizeTemplateType($secondType);
$firstTypeHash = $this->typeHasher->createTypeHash($firstType);
$secondTypeHash = $this->typeHasher->createTypeHash($secondType);
if ($firstTypeHash === $secondTypeHash) {
Expand All @@ -89,9 +91,6 @@ public function arePhpParserAndPhpStanPhpDocTypesEqual(Node $phpParserNode, Type
{
$phpParserNodeType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($phpParserNode);
$phpStanDocType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($phpStanDocTypeNode, $node);
if ($phpStanDocType instanceof TemplateType) {
return \false;
}
if (!$this->areTypesEqual($phpParserNodeType, $phpStanDocType) && $this->isSubtype($phpStanDocType, $phpParserNodeType)) {
return \false;
}
Expand All @@ -110,6 +109,8 @@ public function arePhpParserAndPhpStanPhpDocTypesEqual(Node $phpParserNode, Type
}
public function isSubtype(Type $checkedType, Type $mainType) : bool
{
$checkedType = $this->normalizeTemplateType($checkedType);
$mainType = $this->normalizeTemplateType($mainType);
if ($mainType instanceof MixedType) {
return \false;
}
Expand All @@ -121,6 +122,14 @@ public function isSubtype(Type $checkedType, Type $mainType) : bool
}
return $this->arrayTypeComparator->isSubtype($checkedType, $mainType);
}
/**
* unless it by ref, object param has its own life vs redefined variable
* see https://3v4l.org/dI5Pe vs https://3v4l.org/S8i71
*/
private function normalizeTemplateType(Type $type) : Type
{
return $type instanceof TemplateType ? $type->getBound() : $type;
}
private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType) : bool
{
if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType) {
Expand Down
2 changes: 1 addition & 1 deletion vendor/nette/utils/src/Utils/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function offsetUnset($index) : void
}
/**
* Prepends a item.
* @param mixed $value
* @param T $value
*/
public function prepend($value) : void
{
Expand Down

0 comments on commit 8baae8f

Please sign in to comment.