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:
parent
7e6a3c7b55
commit
be3d130965
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user