Skip to content

Commit

Permalink
[DeadCode] Skip nullable @ template on RemoveUselessReturnTagRector (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Nov 5, 2024
1 parent 1afc647 commit afe7c46
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Source\Performance;

/**
* @template TPerformance of Performance
*/
interface SkipNullableTemplateTag {
/** @return TPerformance|null */
public function performance(): Performance|null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Source;

interface Performance {}
7 changes: 4 additions & 3 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,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 All @@ -30,7 +30,8 @@ public function __construct(
private ParamAnalyzer $paramAnalyzer,
private PhpDocTypeChanger $phpDocTypeChanger,
private StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard,
private StaticTypeMapper $staticTypeMapper
private StaticTypeMapper $staticTypeMapper,
private TemplateTypeRemovalGuard $templateTypeRemovalGuard
) {
}

Expand All @@ -53,7 +54,7 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
$paramTagValueNode->type,
$functionLike
);
if ($docType instanceof TemplateType) {
if (! $this->templateTypeRemovalGuard->isLegal($docType)) {
return false;
}

Expand Down
7 changes: 4 additions & 3 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,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 All @@ -32,7 +32,8 @@ public function __construct(
private MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer,
private StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard,
private PhpDocTypeChanger $phpDocTypeChanger,
private StaticTypeMapper $staticTypeMapper
private StaticTypeMapper $staticTypeMapper,
private TemplateTypeRemovalGuard $templateTypeRemovalGuard,
) {
}

Expand All @@ -52,7 +53,7 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, ClassMethod|Funct
$returnTagValueNode->type,
$functionLike
);
if ($docType instanceof TemplateType) {
if (! $this->templateTypeRemovalGuard->isLegal($docType)) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,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;

Expand All @@ -19,6 +19,7 @@
public function __construct(
private TypeComparator $typeComparator,
private StaticTypeMapper $staticTypeMapper,
private TemplateTypeRemovalGuard $templateTypeRemovalGuard
) {
}

Expand All @@ -36,7 +37,7 @@ public function isDead(VarTagValueNode $varTagValueNode, Property $property): bo
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);

if ($docType instanceof TemplateType) {
if (! $this->templateTypeRemovalGuard->isLegal($docType)) {
return false;
}

Expand Down
33 changes: 33 additions & 0 deletions rules/DeadCode/PhpDoc/Guard/TemplateTypeRemovalGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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;
}
}

0 comments on commit afe7c46

Please sign in to comment.