Skip to content

Commit c4ede85

Browse files
Fix
1 parent 6ead4dd commit c4ede85

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
@@ -1422,7 +1422,7 @@ parameters:
14221422
-
14231423
message: '#^Doing instanceof PHPStan\\Type\\ObjectType is error\-prone and deprecated\. Use Type\:\:isObject\(\) or Type\:\:getObjectClassNames\(\) instead\.$#'
14241424
identifier: phpstanApi.instanceofType
1425-
count: 6
1425+
count: 3
14261426
path: src/Type/ObjectType.php
14271427

14281428
-

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;
@@ -1556,14 +1555,16 @@ private function getInterfaces(): array
15561555
public function tryRemove(Type $typeToRemove): ?Type
15571556
{
15581557
foreach (UnionType::EQUAL_UNION_CLASSES as $baseClass => $classes) {
1559-
if ($this->getClassName() === $baseClass && $typeToRemove instanceof ObjectType) {
1560-
foreach ($classes as $index => $class) {
1561-
if ($typeToRemove->getClassName() === $class) {
1562-
unset($classes[$index]);
1563-
return TypeCombinator::union(
1564-
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes)
1565-
);
1566-
}
1558+
if ($this->getClassName() !== $baseClass || !($typeToRemove instanceof ObjectType)) {
1559+
continue;
1560+
}
1561+
1562+
foreach ($classes as $index => $class) {
1563+
if ($typeToRemove->getClassName() === $class) {
1564+
unset($classes[$index]);
1565+
return TypeCombinator::union(
1566+
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes),
1567+
);
15671568
}
15681569
}
15691570
}

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

@@ -196,13 +195,15 @@ public function getConstantStrings(): array
196195
public function accepts(Type $type, bool $strictTypes): AcceptsResult
197196
{
198197
foreach (self::EQUAL_UNION_CLASSES as $baseClass => $classes) {
199-
if ($type->equals(new ObjectType($baseClass))) {
200-
$union = TypeCombinator::union(
201-
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes)
202-
);
203-
if ($this->accepts($union, $strictTypes)->yes()) {
204-
return AcceptsResult::createYes();
205-
}
198+
if (!$type->equals(new ObjectType($baseClass))) {
199+
continue;
200+
}
201+
202+
$union = TypeCombinator::union(
203+
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes),
204+
);
205+
if ($this->accepts($union, $strictTypes)->yes()) {
206+
return AcceptsResult::createYes();
206207
}
207208
}
208209

0 commit comments

Comments
 (0)