Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Skip nullable @ template on RemoveUselessReturnTagRector #6409

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}