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

Fix #4132 - ignore purity of $this when checking for initialisation

This commit is contained in:
Brown 2020-09-04 16:46:20 -04:00 committed by Daniil Gentili
parent 7e6a3c7b55
commit be3d130965
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 36 additions and 13 deletions

View File

@ -102,20 +102,22 @@ 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
if (!$context->collect_mutations && !$context->collect_initializations) {
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 ($statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
) {
$statements_analyzer->getSource()->inferred_impure = true;
}
} elseif ($statements_analyzer->getSource() instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
) {
$statements_analyzer->getSource()->inferred_impure = true;
}
return true;

View File

@ -387,6 +387,27 @@ class PureAnnotationTest extends TestCase
return false;
}',
],
'allowPureInConstrucctorThis' => [
'<?php
class Port {
private int $portNumber;
public function __construct(int $portNumber) {
if (!$this->isValidPort($portNumber)) {
throw new Exception();
}
$this->portNumber = $portNumber;
}
/**
* @psalm-pure
*/
private function isValidPort(int $portNumber): bool {
return $portNumber >= 1 && $portNumber <= 1000;
}
}'
],
];
}