1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix ImpureVariable test

This commit is contained in:
Brown 2020-08-23 22:07:02 -04:00
parent ba1ff21af4
commit 6103cf0f51
3 changed files with 40 additions and 30 deletions

View File

@ -105,6 +105,24 @@ class VariableFetchAnalyzer
);
}
if ($context->pure) {
if (IssueBuffer::accepts(
new ImpureVariable(
'Cannot reference $this in a pure context',
new CodeLocation($statements_analyzer->getSource(), $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;
}
@ -150,26 +168,6 @@ class VariableFetchAnalyzer
return true;
}
if ($stmt->name === 'this') {
if ($context->pure) {
if (IssueBuffer::accepts(
new ImpureVariable(
'Cannot reference $this in a pure context',
new CodeLocation($statements_analyzer->getSource(), $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;
}
}
if (!is_string($stmt->name)) {
if ($context->pure) {
if (IssueBuffer::accepts(

View File

@ -118,27 +118,25 @@ class PureAnnotationAdditionTest extends FileManipulationTest
],
'dontAddWhenReferencingThis' => [
'<?php
class A {
abstract class A {
public int $a = 5;
/**
* @psalm-pure
*/
public function foo() : self {
return $this;
}
}',
}
class B extends A {}',
'<?php
class A {
abstract class A {
public int $a = 5;
/**
* @psalm-pure
*/
public function foo() : self {
return $this;
}
}',
}
class B extends A {}',
'7.4',
['MissingPureAnnotation'],
true,

View File

@ -718,6 +718,20 @@ class PureAnnotationTest extends TestCase
}',
'error_message' => 'ImpurePropertyFetch',
],
'impureThis' => [
'<?php
class A {
public int $a = 5;
/**
* @psalm-pure
*/
public function foo() : self {
return $this;
}
}',
'error_message' => 'ImpureVariable',
],
];
}
}