mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Prevent isset on uknown property in pure function
This commit is contained in:
parent
1ec0f35011
commit
5513fcdcff
@ -789,6 +789,26 @@ class InstancePropertyFetchAnalyzer
|
||||
$property_id = $context->self . '::$' . $prop_name;
|
||||
} else {
|
||||
if ($context->inside_isset || $context->collect_initializations) {
|
||||
$project_analyzer = $statements_analyzer->getProjectAnalyzer();
|
||||
|
||||
if ($context->pure) {
|
||||
if (IssueBuffer::accepts(
|
||||
new ImpurePropertyFetch(
|
||||
'Cannot access a property on a mutable object from a pure context',
|
||||
new CodeLocation($statements_analyzer, $stmt)
|
||||
),
|
||||
$statements_analyzer->getSuppressedIssues()
|
||||
)) {
|
||||
// fall through
|
||||
}
|
||||
} elseif ($codebase->alter_code
|
||||
&& isset($project_analyzer->getIssuesToFix()['MissingPureAnnotation'])
|
||||
&& $statements_analyzer->getSource()
|
||||
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|
||||
) {
|
||||
$statements_analyzer->getSource()->inferred_impure = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -670,6 +670,50 @@ class PureAnnotationTest extends TestCase
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}',
|
||||
'error_message' => 'ImpurePropertyFetch',
|
||||
],
|
||||
'preventIssetOnMutableClassKnownProperty' => [
|
||||
'<?php
|
||||
namespace Bar;
|
||||
|
||||
class A {
|
||||
public ?int $a;
|
||||
|
||||
public function __construct(?int $a) {
|
||||
$this->a = $a;
|
||||
}
|
||||
}
|
||||
|
||||
/** @psalm-pure */
|
||||
function filterOdd(A $a) : bool {
|
||||
if (isset($a->a)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}',
|
||||
'error_message' => 'ImpurePropertyFetch',
|
||||
],
|
||||
'preventIssetOnMutableClassUnknownProperty' => [
|
||||
'<?php
|
||||
namespace Bar;
|
||||
|
||||
class A {
|
||||
public ?int $a;
|
||||
|
||||
public function __construct(?int $a) {
|
||||
$this->a = $a;
|
||||
}
|
||||
}
|
||||
|
||||
/** @psalm-pure */
|
||||
function filterOdd(A $a) : bool {
|
||||
if (isset($a->b)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}',
|
||||
'error_message' => 'ImpurePropertyFetch',
|
||||
|
Loading…
Reference in New Issue
Block a user