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 3 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
2 changes: 1 addition & 1 deletion src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
if ($type instanceof ConstantStringType) {
$genericType = $this->type;
if ($genericType instanceof MixedType) {
return TrinaryLogic::createYes();
return $type->isClassStringType();
}

if ($genericType instanceof StaticType) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,12 @@ public function testBug10049(): void
$this->assertNoErrors($errors);
}

public function testBug10076(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10076.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10076.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug10076;

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

result();
result('object');
result('array');
result(\DateTime::class);
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
41 changes: 41 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,19 @@ public function dataIntersect(): iterable
IntersectionType::class,
'array{a?: true, c?: true}&non-empty-array',
];
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