|
11 | 11 | use PHPStan\Reflection\ParameterReflection; |
12 | 12 | use PHPStan\Type\ClosureType; |
13 | 13 | use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 14 | +use PHPStan\Type\GeneralizePrecision; |
14 | 15 | use PHPStan\Type\Generic\GenericObjectType; |
15 | | -use PHPStan\Type\NeverType; |
16 | 16 | use PHPStan\Type\NullType; |
17 | 17 | use PHPStan\Type\ObjectType; |
18 | 18 | use PHPStan\Type\Type; |
19 | 19 | use PHPStan\Type\TypeCombinator; |
| 20 | +use PHPStan\Type\TypeTraverser; |
| 21 | +use PHPStan\Type\UnionType; |
20 | 22 | use Resolve\PHPStan\Reflection\PhpOption\LiftedParameterReflection; |
21 | 23 |
|
22 | 24 | /** |
@@ -61,24 +63,24 @@ static function (ParameterReflection $parameterReflection) { |
61 | 63 | $parametersAcceptor->getParameters(), |
62 | 64 | ); |
63 | 65 |
|
64 | | - $returnTypeIsOption = (new ObjectType('PhpOption\Option'))->isSuperTypeOf( |
| 66 | + $returnType = TypeTraverser::map( |
65 | 67 | $parametersAcceptor->getReturnType(), |
66 | | - ); |
| 68 | + static function (Type $type, callable $traverse) use ($noneValueType): Type { |
| 69 | + if ($type instanceof UnionType) { |
| 70 | + return $traverse($type); |
| 71 | + } |
| 72 | + |
| 73 | + if ((new ObjectType('PhpOption\Option'))->isSuperTypeOf($type)->yes()) { |
| 74 | + return $type; |
| 75 | + } |
67 | 76 |
|
68 | | - if ($returnTypeIsOption->yes() || $returnTypeIsOption->maybe()) { |
69 | | - $returnType = $parametersAcceptor->getReturnType(); |
70 | | - } else { |
71 | | - $returnType = new NeverType(); |
72 | | - } |
| 77 | + $type = TypeCombinator::remove(TypeUtil::replaceVoid($type), $noneValueType); |
73 | 78 |
|
74 | | - if ($returnTypeIsOption->no() || $returnTypeIsOption->maybe()) { |
75 | | - $returnType = TypeCombinator::union( |
76 | | - $returnType, |
77 | | - new GenericObjectType('PhpOption\Option', [ |
78 | | - TypeCombinator::remove($parametersAcceptor->getReturnType(), $noneValueType), |
79 | | - ]), |
80 | | - ); |
81 | | - } |
| 79 | + return new GenericObjectType('PhpOption\Option', [ |
| 80 | + $type->generalize(GeneralizePrecision::templateArgument()), |
| 81 | + ]); |
| 82 | + }, |
| 83 | + ); |
82 | 84 |
|
83 | 85 | return new ClosureType($parameters, $returnType); |
84 | 86 | }, |
|
0 commit comments