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 @template tag on RemoveUselessVarTagRector #6396

Merged
merged 8 commits into from
Oct 26, 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,26 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\Properties;

final class NonExistingObject
{
/** @var SomeNonExistingObject */
private Properties|null $properties;
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\Properties;

final class NonExistingObject
{
private Properties|null $properties;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

use Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source\Properties;

/**
* @template TProperties of Properties|null
*/
final class SkipTemplateTag
{
/** @var TProperties */
private Properties|null $properties;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Source;

interface Properties{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace MyNamespace;

class MylegacyClass
{
}
38 changes: 37 additions & 1 deletion rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\Tag\TemplateTag;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\StaticTypeMapper\Naming\NameScopeFactory;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
Expand All @@ -29,14 +35,15 @@
public function __construct(
private ReflectionProvider $reflectionProvider,
private UseImportsResolver $useImportsResolver,
private NameScopeFactory $nameScopeFactory
) {
}

public function narrowToFullyQualifiedOrAliasedObjectType(
Node $node,
ObjectType $objectType,
Scope|null $scope
): TypeWithClassName | NonExistingObjectType | UnionType | MixedType {
): TypeWithClassName | NonExistingObjectType | UnionType | MixedType | TemplateType {
$uses = $this->useImportsResolver->resolve();

$aliasedObjectType = $this->matchAliasedObjectType($objectType, $uses);
Expand Down Expand Up @@ -66,6 +73,35 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
}
}

if ($scope instanceof Scope) {
$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection) {
$templateTags = $classReflection->getTemplateTags();
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node);
$templateTypeScope = $nameScope->getTemplateTypeScope();

if (! $templateTypeScope instanceof TemplateTypeScope) {
// invalid type
return new NonExistingObjectType($className);
}

// only support single @template for now
if (count($templateTags) !== 1) {
// invalid type
return new NonExistingObjectType($className);
}

/** @var TemplateTag $currentTemplateTag */
$currentTemplateTag = current($templateTags);
return TemplateTypeFactory::create(
$templateTypeScope,
$currentTemplateTag->getName(),
$currentTemplateTag->getBound(),
$currentTemplateTag->getVariance()
);
}
}

// invalid type
return new NonExistingObjectType($className);
}
Expand Down
5 changes: 5 additions & 0 deletions src/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
Expand Down Expand Up @@ -75,6 +76,10 @@ public function arePhpParserAndPhpStanPhpDocTypesEqual(
$node
);

if ($phpStanDocType instanceof TemplateType) {
return false;
}

if (! $this->areTypesEqual($phpParserNodeType, $phpStanDocType) && $this->isSubtype(
$phpStanDocType,
$phpParserNodeType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public function isCandidate(mixed $value): bool;
/**
* @param T $value
*/
public function map($value): Expr;
public function map(mixed $value): Expr;
}