1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Static call inside throw does not violate purity

This commit is contained in:
Brown 2020-04-18 12:43:51 -04:00
parent 7af771a006
commit edb07952fc
2 changed files with 44 additions and 19 deletions

View File

@ -1011,25 +1011,27 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
return;
}
if ($context->pure && !$method_storage->pure) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an impure method from a pure context',
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($context->mutation_free && !$method_storage->mutation_free) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an possibly-mutating method from a mutation-free context',
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
if (!$context->inside_throw) {
if ($context->pure && !$method_storage->pure) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an impure method from a pure context',
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($context->mutation_free && !$method_storage->mutation_free) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an possibly-mutating method from a mutation-free context',
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}

View File

@ -278,6 +278,29 @@ class PureAnnotationTest extends TestCase
echo getTraceAsString(new Exception("test"));'
],
'callingMethodInThrowStillPure' => [
'<?php
final class MyException extends \Exception {
public static function hello(): self
{
return new self();
}
}
/**
* @psalm-pure
*/
function sumExpectedToNotBlowPowerFuse(int $first, int $second): int {
$sum = $first + $second;
if ($sum > 9000) {
throw MyException::hello();
}
if ($sum > 90001) {
throw new MyException();
}
return $sum;
}'
],
];
}