Skip to content

Commit 5a565a2

Browse files
committed
Fix signature type for default-null parameters
1 parent 88d5b8c commit 5a565a2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Reflection/SignatureMap/Php8SignatureMapProvider.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection\SignatureMap;
44

55
use PhpParser\Node\AttributeGroup;
6+
use PhpParser\Node\Expr\ConstFetch;
67
use PhpParser\Node\Expr\Variable;
78
use PhpParser\Node\Scalar\String_;
89
use PhpParser\Node\Stmt\ClassConst;
@@ -21,6 +22,7 @@
2122
use PHPStan\Type\MixedType;
2223
use PHPStan\Type\ParserNodeTypeToPHPStanType;
2324
use PHPStan\Type\Type;
25+
use PHPStan\Type\TypeCombinator;
2426
use PHPStan\Type\TypehintHelper;
2527
use ReflectionFunctionAbstract;
2628
use function array_key_exists;
@@ -400,10 +402,23 @@ private function getSignature(
400402
throw new ShouldNotHappenException();
401403
}
402404
$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+
403418
$parameters[] = new ParameterSignature(
404419
$name->name,
405420
$param->default !== null || $param->variadic,
406-
TypehintHelper::decideType($parameterType, $phpDocParameterTypes[$name->name] ?? null),
421+
TypehintHelper::decideType($parameterType, $phpDocParameterType),
407422
$parameterType,
408423
$param->byRef ? PassedByReference::createCreatesNewVariable() : PassedByReference::createNo(),
409424
$param->variadic,

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -1793,4 +1793,10 @@ public function testBug7082(): void
17931793
]);
17941794
}
17951795

1796+
public function testBug7522(): void
1797+
{
1798+
$this->checkExplicitMixed = true;
1799+
$this->analyse([__DIR__ . '/data/bug-7522.php'], []);
1800+
}
1801+
17961802
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug7522;
4+
5+
function doFoo() {
6+
// Example #2 from https://www.php.net/manual/en/function.ob-start.php
7+
\ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_REMOVABLE);
8+
}

0 commit comments

Comments
 (0)