mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
add pure-callable type (#4066)
This commit is contained in:
parent
a716185564
commit
a0a30c500c
@ -367,6 +367,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
||||
|
||||
if ($storage instanceof FunctionStorage) {
|
||||
$closure_type->byref_uses = $storage->byref_uses;
|
||||
$closure_type->is_pure = $storage->pure;
|
||||
}
|
||||
|
||||
$type_provider->setType(
|
||||
|
@ -567,6 +567,18 @@ class FunctionCallAnalyzer extends CallAnalyzer
|
||||
|
||||
foreach ($stmt_name_type->getAtomicTypes() as $var_type_part) {
|
||||
if ($var_type_part instanceof Type\Atomic\TFn || $var_type_part instanceof Type\Atomic\TCallable) {
|
||||
if (!$var_type_part->is_pure && $context->pure) {
|
||||
if (IssueBuffer::accepts(
|
||||
new ImpureFunctionCall(
|
||||
'Cannot call an impure function from a mutation-free context',
|
||||
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
||||
),
|
||||
$statements_analyzer->getSuppressedIssues()
|
||||
)) {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
|
||||
$function_params = $var_type_part->params;
|
||||
|
||||
if (($stmt_type = $statements_analyzer->node_data->getType($real_stmt))
|
||||
|
@ -23,8 +23,6 @@ class AstDiffer
|
||||
* @param array<int, PhpParser\Node\Stmt> $b
|
||||
*
|
||||
* @return array{0:array<int, array<int, int>>, 1: int, 2: int, 3: array<int, bool>}
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
protected static function calculateTrace(
|
||||
\Closure $is_equal,
|
||||
|
@ -30,6 +30,14 @@ class CallableTypeComparator
|
||||
Type\Atomic $container_type_part,
|
||||
?TypeComparisonResult $atomic_comparison_result
|
||||
) : bool {
|
||||
if ($container_type_part->is_pure && !$input_type_part->is_pure) {
|
||||
if ($atomic_comparison_result) {
|
||||
$atomic_comparison_result->scalar_type_match_found = true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($container_type_part->params !== null && $input_type_part->params === null) {
|
||||
if ($atomic_comparison_result) {
|
||||
$atomic_comparison_result->type_coerced = true;
|
||||
@ -291,7 +299,8 @@ class CallableTypeComparator
|
||||
return new TCallable(
|
||||
'callable',
|
||||
$method_storage->params,
|
||||
$converted_return_type
|
||||
$converted_return_type,
|
||||
$method_storage->pure
|
||||
);
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
// do nothing
|
||||
@ -306,13 +315,24 @@ class CallableTypeComparator
|
||||
);
|
||||
|
||||
if ($codebase->methods->methodExists($invoke_id)) {
|
||||
$method_storage = $codebase->methods->getStorage($invoke_id);
|
||||
$method_fqcln = $invoke_id->fq_class_name;
|
||||
$converted_return_type = null;
|
||||
if ($method_storage->return_type) {
|
||||
$converted_return_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
||||
$codebase,
|
||||
$method_storage->return_type,
|
||||
$method_fqcln,
|
||||
$method_fqcln,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
return new TCallable(
|
||||
'callable',
|
||||
$codebase->methods->getMethodParams($invoke_id),
|
||||
$codebase->methods->getMethodReturnType(
|
||||
$invoke_id,
|
||||
$input_type_part->value
|
||||
)
|
||||
$method_storage->params,
|
||||
$converted_return_type,
|
||||
$method_storage->pure
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -745,7 +745,7 @@ class ParseTreeCreator
|
||||
break;
|
||||
|
||||
case '(':
|
||||
if (in_array(strtolower($type_token[0]), ['closure', 'callable', '\closure'], true)) {
|
||||
if (in_array(strtolower($type_token[0]), ['closure', 'callable', '\closure', 'pure-callable'], true)) {
|
||||
$new_leaf = new ParseTree\CallableTree(
|
||||
$type_token[0],
|
||||
$new_parent
|
||||
@ -760,7 +760,7 @@ class ParseTreeCreator
|
||||
);
|
||||
} else {
|
||||
throw new TypeParseTreeException(
|
||||
'Bracket must be preceded by “Closure”, “callable” or a valid @method name'
|
||||
'Bracket must be preceded by “Closure”, “callable”, "pure-callable" or a valid @method name'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -675,12 +675,13 @@ class TypeParser
|
||||
},
|
||||
$parse_tree->children
|
||||
);
|
||||
$pure = strpos($parse_tree->value, 'pure-') === 0;
|
||||
|
||||
if (in_array(strtolower($parse_tree->value), ['closure', '\closure'], true)) {
|
||||
return new TFn('Closure', $params);
|
||||
return new TFn('Closure', $params, null, $pure);
|
||||
}
|
||||
|
||||
return new TCallable($parse_tree->value, $params);
|
||||
return new TCallable($parse_tree->value, $params, null, $pure);
|
||||
}
|
||||
|
||||
if ($parse_tree instanceof ParseTree\EncapsulationTree) {
|
||||
|
@ -164,6 +164,11 @@ abstract class Atomic implements TypeNode
|
||||
|
||||
case 'callable':
|
||||
return new TCallable();
|
||||
case 'pure-callable':
|
||||
$type = new TCallable();
|
||||
$type->is_pure = true;
|
||||
|
||||
return $type;
|
||||
|
||||
case 'array':
|
||||
case 'associative-array':
|
||||
|
@ -26,6 +26,10 @@ class TCallable extends \Psalm\Type\Atomic
|
||||
$php_major_version,
|
||||
$php_minor_version
|
||||
) {
|
||||
if ($this->is_pure) {
|
||||
return 'pure-callable';
|
||||
}
|
||||
|
||||
return 'callable';
|
||||
}
|
||||
|
||||
|
@ -1146,6 +1146,42 @@ class CallableTest extends TestCase
|
||||
$a();',
|
||||
'error_message' => 'InvalidFunctionCall',
|
||||
],
|
||||
'ImpureFunctionCall' => [
|
||||
'<?php
|
||||
/**
|
||||
* @psalm-template T
|
||||
*
|
||||
* @psalm-param array<int, T> $values
|
||||
* @psalm-param (callable(T): numeric) $num_func
|
||||
*
|
||||
* @psalm-return null|T
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
function max_by(array $values, callable $num_func)
|
||||
{
|
||||
$max = null;
|
||||
$max_num = null;
|
||||
foreach ($values as $value) {
|
||||
$value_num = $num_func($value);
|
||||
if (null === $max_num || $value_num >= $max_num) {
|
||||
$max = $value;
|
||||
$max_num = $value_num;
|
||||
}
|
||||
}
|
||||
|
||||
return $max;
|
||||
}
|
||||
|
||||
$c = max_by([1, 2, 3], static function(int $a): int {
|
||||
return $a + mt_rand(0, $a);
|
||||
});
|
||||
|
||||
echo $c;
|
||||
',
|
||||
'error_message' => 'ImpureFunctionCall',
|
||||
'error_levels' => [],
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
241
tests/PureCallableTest.php
Normal file
241
tests/PureCallableTest.php
Normal file
@ -0,0 +1,241 @@
|
||||
<?php
|
||||
namespace Psalm\Tests;
|
||||
|
||||
class PureCallableTest extends TestCase
|
||||
{
|
||||
use Traits\ValidCodeAnalysisTestTrait;
|
||||
use Traits\InvalidCodeAnalysisTestTrait;
|
||||
|
||||
/**
|
||||
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
||||
*/
|
||||
public function providerValidCodeParse()
|
||||
{
|
||||
return [
|
||||
'varCallableParamReturnType' => [
|
||||
'<?php
|
||||
$add_one =
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
function(int $a): int {
|
||||
return $a + 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param pure-callable(int): int $c
|
||||
*/
|
||||
function bar(callable $c) : int {
|
||||
return $c(1);
|
||||
}
|
||||
|
||||
bar($add_one);',
|
||||
],
|
||||
'callableToClosure' => [
|
||||
'<?php
|
||||
/**
|
||||
* @return pure-callable
|
||||
*/
|
||||
function foo() {
|
||||
return
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
function(string $a): string {
|
||||
return $a . "blah";
|
||||
};
|
||||
}',
|
||||
],
|
||||
'callableToClosureArrow' => [
|
||||
'<?php
|
||||
/**
|
||||
* @return pure-callable
|
||||
*/
|
||||
function foo() {
|
||||
return
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
fn(string $a): string => $a . "blah";
|
||||
}',
|
||||
'assertions' => [],
|
||||
'error_levels' => [],
|
||||
'7.4',
|
||||
],
|
||||
'callableWithNonInvokable' => [
|
||||
'<?php
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
function asd(): void {}
|
||||
class B {}
|
||||
|
||||
/**
|
||||
* @param pure-callable|B $p
|
||||
*/
|
||||
function passes($p): void {}
|
||||
|
||||
passes("asd");',
|
||||
],
|
||||
'callableWithInvokableUnion' => [
|
||||
'<?php
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
function asd(): void {}
|
||||
class A {public function __invoke(): void {} }
|
||||
|
||||
/**
|
||||
* @param pure-callable|A $p
|
||||
*/
|
||||
function fails($p): void {}
|
||||
|
||||
fails("asd");',
|
||||
],
|
||||
'callableWithInvokable' => [
|
||||
'<?php
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
function asd(): void {}
|
||||
class A {
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
public function __invoke(): void {}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pure-callable $p
|
||||
*/
|
||||
function fails($p): void {}
|
||||
|
||||
fails(new A());
|
||||
fails("asd");',
|
||||
],
|
||||
'allowVoidCallable' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param pure-callable():void $p
|
||||
*/
|
||||
function doSomething($p): void {}
|
||||
doSomething(
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
function(): bool { return false; }
|
||||
);',
|
||||
],
|
||||
'callableProperties' => [
|
||||
'<?php
|
||||
class C {
|
||||
/** @psalm-var pure-callable():bool */
|
||||
private $callable;
|
||||
|
||||
/**
|
||||
* @psalm-param pure-callable():bool $callable
|
||||
*/
|
||||
public function __construct(callable $callable) {
|
||||
$this->callable = $callable;
|
||||
}
|
||||
|
||||
public function callTheCallableDirectly(): bool {
|
||||
return ($this->callable)();
|
||||
}
|
||||
|
||||
public function callTheCallableIndirectly(): bool {
|
||||
$r = $this->callable;
|
||||
return $r();
|
||||
}
|
||||
}',
|
||||
],
|
||||
'nullableReturnTypeShorthand' => [
|
||||
'<?php
|
||||
class A {}
|
||||
/** @param pure-callable(mixed):?A $a */
|
||||
function foo(callable $a): void {}',
|
||||
],
|
||||
'callablesCanBeObjects' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param pure-callable $c
|
||||
*/
|
||||
function foo(callable $c) : void {
|
||||
if (is_object($c)) {
|
||||
$c();
|
||||
}
|
||||
}',
|
||||
],
|
||||
'goodCallableArgs' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param pure-callable(string,string):int $_p
|
||||
*/
|
||||
function f(callable $_p): void {}
|
||||
|
||||
class C {
|
||||
/**
|
||||
* @psalm-pure
|
||||
*/
|
||||
public static function m(string $a, string $b): int { return $a <=> $b; }
|
||||
}
|
||||
|
||||
f("strcmp");
|
||||
f([new C, "m"]);
|
||||
f([C::class, "m"]);',
|
||||
],
|
||||
'callableWithSpaces' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param pure-callable(string, string) : int $p
|
||||
*/
|
||||
function f(callable $p): void {}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
||||
*/
|
||||
public function providerInvalidCodeParse()
|
||||
{
|
||||
return [
|
||||
'InvalidScalarArgument' => [
|
||||
'<?php
|
||||
/**
|
||||
* @psalm-template T
|
||||
*
|
||||
* @psalm-param array<int, T> $values
|
||||
* @psalm-param (pure-callable(T): numeric) $num_func
|
||||
*
|
||||
* @psalm-return null|T
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
function max_by(array $values, callable $num_func)
|
||||
{
|
||||
$max = null;
|
||||
$max_num = null;
|
||||
foreach ($values as $value) {
|
||||
$value_num = $num_func($value);
|
||||
if (null === $max_num || $value_num >= $max_num) {
|
||||
$max = $value;
|
||||
$max_num = $value_num;
|
||||
}
|
||||
}
|
||||
|
||||
return $max;
|
||||
}
|
||||
|
||||
$c = max_by([1, 2, 3], static function(int $a): int {
|
||||
return $a + mt_rand(0, $a);
|
||||
});
|
||||
|
||||
echo $c;
|
||||
',
|
||||
'error_message' => 'InvalidScalarArgument',
|
||||
'error_levels' => [],
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user