mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Propagate possibly-null issues onto fetched properties
This commit is contained in:
parent
f00ee740c0
commit
fef61e996e
@ -297,7 +297,7 @@ class PropertyFetchAnalyzer
|
||||
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getRootFilePath());
|
||||
}
|
||||
|
||||
if ($stmt_var_type->isNullable() && !$stmt_var_type->ignore_nullable_issues && !$context->inside_isset) {
|
||||
if ($stmt_var_type->isNullable() && !$context->inside_isset && !$stmt_var_type->ignore_nullable_issues) {
|
||||
if (IssueBuffer::accepts(
|
||||
new PossiblyNullPropertyFetch(
|
||||
'Cannot get property on possibly null variable ' . $stmt_var_id . ' of type ' . $stmt_var_type,
|
||||
@ -307,8 +307,6 @@ class PropertyFetchAnalyzer
|
||||
)) {
|
||||
// fall through
|
||||
}
|
||||
|
||||
$stmt->inferredType = Type::getNull();
|
||||
}
|
||||
|
||||
if (!$prop_name) {
|
||||
@ -798,6 +796,14 @@ class PropertyFetchAnalyzer
|
||||
}
|
||||
}
|
||||
|
||||
if ($stmt_var_type->isNullable() && !$context->inside_isset && $stmt->inferredType) {
|
||||
$stmt->inferredType->addType(new TNull);
|
||||
|
||||
if ($stmt_var_type->ignore_nullable_issues) {
|
||||
$stmt->inferredType->ignore_nullable_issues = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($codebase->store_node_types
|
||||
&& !$context->collect_initializations
|
||||
&& !$context->collect_mutations
|
||||
|
@ -1702,7 +1702,28 @@ class PropertyTypeTest extends TestCase
|
||||
/** @var array<int, static> */
|
||||
private $t2 = [];
|
||||
}'
|
||||
]
|
||||
],
|
||||
'propagateIgnoreNullableOnPropertyFetch' => [
|
||||
'<?php
|
||||
class A {
|
||||
public string $s = "hey";
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-ignore-nullable-return
|
||||
*/
|
||||
function foo() : ?A {
|
||||
return rand(0, 1) ? new A : null;
|
||||
}
|
||||
|
||||
function takesString(string $_s) : void {}
|
||||
|
||||
$foo = foo();
|
||||
|
||||
if ($foo->s !== null) {}
|
||||
echo $foo->s ?? "bar";
|
||||
takesString($foo->s);',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -2689,6 +2710,26 @@ class PropertyTypeTest extends TestCase
|
||||
$a->bar = "goodbye";',
|
||||
'error_message' => 'InaccessibleProperty',
|
||||
],
|
||||
'addNullToMixedAfterNullablePropertyFetch' => [
|
||||
'<?php
|
||||
class A {
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
public $foo;
|
||||
}
|
||||
|
||||
function takesString(string $s) : void {}
|
||||
|
||||
function takesA(?A $a) : void {
|
||||
/**
|
||||
* @psalm-suppress PossiblyNullPropertyFetch
|
||||
* @psalm-suppress MixedArgument
|
||||
*/
|
||||
takesString($a->foo);
|
||||
}',
|
||||
'error_message' => 'PossiblyNullArgument',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user