Skip to content

Commit

Permalink
Merge pull request #7545 from orklah/impure-call-detection-for-psalter
Browse files Browse the repository at this point in the history
fix wrong detection of purity
  • Loading branch information
orklah committed Jan 31, 2022
2 parents 4729eb0 + 48e09ab commit 69e8815
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Psalm\Plugin\EventHandler\Event\AfterEveryFunctionCallAnalysisEvent;
use Psalm\Storage\Assertion;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Storage\FunctionStorage;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArray;
Expand Down Expand Up @@ -644,14 +645,23 @@ private static function getAnalyzeNamedExpression(
}

if ($var_type_part instanceof TClosure || $var_type_part instanceof TCallable) {
if (!$var_type_part->is_pure && ($context->pure || $context->mutation_free)) {
IssueBuffer::maybeAdd(
new ImpureFunctionCall(
'Cannot call an impure function from a mutation-free context',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
if (!$var_type_part->is_pure) {
if ($context->pure || $context->mutation_free) {
IssueBuffer::maybeAdd(
new ImpureFunctionCall(
'Cannot call an impure function from a mutation-free context',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
);
}

if (!$function_call_info->function_storage) {
$function_call_info->function_storage = new FunctionStorage();
}

$function_call_info->function_storage->pure = false;
$function_call_info->function_storage->mutation_free = false;
}

$function_call_info->function_params = $var_type_part->params;
Expand Down
13 changes: 13 additions & 0 deletions tests/FileManipulation/PureAnnotationAdditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ public function bar(string $s) : string {
['MissingPureAnnotation'],
true,
],
'dontAddPureIfCallableNotPure' => [
'<?php
function pure(callable $callable): string{
return $callable();
}',
'<?php
function pure(callable $callable): string{
return $callable();
}',
'7.4',
['MissingPureAnnotation'],
true,
],
];
}
}

0 comments on commit 69e8815

Please sign in to comment.