1
0
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:
Brown 2020-08-23 18:50:17 -04:00 committed by Daniil Gentili
parent 1ec0f35011
commit 5513fcdcff
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 64 additions and 0 deletions

View File

@ -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;
}

View File

@ -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',