Skip to content

Commit 6c3da90

Browse files
committed
support for handling union types
1 parent 63795fa commit 6c3da90

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

src/Type/Symfony/MessengerHandleTraitReturnTypeExtension.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Symfony\MessageMapFactory;
1111
use PHPStan\Type\ExpressionTypeResolverExtension;
1212
use PHPStan\Type\Type;
13+
use PHPStan\Type\TypeCombinator;
1314
use function count;
1415
use function is_null;
1516

@@ -32,26 +33,36 @@ public function __construct(MessageMapFactory $symfonyMessageMapFactory)
3233

3334
public function getType(Expr $expr, Scope $scope): ?Type
3435
{
35-
if ($this->isSupported($expr, $scope)) {
36-
$args = $expr->getArgs();
37-
if (count($args) !== 1) {
38-
return null;
39-
}
36+
if (!$this->isSupported($expr, $scope)) {
37+
return null;
38+
}
39+
40+
$args = $expr->getArgs();
41+
if (count($args) !== 1) {
42+
return null;
43+
}
4044

41-
$arg = $args[0]->value;
42-
$argClassNames = $scope->getType($arg)->getObjectClassNames();
45+
$arg = $args[0]->value;
46+
$argClassNames = $scope->getType($arg)->getObjectClassNames();
4347

44-
if (count($argClassNames) === 1) {
45-
$messageMap = $this->getMessageMap();
46-
$returnType = $messageMap->getTypeForClass($argClassNames[0]);
48+
if (count($argClassNames) === 0) {
49+
return null;
50+
}
51+
52+
$messageMap = $this->getMessageMap();
53+
54+
$returnTypes = [];
55+
foreach ($argClassNames as $argClassName) {
56+
$returnType = $messageMap->getTypeForClass($argClassName);
4757

48-
if (!is_null($returnType)) {
49-
return $returnType;
50-
}
58+
if (is_null($returnType)) {
59+
return null;
5160
}
61+
62+
$returnTypes[] = $returnType;
5263
}
5364

54-
return null;
65+
return TypeCombinator::union(...$returnTypes);
5566
}
5667

5768
private function getMessageMap(): MessageMap

src/Type/Symfony/MessengerHandleTraitWrapperReturnTypeExtension.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Symfony\MessageMapFactory;
1313
use PHPStan\Type\ExpressionTypeResolverExtension;
1414
use PHPStan\Type\Type;
15+
use PHPStan\Type\TypeCombinator;
1516
use function count;
1617
use function in_array;
1718
use function is_null;
@@ -62,16 +63,23 @@ public function getType(Expr $expr, Scope $scope): ?Type
6263
$arg = $args[0]->value;
6364
$argClassNames = $scope->getType($arg)->getObjectClassNames();
6465

65-
if (count($argClassNames) === 1) {
66+
if (count($argClassNames) === 0) {
67+
return null;
68+
}
69+
70+
$returnTypes = [];
71+
foreach ($argClassNames as $argClassName) {
6672
$messageMap = $this->getMessageMap();
67-
$returnType = $messageMap->getTypeForClass($argClassNames[0]);
73+
$returnType = $messageMap->getTypeForClass($argClassName);
6874

69-
if (!is_null($returnType)) {
70-
return $returnType;
75+
if (is_null($returnType)) {
76+
return null;
7177
}
78+
79+
$returnTypes[] = $returnType;
7280
}
7381

74-
return null;
82+
return TypeCombinator::union(...$returnTypes);
7583
}
7684

7785
/**
@@ -87,11 +95,21 @@ private function isSupported(Expr $expr, Scope $scope): bool
8795
$varType = $scope->getType($expr->var);
8896
$classNames = $varType->getObjectClassNames();
8997

90-
if (count($classNames) !== 1) {
98+
if (count($classNames) === 0) {
9199
return false;
92100
}
93101

94-
$className = $classNames[0];
102+
foreach ($classNames as $className) {
103+
if (!$this->isClassMethodSupported($className, $methodName)) {
104+
return false;
105+
}
106+
}
107+
108+
return true;
109+
}
110+
111+
private function isClassMethodSupported(string $className, string $methodName): bool
112+
{
95113
$classMethodCombination = $className . '::' . $methodName;
96114

97115
// Check if this exact class::method combination is configured

0 commit comments

Comments
 (0)