Skip to content
Closed
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: 5 additions & 2 deletions src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\SubtractableType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -222,12 +223,14 @@ public function tryRemove(Type $typeToRemove): ?Type

if ($classReflection->getAllowedSubTypes() !== null) {
$objectTypeToRemove = new ObjectType($typeToRemove->getValue());
$remainingType = TypeCombinator::remove($generic, $objectTypeToRemove);
$baseType = new ObjectType($genericObjectClassNames[0],
$generic instanceof SubtractableType ? $generic->getSubtractedType() : null);
$remainingType = TypeCombinator::remove($baseType, $objectTypeToRemove);
if ($remainingType instanceof NeverType) {
return new NeverType();
}

if (!$remainingType->equals($generic)) {
if (!$remainingType->equals($baseType)) {
return new self($remainingType);
}
}
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 @@ -453,6 +453,12 @@ public function testBug12241(): void
$this->analyse([__DIR__ . '/data/bug-12241.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testGenericSealedClassStringMatch(): void
{
$this->analyse([__DIR__ . '/data/match-generic-sealed-class-string.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug13029(): void
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace MatchGenericSealedClassString;

/**
* @template-covariant T
* @phpstan-sealed BarCov|BazCov
*/
abstract class FooCov {}

/** @template-covariant T @extends FooCov<T> */
final class BarCov extends FooCov {}

/** @template-covariant T @extends FooCov<T> */
final class BazCov extends FooCov {}

/** @param FooCov<string> $foo */
function testTemplateCovariant(FooCov $foo): string {
return match ($foo::class) {
BarCov::class => 'bar',
BazCov::class => 'baz',
};
}

/**
* @template T
* @phpstan-sealed BarInv|BazInv
*/
abstract class FooInv {}

/** @template T @extends FooInv<T> */
final class BarInv extends FooInv {}

/** @template T @extends FooInv<T> */
final class BazInv extends FooInv {}

/** @param FooInv<covariant string> $foo */
function testCovariantParam(FooInv $foo): string {
return match ($foo::class) {
BarInv::class => 'bar',
BazInv::class => 'baz',
};
}
Loading