1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Limited fix for #5028

This commit is contained in:
Matt Brown 2021-02-07 22:56:37 -05:00
parent e3d73a3166
commit 3be1bb7342
2 changed files with 28 additions and 1 deletions

View File

@ -177,7 +177,7 @@ class InstancePropertyFetchAnalyzer
if ($stmt_var_type->isNullable() && !$stmt_var_type->ignore_nullable_issues) {
// we can only be sure that the variable is possibly null if we know the var_id
if (!$context->inside_isset && $var_id) {
if (!$context->inside_isset && $stmt->name instanceof PhpParser\Node\Identifier) {
if (IssueBuffer::accepts(
new PossiblyNullPropertyFetch(
'Cannot get property on possibly null variable ' . $stmt_var_id . ' of type ' . $stmt_var_type,

View File

@ -2380,6 +2380,18 @@ class PropertyTypeTest extends TestCase
[],
'8.0'
],
'possiblyNullOnFunctionCallCoalesced' => [
'<?php
class Foo
{
/** @var int */
public $a = 0;
}
function accessOnVar(?Foo $bar, string $b) : void {
echo $bar->{$b} ?? null;
}',
],
];
}
@ -3511,6 +3523,21 @@ class PropertyTypeTest extends TestCase
}',
'error_message' => 'MismatchingDocblockPropertyType',
],
'possiblyNullOnFunctionCallNotCoalesced' => [
'<?php
function getC() : ?C {
return rand(0, 1) ? new C() : null;
}
function foo() : void {
echo getC()->id;
}
class C {
public int $id = 1;
}',
'error_message' => 'PossiblyNullPropertyFetch',
],
];
}
}