Skip to content

Commit

Permalink
Builtin functions are assumed to be pure
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 7, 2019
1 parent b5eb63b commit 955899a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Expand Up @@ -599,7 +599,7 @@ public static function analyze(
}

if ($context->pure) {
if (!$function_storage || !$function_storage->pure) {
if ($function_storage && !$function_storage->pure) {
if (IssueBuffer::accepts(
new ImpureFunctionCall(
'Cannot call an impure function from a pure context',
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Codebase/Reflection.php
Expand Up @@ -368,6 +368,8 @@ public function registerFunction($function_id)
}
}

$storage->pure = true;

$storage->required_param_count = 0;

foreach ($storage->params as $i => $param) {
Expand Down
20 changes: 19 additions & 1 deletion tests/PureAnnotationTest.php
Expand Up @@ -33,6 +33,15 @@ function filterOdd(int $i, A $a) : ?int {
return null;
}',
],
'pureFunctionCallingBuiltinFunctions' => [
'<?php
namespace Bar;
/** @psalm-pure */
function lower(string $s) : string {
return substr(strtolower($s), 0, 10);
}',
],
];
}

Expand Down Expand Up @@ -90,9 +99,18 @@ function filterOdd(int $i, A $a) : ?int {
'<?php
namespace Bar;
function impure() : ?string {
/** @var int */
static $i = 0;
++$i;
return $i % 2 ? "hello" : null;
}
/** @psalm-pure */
function filterOdd(array $a) : void {
extract($a);
impure();
}',
'error_message' => 'ImpureFunctionCall',
],
Expand Down

0 comments on commit 955899a

Please sign in to comment.