Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,18 @@ private static function unionWithSubtractedType(
return $type;
}

if ($subtractedType instanceof SubtractableType) {
$withoutSubtracted = $subtractedType->getTypeWithoutSubtractedType();
if ($withoutSubtracted->isSuperTypeOf($type)->yes()) {
$subtractedSubtractedType = $subtractedType->getSubtractedType();
if ($subtractedSubtractedType === null) {
return new NeverType();
}

return self::intersect($type, $subtractedSubtractedType);
}
}

if ($type instanceof SubtractableType) {
$subtractedType = $type->getSubtractedType() === null
? $subtractedType
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/subtracted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Substracted;


use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param mixed $date
* @param bool $foo
*/
public function sayHello($date, $foo): void
{
if(is_object($date)){

} else {
assertType('mixed~object', $date);

if ($foo) {
$date = new \stdClass();
}
assertType('mixed~object~stdClass', $date);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this means. There should be parentheses to disambiguate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already the current behavior https://phpstan.org/r/0850c2cb-3c81-4451-b2f6-b570d1d41a30

I'll see what I can do


if (is_object($date)) {
assertType('stdClass', $date);
}
}
}
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4143,6 +4143,14 @@ public static function dataIntersect(): iterable
ObjectWithoutClassType::class,
'object',
],
[
[
new MixedType(subtractedType: new ObjectWithoutClassType(new ObjectType('stdClass'))),
new ObjectWithoutClassType(),
],
ObjectType::class,
'stdClass',
],
];

if (PHP_VERSION_ID < 80100) {
Expand Down
Loading