Skip to content

Commit

Permalink
Static call inside throw does not violate purity
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 18, 2020
1 parent 7af771a commit edb0795
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
Expand Up @@ -1011,25 +1011,27 @@ function (PhpParser\Node\Arg $arg) {
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
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/PureAnnotationTest.php
Expand Up @@ -278,6 +278,29 @@ function getTraceAsString(Throwable $e): string {
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;
}'
],
];
}

Expand Down

0 comments on commit edb0795

Please sign in to comment.