2020-08-26 22:51:22 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 14:16:41 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-26 22:51:22 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2020-08-26 22:51:22 +02:00
|
|
|
class PureCallableTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
2020-08-26 22:51:22 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2020-08-26 22:51:22 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'varCallableParamReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @return pure-callable
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return
|
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function(string $a): string {
|
|
|
|
return $a . "blah";
|
|
|
|
};
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'callableToClosureArrow' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @return pure-callable
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return
|
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
fn(string $a): string => $a . "blah";
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2020-08-26 22:51:22 +02:00
|
|
|
],
|
|
|
|
'callableWithNonInvokable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function asd(): void {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param pure-callable|B $p
|
|
|
|
*/
|
|
|
|
function passes($p): void {}
|
|
|
|
|
|
|
|
passes("asd");',
|
|
|
|
],
|
|
|
|
'callableWithInvokableUnion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function asd(): void {}
|
|
|
|
class A {public function __invoke(): void {} }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param pure-callable|A $p
|
|
|
|
*/
|
|
|
|
function fails($p): void {}
|
|
|
|
|
|
|
|
fails("asd");',
|
|
|
|
],
|
|
|
|
'callableWithInvokable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @param pure-callable():void $p
|
|
|
|
*/
|
|
|
|
function doSomething($p): void {}
|
|
|
|
doSomething(
|
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function(): bool { return false; }
|
|
|
|
);',
|
|
|
|
],
|
|
|
|
'callableProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
class A {}
|
|
|
|
/** @param pure-callable(mixed):?A $a */
|
|
|
|
function foo(callable $a): void {}',
|
|
|
|
],
|
|
|
|
'callablesCanBeObjects' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @param pure-callable $c
|
|
|
|
*/
|
|
|
|
function foo(callable $c) : void {
|
|
|
|
if (is_object($c)) {
|
|
|
|
$c();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'goodCallableArgs' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @param pure-callable(string, string) : int $p
|
|
|
|
*/
|
|
|
|
function f(callable $p): void {}',
|
|
|
|
],
|
2020-08-26 22:57:27 +02:00
|
|
|
'varCallableInNamespace' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:57:27 +02:00
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param pure-callable $c
|
|
|
|
*/
|
|
|
|
function bar(callable $c) : callable {
|
|
|
|
return $c;
|
|
|
|
}',
|
|
|
|
],
|
2020-08-28 18:42:55 +02:00
|
|
|
'pureCallableArgument' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-28 18:42:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-param array<int, int> $values
|
|
|
|
* @psalm-param pure-callable(int):int $num_func
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function max_by(array $values, callable $num_func) : ?int {
|
|
|
|
$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], function(int $a): int {
|
|
|
|
return $a + 5;
|
|
|
|
});
|
|
|
|
|
|
|
|
echo $c;',
|
|
|
|
],
|
2020-08-26 22:51:22 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2020-08-26 22:51:22 +02:00
|
|
|
{
|
|
|
|
return [
|
2020-08-28 18:42:55 +02:00
|
|
|
'impureCallableArgument' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
2020-08-28 18:42:55 +02:00
|
|
|
* @psalm-param array<int, int> $values
|
|
|
|
* @psalm-param pure-callable(int):int $num_func
|
2020-08-26 22:51:22 +02:00
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
2020-08-28 18:42:55 +02:00
|
|
|
function max_by(array $values, callable $num_func) : ?int {
|
2020-08-26 22:51:22 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-08-28 18:42:55 +02:00
|
|
|
$c = max_by([1, 2, 3], function(int $a): int {
|
2020-08-26 22:51:22 +02:00
|
|
|
return $a + mt_rand(0, $a);
|
|
|
|
});
|
|
|
|
|
2020-08-28 18:42:55 +02:00
|
|
|
echo $c;',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'impureCallableReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-28 18:42:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
* @return pure-callable():int
|
|
|
|
*/
|
|
|
|
function foo(): callable {
|
|
|
|
return function() {
|
|
|
|
echo "bar";
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2020-08-26 22:51:22 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|