mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 13:51:54 +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 (!$context->collect_mutations && !$context->collect_initializations) {
|
||||||
if (IssueBuffer::accepts(
|
if ($context->pure) {
|
||||||
new ImpureVariable(
|
if (IssueBuffer::accepts(
|
||||||
'Cannot reference $this in a pure context',
|
new ImpureVariable(
|
||||||
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
'Cannot reference $this in a pure context',
|
||||||
),
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
||||||
$statements_analyzer->getSuppressedIssues()
|
),
|
||||||
)) {
|
$statements_analyzer->getSuppressedIssues()
|
||||||
// fall through
|
)) {
|
||||||
|
// 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;
|
return true;
|
||||||
|
@ -387,6 +387,27 @@ class PureAnnotationTest extends TestCase
|
|||||||
return false;
|
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