Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/Rules/PhpDoc/VarTagTypeRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function array_key_exists;
use function array_map;
use function count;
use function is_string;
use function sprintf;
Expand Down Expand Up @@ -182,6 +185,17 @@ private function isValidSuperType(Scope $scope, Type $type, Type $varTagType, in
return $this->isSuperTypeOfVarType($scope, $type, $varTagType);
}

$type = TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof GenericObjectType) {
$type = $type->changeVariances(array_map(
static fn (TemplateTypeVariance $variance) => $variance->invariant() ? TemplateTypeVariance::createCovariant() : $variance,
$type->getVariances(),
));
}

return $traverse($type);
});

if ($type->isConstantArray()->yes()) {
if ($type->isIterableAtLeastOnce()->no()) {
$type = new ArrayType(new MixedType(), new MixedType());
Expand Down
8 changes: 8 additions & 0 deletions src/Type/Generic/GenericObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ protected function recreate(string $className, array $types, ?Type $subtractedTy
);
}

/**
* @param TemplateTypeVariance[] $variances
*/
public function changeVariances(array $variances): self
{
return $this->recreate($this->getClassName(), $this->getTypes(), $this->getSubtractedType(), $variances);
}

public function changeSubtractedType(?Type $subtractedType): Type
{
$result = parent::changeSubtractedType($subtractedType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public function testBug12457(): void
]);
}

public function testGenericSubtype(): void
public function testGenericSubtypeWithStrictCheck(): void
{
$this->checkTypeAgainstPhpDocType = true;
$this->strictWideningCheck = true;
Expand All @@ -604,6 +604,13 @@ public function testGenericSubtype(): void
]);
}

public function testGenericSubtypeWithoutStrictCheck(): void
{
$this->checkTypeAgainstPhpDocType = true;
$this->strictWideningCheck = false;
$this->analyse([__DIR__ . '/data/generic-subtype.php'], []);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to come up with an example that would still be reported even here. Like Foo<int> vs. Foo<string>.

}

public function testNewIsAlwaysFinalClass(): void
{
$this->checkTypeAgainstPhpDocType = true;
Expand Down
Loading