Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Reflection/SignatureMap/Php8SignatureMapProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Reflection\SignatureMap;

use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassConst;
Expand All @@ -22,6 +23,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypehintHelper;
use ReflectionFunctionAbstract;
use function array_key_exists;
Expand Down Expand Up @@ -409,10 +411,23 @@ private function getSignature(
throw new ShouldNotHappenException();
}
$parameterType = ParserNodeTypeToPHPStanType::resolve($param->type, $classReflection);
$phpDocParameterType = $phpDocParameterTypes[$name->name] ?? null;

if ($param->default instanceof ConstFetch) {
$constName = (string) $param->default->name;
$loweredConstName = strtolower($constName);
if ($loweredConstName === 'null') {
$parameterType = TypeCombinator::addNull($parameterType);
if ($phpDocParameterType !== null) {
$phpDocParameterType = TypeCombinator::addNull($phpDocParameterType);
}
}
}

$parameters[] = new ParameterSignature(
$name->name,
$param->default !== null || $param->variadic,
TypehintHelper::decideType($parameterType, $phpDocParameterTypes[$name->name] ?? null),
TypehintHelper::decideType($parameterType, $phpDocParameterType),
$parameterType,
$param->byRef ? PassedByReference::createCreatesNewVariable() : PassedByReference::createNo(),
$param->variadic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2047,4 +2047,10 @@ public function testBug12499(): void
$this->analyse([__DIR__ . '/data/bug-12499.php'], []);
}

public function testBug7522(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-7522.php'], []);
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-7522.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Bug7522;

function doFoo() {
// Example #2 from https://www.php.net/manual/en/function.ob-start.php
\ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_REMOVABLE);
}
Loading