Skip to content

Commit 60d8836

Browse files
committed
support for handling union types
1 parent b09cd1e commit 60d8836

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

@@ -30,26 +31,36 @@ public function __construct(MessageMapFactory $symfonyMessageMapFactory)
3031

3132
public function getType(Expr $expr, Scope $scope): ?Type
3233
{
33-
if ($this->isSupported($expr, $scope)) {
34-
$args = $expr->getArgs();
35-
if (count($args) !== 1) {
36-
return null;
37-
}
34+
if (!$this->isSupported($expr, $scope)) {
35+
return null;
36+
}
37+
38+
$args = $expr->getArgs();
39+
if (count($args) !== 1) {
40+
return null;
41+
}
3842

39-
$arg = $args[0]->value;
40-
$argClassNames = $scope->getType($arg)->getObjectClassNames();
43+
$arg = $args[0]->value;
44+
$argClassNames = $scope->getType($arg)->getObjectClassNames();
4145

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

46-
if (!is_null($returnType)) {
47-
return $returnType;
48-
}
56+
if (is_null($returnType)) {
57+
return null;
4958
}
59+
60+
$returnTypes[] = $returnType;
5061
}
5162

52-
return null;
63+
return TypeCombinator::union(...$returnTypes);
5364
}
5465

5566
private function getMessageMap(): MessageMap

src/Type/Symfony/MessengerHandleTraitWrapperReturnTypeExtension.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Symfony\MessageMapFactory;
1212
use PHPStan\Type\ExpressionTypeResolverExtension;
1313
use PHPStan\Type\Type;
14+
use PHPStan\Type\TypeCombinator;
1415
use function count;
1516
use function in_array;
1617
use function is_null;
@@ -59,16 +60,23 @@ public function getType(Expr $expr, Scope $scope): ?Type
5960
$arg = $args[0]->value;
6061
$argClassNames = $scope->getType($arg)->getObjectClassNames();
6162

62-
if (count($argClassNames) === 1) {
63+
if (count($argClassNames) === 0) {
64+
return null;
65+
}
66+
67+
$returnTypes = [];
68+
foreach ($argClassNames as $argClassName) {
6369
$messageMap = $this->getMessageMap();
64-
$returnType = $messageMap->getTypeForClass($argClassNames[0]);
70+
$returnType = $messageMap->getTypeForClass($argClassName);
6571

66-
if (!is_null($returnType)) {
67-
return $returnType;
72+
if (is_null($returnType)) {
73+
return null;
6874
}
75+
76+
$returnTypes[] = $returnType;
6977
}
7078

71-
return null;
79+
return TypeCombinator::union(...$returnTypes);
7280
}
7381

7482
/**
@@ -88,11 +96,21 @@ private function isSupported(Expr $expr, Scope $scope): bool
8896
$varType = $scope->getType($expr->var);
8997
$classNames = $varType->getObjectClassNames();
9098

91-
if (count($classNames) !== 1) {
99+
if (count($classNames) === 0) {
92100
return false;
93101
}
94102

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

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

0 commit comments

Comments
 (0)