Skip to content

Commit 6777f6d

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents c420818 + b70fb0f commit 6777f6d

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ parameters:
11401140
-
11411141
rawMessage: 'Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead.'
11421142
identifier: phpstanApi.instanceofType
1143-
count: 1
1143+
count: 2
11441144
path: src/Type/Generic/GenericObjectType.php
11451145

11461146
-

src/Type/Generic/GenericObjectType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ protected function recreate(string $className, array $types, ?Type $subtractedTy
405405

406406
public function changeSubtractedType(?Type $subtractedType): Type
407407
{
408+
$result = parent::changeSubtractedType($subtractedType);
409+
410+
// Parent handles sealed type exhaustiveness (returning NeverType when all
411+
// allowed subtypes are subtracted, or a single remaining subtype).
412+
if (!$result instanceof ObjectType || $result->getClassName() !== $this->getClassName()) {
413+
return $result;
414+
}
415+
408416
return new self($this->getClassName(), $this->types, $subtractedType, null, $this->variances);
409417
}
410418

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,12 @@ public function testBug12241(): void
459459
$this->analyse([__DIR__ . '/data/bug-12241.php'], []);
460460
}
461461

462+
#[RequiresPhp('>= 8.0')]
463+
public function testBug14412(): void
464+
{
465+
$this->analyse([__DIR__ . '/data/bug-14412.php'], []);
466+
}
467+
462468
#[RequiresPhp('>= 8.0')]
463469
public function testBug13029(): void
464470
{
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14412;
6+
7+
/**
8+
* @template-covariant T
9+
* @phpstan-sealed BarCov|BazCov
10+
*/
11+
abstract class FooCov {}
12+
13+
/**
14+
* @template-covariant T
15+
* @extends FooCov<T>
16+
*/
17+
final class BarCov extends FooCov {}
18+
19+
/**
20+
* @template-covariant T
21+
* @extends FooCov<T>
22+
*/
23+
final class BazCov extends FooCov {}
24+
25+
/** @param FooCov<string> $foo */
26+
function testTemplateCovariant(FooCov $foo): string {
27+
return match ($foo::class) {
28+
BarCov::class => 'bar',
29+
BazCov::class => 'baz',
30+
};
31+
}
32+
33+
/**
34+
* @template T
35+
* @phpstan-sealed BarInv|BazInv
36+
*/
37+
abstract class FooInv {}
38+
39+
/**
40+
* @template T
41+
* @extends FooInv<T>
42+
*/
43+
final class BarInv extends FooInv {}
44+
45+
/**
46+
* @template T
47+
* @extends FooInv<T>
48+
*/
49+
final class BazInv extends FooInv {}
50+
51+
/** @param FooInv<covariant string> $foo */
52+
function testCovariantParam(FooInv $foo): string {
53+
return match ($foo::class) {
54+
BarInv::class => 'bar',
55+
BazInv::class => 'baz',
56+
};
57+
}

0 commit comments

Comments
 (0)