Skip to content

Commit

Permalink
fix class-string<*> being supertype of any string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed Oct 31, 2023
1 parent a849f06 commit 428ba86
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
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

0 comments on commit 428ba86

Please sign in to comment.