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

Fix class-string<*> being supertype of any string literal #2707

Open
wants to merge 8 commits into
base: 1.10.x
Choose a base branch
from
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
7 changes: 4 additions & 3 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ private function export(string $value): string
public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof GenericClassStringType) {
$genericType = $type->getGenericType();
if ($genericType instanceof MixedType) {
return TrinaryLogic::createMaybe();
if (!$this->isClassStringType()->yes()) {
return TrinaryLogic::createNo();
}

$genericType = $type->getGenericType();
if ($genericType instanceof StaticType) {
$genericType = $genericType->getStaticObjectType();
}
Expand Down
11 changes: 1 addition & 10 deletions src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
Expand Down Expand Up @@ -93,10 +92,6 @@ public function isSuperTypeOf(Type $type): TrinaryLogic

if ($type instanceof ConstantStringType) {
$genericType = $this->type;
if ($genericType instanceof MixedType) {
return TrinaryLogic::createYes();
}

if ($genericType instanceof StaticType) {
$genericType = $genericType->getStaticObjectType();
}
Expand All @@ -113,11 +108,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
$isSuperType = $genericType->isSuperTypeOf($objectType);
}

if (!$type->isClassStringType()->yes()) {
$isSuperType = $isSuperType->and(TrinaryLogic::createMaybe());
}

return $isSuperType;
return $isSuperType->and($type->isClassStringType());
} elseif ($type instanceof self) {
return $this->type->isSuperTypeOf($type->type);
} elseif ($type instanceof StringType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,4 +1450,9 @@ public function testBug9803(): void
$this->analyse([__DIR__ . '/data/bug-9803.php'], []);
}

public function testBug10076(): void
{
$this->analyse([__DIR__ . '/data/bug-10076.php'], []);
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10076.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug10076;

/**
* @template T
*
* @param 'object'|'array'|class-string<T> $type
*
* @return list<mixed>
*/
function result($type = 'object'): array
{
return [];
}

function () {
result();
result('object');
result('array');
result(\DateTime::class);
};
10 changes: 10 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantStringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public function dataIsSuperTypeOf(): array
new GenericClassStringType(new StaticType($reflectionProvider->getClass(Throwable::class))),
TrinaryLogic::createMaybe(),
],
16 => [
new ConstantStringType('array'),
new GenericClassStringType(TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('foo'),
'T',
null,
TemplateTypeVariance::createInvariant(),
)),
TrinaryLogic::createNo(),
],
];
}

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Type/Generic/GenericClassStringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ public function dataIsSuperTypeOf(): array
new ConstantStringType(IntegerType::class),
TrinaryLogic::createMaybe(),
],
19 => [
new GenericClassStringType(TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('foo'),
'T',
null,
TemplateTypeVariance::createInvariant(),
)),
new ConstantStringType('array'),
TrinaryLogic::createNo(),
],
];
}

Expand Down
54 changes: 54 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,34 @@ public function dataUnion(): iterable
UnionType::class,
'array{a?: true, b: true}|(array{a?: true, c?: true}&non-empty-array)',
];

yield [
[
new ConstantStringType('array'),
new GenericClassStringType(TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('foo'),
'T',
null,
TemplateTypeVariance::createInvariant(),
)),
],
UnionType::class,
"'array'|class-string<T (function foo(), parameter)>",
];

yield [
[
new ConstantStringType(Exception::class),
new GenericClassStringType(TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('foo'),
'T',
null,
TemplateTypeVariance::createInvariant(),
)),
],
GenericClassStringType::class,
'class-string<T (function foo(), parameter)>',
];
}

/**
Expand Down Expand Up @@ -4134,6 +4162,32 @@ public function dataIntersect(): iterable
IntersectionType::class,
'array{a?: true, c?: true}&non-empty-array',
];
yield [
[
new ConstantStringType('array'),
new GenericClassStringType(TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('foo'),
'T',
null,
TemplateTypeVariance::createInvariant(),
)),
],
NeverType::class,
'*NEVER*=implicit',
];
yield [
[
new ConstantStringType(Exception::class),
new GenericClassStringType(TemplateTypeFactory::create(
TemplateTypeScope::createWithFunction('foo'),
'T',
null,
TemplateTypeVariance::createInvariant(),
)),
],
ConstantStringType::class,
"'Exception'",
Copy link
Member

Choose a reason for hiding this comment

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

Please test intersection with 'array' too - should be NeverType.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixing the test for phpstan/phpstan#6697 would make it 'array'&class-string<T (function foo(), parameter)> instead of never, because we would need to assume that 'array' may be name of a class PHPStan doesn't know about. What should I do about that?

];
}

/**
Expand Down