Skip to content

Fix intersection with substracted #4132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
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
5 changes: 5 additions & 0 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ private static function unionWithSubtractedType(
$subtractedType = $type->getSubtractedType() === null
? $subtractedType
: self::union($type->getSubtractedType(), $subtractedType);

$subtractedType = self::intersect(
$type->getTypeWithoutSubtractedType(),
$subtractedType,
);
if ($subtractedType instanceof NeverType) {
$subtractedType = null;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4127,6 +4127,22 @@ public static function dataIntersect(): iterable
IntersectionType::class,
'lowercase-string&uppercase-string',
],
[
[
new ObjectType(DateTime::class),
new MixedType(subtractedType: new NullType()),
],
ObjectType::class,
'DateTime',
],
[
[
new ObjectWithoutClassType(),
new MixedType(subtractedType: new NullType()),
],
ObjectWithoutClassType::class,
'object',
],
];

if (PHP_VERSION_ID < 80100) {
Expand Down
Loading