Skip to content
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

Assert on var of nullsafe expr #10575

Draft
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,18 @@ private static function getNullInequalityAssertions(
$source,
);


if ($base_conditional instanceof PhpParser\Node\Expr\NullsafeMethodCall) {
$vvar_name = ExpressionIdentifier::getExtendedVarId(
$base_conditional->var,
$this_class_name,
$source,
);
if ($vvar_name !== null) {
$if_types[$vvar_name] = [[new IsNotType(new TNull())]];
}
}

if ($var_name) {
if ($base_conditional instanceof PhpParser\Node\Expr\Assign) {
$var_name = '=' . $var_name;
Expand Down Expand Up @@ -2801,6 +2813,17 @@ private static function getNullEqualityAssertions(
throw new UnexpectedValueException('$null_position value');
}

if ($base_conditional instanceof PhpParser\Node\Expr\NullsafeMethodCall) {
$vvar_name = ExpressionIdentifier::getExtendedVarId(
$base_conditional->var,
$this_class_name,
$source,
);
if ($vvar_name !== null) {
$if_types[$vvar_name] = [[new IsNotType(new TNull())]];
}
}

$var_name = ExpressionIdentifier::getExtendedVarId(
$base_conditional,
$this_class_name,
Expand Down
54 changes: 54 additions & 0 deletions tests/MethodCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,60 @@ function printInt(int $int): void {
public function providerValidCodeParse(): iterable
{
return [
'nullSafeCallNotNullMakesVarNotNull' => [
'code' => '<?php
class Foo {
public function check(): bool {
return false;
}
public function do(): void {}
}

/** @var ?Foo */
$foo = null;

if ($foo?->check() === null) {
/** @psalm-check-type-exact $foo = null */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be curious to see the same test but with check that returns "?bool". Because then you can't infer anything from the nullsafe call

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep my exact thoughts, I still need to finish this PR

}

/** @var ?Foo */
$foo = null;

if ($foo?->check() !== true) {
/** @psalm-check-type-exact $foo = null */
}

/** @var ?Foo */
$foo = null;

if ($foo?->check() !== false) {
/** @psalm-check-type-exact $foo = null */
}

/** @var ?Foo */
$foo = null;

if ($foo?->check() !== null) {
/** @psalm-check-type-exact $foo = Foo */
}

/** @var ?Foo */
$foo = null;

if ($foo?->check() === false) {
/** @psalm-check-type-exact $foo = Foo */
}

/** @var ?Foo */
$foo = null;

if ($foo?->check() === true) {
/** @psalm-check-type-exact $foo = Foo */
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1'
],
'notInCallMapTest' => [
'code' => '<?php
new DOMImplementation();',
Expand Down
Loading