Skip to content

Commit 9f29868

Browse files
Fix
1 parent 5cd7a41 commit 9f29868

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ parameters:
12791279

12801280
-
12811281
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone and deprecated\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
1282-
count: 6
1282+
count: 3
12831283
path: src/Type/ObjectType.php
12841284

12851285
-

src/Type/ObjectType.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
4242
use PHPStan\Type\Traits\NonGenericTypeTrait;
4343
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
44-
use Throwable;
4544
use Traversable;
4645
use function array_key_exists;
4746
use function array_map;
@@ -1584,14 +1583,16 @@ private function getInterfaces(): array
15841583
public function tryRemove(Type $typeToRemove): ?Type
15851584
{
15861585
foreach (UnionType::EQUAL_UNION_CLASSES as $baseClass => $classes) {
1587-
if ($this->getClassName() === $baseClass && $typeToRemove instanceof ObjectType) {
1588-
foreach ($classes as $index => $class) {
1589-
if ($typeToRemove->getClassName() === $class) {
1590-
unset($classes[$index]);
1591-
return TypeCombinator::union(
1592-
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes)
1593-
);
1594-
}
1586+
if ($this->getClassName() !== $baseClass || !($typeToRemove instanceof ObjectType)) {
1587+
continue;
1588+
}
1589+
1590+
foreach ($classes as $index => $class) {
1591+
if ($typeToRemove->getClassName() === $class) {
1592+
unset($classes[$index]);
1593+
return TypeCombinator::union(
1594+
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes),
1595+
);
15951596
}
15961597
}
15971598
}

src/Type/UnionType.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use DateTimeImmutable;
77
use DateTimeInterface;
88
use Error;
9-
use Exception;
109
use Iterator;
1110
use IteratorAggregate;
1211
use PHPStan\Php\PhpVersion;
@@ -53,7 +52,7 @@ class UnionType implements CompoundType
5352

5453
public const EQUAL_UNION_CLASSES = [
5554
DateTimeInterface::class => [DateTimeImmutable::class, DateTime::class],
56-
Throwable::class => [Error::class, Exception::class],
55+
Throwable::class => [Error::class, Throwable::class],
5756
Traversable::class => [IteratorAggregate::class, Iterator::class],
5857
];
5958

@@ -204,13 +203,15 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
204203
public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
205204
{
206205
foreach (self::EQUAL_UNION_CLASSES as $baseClass => $classes) {
207-
if ($type->equals(new ObjectType($baseClass))) {
208-
$union = TypeCombinator::union(
209-
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes)
210-
);
211-
if ($this->accepts($union, $strictTypes)->yes()) {
212-
return AcceptsResult::createYes();
213-
}
206+
if (!$type->equals(new ObjectType($baseClass))) {
207+
continue;
208+
}
209+
210+
$union = TypeCombinator::union(
211+
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes),
212+
);
213+
if ($this->accepts($union, $strictTypes)->yes()) {
214+
return AcceptsResult::createYes();
214215
}
215216
}
216217

0 commit comments

Comments
 (0)