Skip to content

Commit 428ba86

Browse files
committed
fix class-string<*> being supertype of any string literal
1 parent a849f06 commit 428ba86

4 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/Type/Generic/GenericClassStringType.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
9494
if ($type instanceof ConstantStringType) {
9595
$genericType = $this->type;
9696
if ($genericType instanceof MixedType) {
97-
return TrinaryLogic::createYes();
97+
return $type->isClassStringType();
9898
}
9999

100100
if ($genericType instanceof StaticType) {

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,12 @@ public function testBug10049(): void
12521252
$this->assertNoErrors($errors);
12531253
}
12541254

1255+
public function testBug10076(): void
1256+
{
1257+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10076.php');
1258+
$this->assertNoErrors($errors);
1259+
}
1260+
12551261
/**
12561262
* @param string[]|null $allAnalysedFiles
12571263
* @return Error[]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug10076;
4+
5+
/**
6+
* @template T
7+
*
8+
* @param 'object'|'array'|class-string<T> $type
9+
*
10+
* @return list<mixed>
11+
*/
12+
function result($type = 'object'): array
13+
{
14+
return [];
15+
}
16+
17+
result();
18+
result('object');
19+
result('array');
20+
result(\DateTime::class);

‎tests/PHPStan/Type/Generic/GenericClassStringTypeTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ public function dataIsSuperTypeOf(): array
155155
new ConstantStringType(IntegerType::class),
156156
TrinaryLogic::createMaybe(),
157157
],
158+
19 => [
159+
new GenericClassStringType(TemplateTypeFactory::create(
160+
TemplateTypeScope::createWithFunction('foo'),
161+
'T',
162+
null,
163+
TemplateTypeVariance::createInvariant(),
164+
)),
165+
new ConstantStringType('array'),
166+
TrinaryLogic::createNo(),
167+
],
158168
];
159169
}
160170

0 commit comments

Comments
 (0)