Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ public function tryRemove(Type $typeToRemove): ?Type
if ($classReflection->isFinal() && $genericObjectClassNames[0] === $typeToRemove->getValue()) {
return new NeverType();
}

if ($classReflection->getAllowedSubTypes() !== null) {
$objectTypeToRemove = new ObjectType($typeToRemove->getValue());
$remainingType = TypeCombinator::remove($generic, $objectTypeToRemove);
if ($remainingType instanceof NeverType) {
return new NeverType();
}

if (!$remainingType->equals($generic)) {
return new self($remainingType);
}
}
}
} elseif (count($genericObjectClassNames) > 1) {
$objectTypeToRemove = new ObjectType($typeToRemove->getValue());
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ public function testBug9534(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug12241(): void
{
$this->analyse([__DIR__ . '/data/bug-12241.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug13029(): void
{
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-12241.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug12241;

/**
* @phpstan-sealed Bar|Baz
*/
abstract class Foo{}

final class Bar extends Foo{}
final class Baz extends Foo{}

function (Foo $foo): string {
return match ($foo::class) {
Bar::class => 'Bar',
Baz::class => 'Baz',
};
};
Loading