Skip to content

Handle default value in phpstan-assert #4226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\Name;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Node\IssetExpr;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Php\PhpVersion;
Expand Down Expand Up @@ -1451,6 +1452,14 @@ private function specifyTypesFromAsserts(TypeSpecifierContext $context, Expr\Cal

$argsMap[$paramName][] = $arg->value;
}
foreach ($parameters as $parameter) {
$name = $parameter->getName();
$defaultValue = $parameter->getDefaultValue();
if (isset($argsMap[$name]) || $defaultValue === null) {
continue;
}
$argsMap[$name][] = new TypeExpr($defaultValue);
}

if ($call instanceof MethodCall) {
$argsMap['this'] = [$call->var];
Expand Down
144 changes: 144 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9435.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php // lint >= 8.0

declare(strict_types=1);

namespace Bug9435;

use function PHPStan\Testing\assertType;

function x(): mixed
{
return null;
}

/** @phpstan-assert ($allow_null is true ? string|null : string) $input */
function trueCheck(mixed $input, bool $allow_null = false): void
{
}

$a = x();
trueCheck($a);
assertType('string', $a); // incorrect: should be string but is string|null

$a = x();
trueCheck($a, false);
assertType('string', $a); // correct (string)

$a = x();
trueCheck($a, allow_null: false);
assertType('string', $a); // correct (string)

$a = x();
trueCheck(allow_null: false, input: $a);
assertType('string', $a); // correct (string)

$a = x();
trueCheck($a, true);
assertType('string|null', $a); // correct (string|null)

$a = x();
trueCheck($a, allow_null: true);
assertType('string|null', $a); // correct (string|null)

$a = x();
trueCheck(allow_null: true, input: $a);
assertType('string|null', $a); // correct (string|null)

/** @phpstan-assert ($allow_null is false ? string : string|null) $input */
function falseCheck(mixed $input, bool $allow_null = false): void
{
}

$a = x();
falseCheck($a);
assertType('string', $a); // incorrect: should be string but is string|null

$a = x();
falseCheck($a, false);
assertType('string', $a); // correct (string)

$a = x();
falseCheck($a, allow_null: false);
assertType('string', $a); // correct (string)

$a = x();
falseCheck(allow_null: false, input: $a);
assertType('string', $a); // correct (string|null)

$a = x();
falseCheck($a, true);
assertType('string|null', $a); // correct (string|null)

$a = x();
falseCheck($a, allow_null: true);
assertType('string|null', $a); // correct (string|null)

$a = x();
falseCheck(allow_null: true, input: $a);
assertType('string|null', $a); // correct (string|null)

/** @phpstan-assert ($allow_null is not true ? string : string|null) $input */
function notTrueCheck(mixed $input, bool $allow_null = false): void
{
}

$a = x();
notTrueCheck($a);
assertType('string', $a); // incorrect: should be string but is string|null

$a = x();
notTrueCheck($a, false);
assertType('string', $a); // correct (string)

$a = x();
notTrueCheck($a, allow_null: false);
assertType('string', $a); // correct (string)

$a = x();
notTrueCheck(allow_null: false, input: $a);
assertType('string', $a); // correct (string|null)

$a = x();
notTrueCheck($a, true);
assertType('string|null', $a); // correct (string|null)

$a = x();
notTrueCheck($a, allow_null: true);
assertType('string|null', $a); // correct (string|null)

$a = x();
notTrueCheck(allow_null: true, input: $a);
assertType('string|null', $a); // correct (string|null)

/** @phpstan-assert ($allow_null is not false ? string|null : string) $input */
function notFalseCheck(mixed $input, bool $allow_null = false): void
{
}

$a = x();
notFalseCheck($a);
assertType('string', $a); // incorrect: should be string but is string|null

$a = x();
notFalseCheck($a, false);
assertType('string', $a); // correct (string)

$a = x();
notFalseCheck($a, allow_null: false);
assertType('string', $a); // correct (string)

$a = x();
notFalseCheck(allow_null: false, input: $a);
assertType('string', $a); // correct (string|null)

$a = x();
notFalseCheck($a, true);
assertType('string|null', $a); // correct (string|null)

$a = x();
notFalseCheck($a, allow_null: true);
assertType('string|null', $a); // correct (string|null)

$a = x();
notFalseCheck(allow_null: true, input: $a);
assertType('string|null', $a); // correct (string|null)
Loading