Skip to content

Commit

Permalink
Assert on var of nullsafe expr
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 19, 2024
1 parent f7edaa6 commit f67746a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
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 */
}
/** @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

0 comments on commit f67746a

Please sign in to comment.