|
3 | 3 | namespace PHPStan\Reflection\SignatureMap; |
4 | 4 |
|
5 | 5 | use PhpParser\Node\AttributeGroup; |
| 6 | +use PhpParser\Node\Expr\ConstFetch; |
6 | 7 | use PhpParser\Node\Expr\Variable; |
7 | 8 | use PhpParser\Node\Scalar\String_; |
8 | 9 | use PhpParser\Node\Stmt\ClassConst; |
|
21 | 22 | use PHPStan\Type\MixedType; |
22 | 23 | use PHPStan\Type\ParserNodeTypeToPHPStanType; |
23 | 24 | use PHPStan\Type\Type; |
| 25 | +use PHPStan\Type\TypeCombinator; |
24 | 26 | use PHPStan\Type\TypehintHelper; |
25 | 27 | use ReflectionFunctionAbstract; |
26 | 28 | use function array_key_exists; |
@@ -400,10 +402,23 @@ private function getSignature( |
400 | 402 | throw new ShouldNotHappenException(); |
401 | 403 | } |
402 | 404 | $parameterType = ParserNodeTypeToPHPStanType::resolve($param->type, null); |
| 405 | + $phpDocParameterType = $phpDocParameterTypes[$name->name] ?? null; |
| 406 | + |
| 407 | + if ($param->default instanceof ConstFetch) { |
| 408 | + $constName = (string) $param->default->name; |
| 409 | + $loweredConstName = strtolower($constName); |
| 410 | + if ($loweredConstName === 'null') { |
| 411 | + $parameterType = TypeCombinator::addNull($parameterType); |
| 412 | + if ($phpDocParameterType !== null) { |
| 413 | + $phpDocParameterType = TypeCombinator::addNull($phpDocParameterType); |
| 414 | + } |
| 415 | + } |
| 416 | + } |
| 417 | + |
403 | 418 | $parameters[] = new ParameterSignature( |
404 | 419 | $name->name, |
405 | 420 | $param->default !== null || $param->variadic, |
406 | | - TypehintHelper::decideType($parameterType, $phpDocParameterTypes[$name->name] ?? null), |
| 421 | + TypehintHelper::decideType($parameterType, $phpDocParameterType), |
407 | 422 | $parameterType, |
408 | 423 | $param->byRef ? PassedByReference::createCreatesNewVariable() : PassedByReference::createNo(), |
409 | 424 | $param->variadic, |
|
0 commit comments