Skip to content

Commit

Permalink
Updated Rector to commit afe7c46c0387e27e1eae37cf76f8a3a4119a1d39
Browse files Browse the repository at this point in the history
rectorphp/rector-src@afe7c46 [DeadCode] Skip nullable @ template on RemoveUselessReturnTagRector (#6409)
  • Loading branch information
TomasVotruba committed Nov 5, 2024
1 parent 3b34bec commit b5c99e3
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
12 changes: 9 additions & 3 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
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\PhpDoc\Guard\TemplateTypeRemovalGuard;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\DeadCode\TypeNodeAnalyzer\MixedArrayTypeNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
Expand Down Expand Up @@ -60,7 +60,12 @@ final class DeadParamTagValueNodeAnalyzer
* @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)
/**
* @readonly
* @var \Rector\DeadCode\PhpDoc\Guard\TemplateTypeRemovalGuard
*/
private $templateTypeRemovalGuard;
public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard, StaticTypeMapper $staticTypeMapper, TemplateTypeRemovalGuard $templateTypeRemovalGuard)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->typeComparator = $typeComparator;
Expand All @@ -70,6 +75,7 @@ public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->standaloneTypeRemovalGuard = $standaloneTypeRemovalGuard;
$this->staticTypeMapper = $staticTypeMapper;
$this->templateTypeRemovalGuard = $templateTypeRemovalGuard;
}
public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $functionLike) : bool
{
Expand All @@ -84,7 +90,7 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
return \false;
}
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($paramTagValueNode->type, $functionLike);
if ($docType instanceof TemplateType) {
if (!$this->templateTypeRemovalGuard->isLegal($docType)) {
return \false;
}
if ($param->type instanceof Name && $this->nodeNameResolver->isName($param->type, 'object')) {
Expand Down
12 changes: 9 additions & 3 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
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;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\DeadCode\PhpDoc\Guard\StandaloneTypeRemovalGuard;
use Rector\DeadCode\PhpDoc\Guard\TemplateTypeRemovalGuard;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\DeadCode\TypeNodeAnalyzer\MixedArrayTypeNodeAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -54,14 +54,20 @@ final class DeadReturnTagValueNodeAnalyzer
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard, PhpDocTypeChanger $phpDocTypeChanger, StaticTypeMapper $staticTypeMapper)
/**
* @readonly
* @var \Rector\DeadCode\PhpDoc\Guard\TemplateTypeRemovalGuard
*/
private $templateTypeRemovalGuard;
public function __construct(TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard, PhpDocTypeChanger $phpDocTypeChanger, StaticTypeMapper $staticTypeMapper, TemplateTypeRemovalGuard $templateTypeRemovalGuard)
{
$this->typeComparator = $typeComparator;
$this->genericTypeNodeAnalyzer = $genericTypeNodeAnalyzer;
$this->mixedArrayTypeNodeAnalyzer = $mixedArrayTypeNodeAnalyzer;
$this->standaloneTypeRemovalGuard = $standaloneTypeRemovalGuard;
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->staticTypeMapper = $staticTypeMapper;
$this->templateTypeRemovalGuard = $templateTypeRemovalGuard;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
Expand All @@ -76,7 +82,7 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, $functionLike) :
return \false;
}
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($returnTagValueNode->type, $functionLike);
if ($docType instanceof TemplateType) {
if (!$this->templateTypeRemovalGuard->isLegal($docType)) {
return \false;
}
$scope = $functionLike->getAttribute(AttributeKey::SCOPE);
Expand Down
12 changes: 9 additions & 3 deletions rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

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;
use PHPStan\Type\UnionType;
use Rector\DeadCode\PhpDoc\Guard\TemplateTypeRemovalGuard;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
final class DeadVarTagValueNodeAnalyzer
Expand All @@ -24,10 +24,16 @@ final class DeadVarTagValueNodeAnalyzer
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(TypeComparator $typeComparator, StaticTypeMapper $staticTypeMapper)
/**
* @readonly
* @var \Rector\DeadCode\PhpDoc\Guard\TemplateTypeRemovalGuard
*/
private $templateTypeRemovalGuard;
public function __construct(TypeComparator $typeComparator, StaticTypeMapper $staticTypeMapper, TemplateTypeRemovalGuard $templateTypeRemovalGuard)
{
$this->typeComparator = $typeComparator;
$this->staticTypeMapper = $staticTypeMapper;
$this->templateTypeRemovalGuard = $templateTypeRemovalGuard;
}
public function isDead(VarTagValueNode $varTagValueNode, Property $property) : bool
{
Expand All @@ -40,7 +46,7 @@ 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) {
if (!$this->templateTypeRemovalGuard->isLegal($docType)) {
return \false;
}
if ($propertyType instanceof UnionType && !$docType instanceof UnionType) {
Expand Down
26 changes: 26 additions & 0 deletions rules/DeadCode/PhpDoc/Guard/TemplateTypeRemovalGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare (strict_types=1);
namespace Rector\DeadCode\PhpDoc\Guard;

use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\Generic\TemplateType;
final class TemplateTypeRemovalGuard
{
public function isLegal(Type $docType) : bool
{
// cover direct \PHPStan\Type\Generic\TemplateUnionType
if ($docType instanceof TemplateType) {
return \false;
}
// cover mixed template with mix from @template and non @template
$types = $docType instanceof UnionType ? $docType->getTypes() : [$docType];
foreach ($types as $type) {
if ($type instanceof TemplateType) {
return \false;
}
}
return \true;
}
}
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 = '1afc64719771eae635df76bf5f85b225b459b3d6';
public const PACKAGE_VERSION = 'afe7c46c0387e27e1eae37cf76f8a3a4119a1d39';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-11-04 22:15:08';
public const RELEASE_DATE = '2024-11-05 21:18:05';
/**
* @var int
*/
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,7 @@
'Rector\\DeadCode\\PhpDoc\\DeadReturnTagValueNodeAnalyzer' => $baseDir . '/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php',
'Rector\\DeadCode\\PhpDoc\\DeadVarTagValueNodeAnalyzer' => $baseDir . '/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php',
'Rector\\DeadCode\\PhpDoc\\Guard\\StandaloneTypeRemovalGuard' => $baseDir . '/rules/DeadCode/PhpDoc/Guard/StandaloneTypeRemovalGuard.php',
'Rector\\DeadCode\\PhpDoc\\Guard\\TemplateTypeRemovalGuard' => $baseDir . '/rules/DeadCode/PhpDoc/Guard/TemplateTypeRemovalGuard.php',
'Rector\\DeadCode\\PhpDoc\\TagRemover\\ParamTagRemover' => $baseDir . '/rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php',
'Rector\\DeadCode\\PhpDoc\\TagRemover\\ReturnTagRemover' => $baseDir . '/rules/DeadCode/PhpDoc/TagRemover/ReturnTagRemover.php',
'Rector\\DeadCode\\PhpDoc\\TagRemover\\VarTagRemover' => $baseDir . '/rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ class ComposerStaticInit4d4c37b878ce01a3ff505ba7def6aac7
'Rector\\DeadCode\\PhpDoc\\DeadReturnTagValueNodeAnalyzer' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php',
'Rector\\DeadCode\\PhpDoc\\DeadVarTagValueNodeAnalyzer' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php',
'Rector\\DeadCode\\PhpDoc\\Guard\\StandaloneTypeRemovalGuard' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/Guard/StandaloneTypeRemovalGuard.php',
'Rector\\DeadCode\\PhpDoc\\Guard\\TemplateTypeRemovalGuard' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/Guard/TemplateTypeRemovalGuard.php',
'Rector\\DeadCode\\PhpDoc\\TagRemover\\ParamTagRemover' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php',
'Rector\\DeadCode\\PhpDoc\\TagRemover\\ReturnTagRemover' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/TagRemover/ReturnTagRemover.php',
'Rector\\DeadCode\\PhpDoc\\TagRemover\\VarTagRemover' => __DIR__ . '/../..' . '/rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php',
Expand Down

0 comments on commit b5c99e3

Please sign in to comment.