Skip to content

Commit c0bfae6

Browse files
Refactor TryRemove/Accepts for DateTime|DateTimeImmutable and Exception|Error
1 parent 78ac6f2 commit c0bfae6

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
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: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
use ArrayAccess;
66
use Closure;
77
use Countable;
8-
use DateTime;
9-
use DateTimeImmutable;
10-
use DateTimeInterface;
11-
use Error;
12-
use Exception;
138
use Iterator;
149
use IteratorAggregate;
1510
use PHPStan\Analyser\OutOfClassScope;
@@ -46,7 +41,6 @@
4641
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
4742
use PHPStan\Type\Traits\NonGenericTypeTrait;
4843
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
49-
use Throwable;
5044
use Traversable;
5145
use function array_key_exists;
5246
use function array_map;
@@ -1560,23 +1554,21 @@ private function getInterfaces(): array
15601554

15611555
public function tryRemove(Type $typeToRemove): ?Type
15621556
{
1563-
if ($this->getClassName() === DateTimeInterface::class) {
1564-
if ($typeToRemove instanceof ObjectType && $typeToRemove->getClassName() === DateTimeImmutable::class) {
1565-
return new ObjectType(DateTime::class);
1566-
}
1567-
1568-
if ($typeToRemove instanceof ObjectType && $typeToRemove->getClassName() === DateTime::class) {
1569-
return new ObjectType(DateTimeImmutable::class);
1570-
}
1571-
}
1557+
if ($typeToRemove instanceof ObjectType) {
1558+
foreach (UnionType::EQUAL_UNION_CLASSES as $baseClass => $classes) {
1559+
if ($this->getClassName() !== $baseClass) {
1560+
continue;
1561+
}
15721562

1573-
if ($this->getClassName() === Throwable::class) {
1574-
if ($typeToRemove instanceof ObjectType && $typeToRemove->getClassName() === Error::class) {
1575-
return new ObjectType(Exception::class); // phpcs:ignore SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException
1576-
}
1563+
foreach ($classes as $index => $class) {
1564+
if ($typeToRemove->getClassName() === $class) {
1565+
unset($classes[$index]);
15771566

1578-
if ($typeToRemove instanceof ObjectType && $typeToRemove->getClassName() === Exception::class) { // phpcs:ignore SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException
1579-
return new ObjectType(Error::class);
1567+
return TypeCombinator::union(
1568+
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes),
1569+
);
1570+
}
1571+
}
15801572
}
15811573
}
15821574

src/Type/UnionType.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use DateTime;
66
use DateTimeImmutable;
77
use DateTimeInterface;
8+
use Error;
9+
use Exception;
810
use PHPStan\Php\PhpVersion;
911
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1012
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
@@ -26,6 +28,7 @@
2628
use PHPStan\Type\Generic\TemplateTypeVariance;
2729
use PHPStan\Type\Generic\TemplateUnionType;
2830
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
31+
use Throwable;
2932
use function array_diff_assoc;
3033
use function array_fill_keys;
3134
use function array_map;
@@ -45,6 +48,11 @@ class UnionType implements CompoundType
4548

4649
use NonGeneralizableTypeTrait;
4750

51+
public const EQUAL_UNION_CLASSES = [
52+
DateTimeInterface::class => [DateTimeImmutable::class, DateTime::class],
53+
Throwable::class => [Error::class, Exception::class], // phpcs:ignore SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException
54+
];
55+
4856
private bool $sortedTypes = false;
4957

5058
/** @var array<int, string> */
@@ -183,14 +191,18 @@ public function getConstantStrings(): array
183191

184192
public function accepts(Type $type, bool $strictTypes): AcceptsResult
185193
{
186-
if (
187-
$type->equals(new ObjectType(DateTimeInterface::class))
188-
&& $this->accepts(
189-
new UnionType([new ObjectType(DateTime::class), new ObjectType(DateTimeImmutable::class)]),
190-
$strictTypes,
191-
)->yes()
192-
) {
193-
return AcceptsResult::createYes();
194+
foreach (self::EQUAL_UNION_CLASSES as $baseClass => $classes) {
195+
if (!$type->equals(new ObjectType($baseClass))) {
196+
continue;
197+
}
198+
199+
$union = TypeCombinator::union(
200+
...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes),
201+
);
202+
if ($this->accepts($union, $strictTypes)->yes()) {
203+
return AcceptsResult::createYes();
204+
}
205+
break;
194206
}
195207

196208
$result = AcceptsResult::createNo();

0 commit comments

Comments
 (0)