Skip to content

Commit f222ea6

Browse files
Improve TypeCombinator::unionWithSubtractedType
1 parent 9dbff97 commit f222ea6

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Type/TypeCombinator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ private static function unionWithSubtractedType(
540540
return $type;
541541
}
542542

543+
if ($subtractedType instanceof SubtractableType) {
544+
$withoutSubtracted = $subtractedType->getTypeWithoutSubtractedType();
545+
if ($withoutSubtracted->isSuperTypeOf($type)->yes()) {
546+
$subtractedSubtractedType = $subtractedType->getSubtractedType();
547+
if ($subtractedSubtractedType === null) {
548+
return new NeverType();
549+
}
550+
551+
return self::intersect($type, $subtractedSubtractedType);
552+
}
553+
}
554+
543555
if ($type instanceof SubtractableType) {
544556
$subtractedType = $type->getSubtractedType() === null
545557
? $subtractedType
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Substracted;
4+
5+
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
public function sayHello(mixed $date, bool $foo): void
11+
{
12+
if(is_object($date)){
13+
14+
} else {
15+
assertType('mixed~object', $date);
16+
17+
if ($foo) {
18+
$date = new \stdClass();
19+
}
20+
assertType('mixed~object~stdClass', $date);
21+
22+
if (is_object($date)) {
23+
assertType('stdClass', $date);
24+
}
25+
}
26+
}
27+
}

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,6 +4143,14 @@ public static function dataIntersect(): iterable
41434143
ObjectWithoutClassType::class,
41444144
'object',
41454145
],
4146+
[
4147+
[
4148+
new MixedType(subtractedType: new ObjectWithoutClassType(new ObjectType('stdClass'))),
4149+
new ObjectWithoutClassType(),
4150+
],
4151+
ObjectType::class,
4152+
'stdClass',
4153+
],
41464154
];
41474155

41484156
if (PHP_VERSION_ID < 80100) {

0 commit comments

Comments
 (0)