2016-10-30 03:17:46 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2018-03-20 18:24:16 +01:00
|
|
|
class CallableTest extends TestCase
|
2016-10-30 03:17:46 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-01-13 18:40:01 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-01-13 18:40:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'byRefUseVar' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function run_function(\Closure $fnc) {
|
|
|
|
$fnc();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
// here we have to make sure $data exists as a side-effect of calling `run_function`
|
|
|
|
// because it could exist depending on how run_function is implemented
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
function fn() {
|
|
|
|
run_function(
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function() use(&$data) {
|
|
|
|
$data = 1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
echo $data;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
fn();',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'inferredArg' => [
|
|
|
|
'<?php
|
|
|
|
$bar = ["foo", "bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bam = array_map(
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingClosureReturnType
|
|
|
|
*/
|
|
|
|
function(string $a) {
|
|
|
|
return $a . "blah";
|
|
|
|
},
|
|
|
|
$bar
|
2017-05-27 02:05:57 +02:00
|
|
|
);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'varReturnType' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$add_one = function(int $a): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a + 1;
|
|
|
|
};
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = $add_one(1);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'int',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-03-27 06:12:41 +02:00
|
|
|
'varReturnType' => [
|
|
|
|
'<?php
|
|
|
|
$add_one = function(int $a): int {
|
|
|
|
return $a + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable(int) : int $c
|
|
|
|
*/
|
|
|
|
function bar(callable $c) : int {
|
|
|
|
return $c(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bar($add_one);',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'callableToClosure' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
function foo() {
|
2018-01-11 21:50:45 +01:00
|
|
|
return function(string $a): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a . "blah";
|
|
|
|
};
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'callable' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo (string)$c();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'callableClass' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __invoke(): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
return "You ran?";
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo (string)$c();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foo(new C());
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$c2 = new C();
|
2017-05-27 02:05:57 +02:00
|
|
|
$c2();',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'correctParamType' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$take_string = function(string $s): string { return $s; };
|
2017-05-27 02:05:57 +02:00
|
|
|
$take_string("string");',
|
|
|
|
],
|
2017-08-12 00:30:58 +02:00
|
|
|
'callableMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function bar(string $a): string {
|
2017-08-12 00:30:58 +02:00
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {}
|
2017-08-12 00:30:58 +02:00
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
foo("A::bar");
|
|
|
|
foo(["A", "bar"]);
|
|
|
|
foo([A::class, "bar"]);
|
|
|
|
$a = new A();
|
|
|
|
foo([$a, "bar"]);',
|
|
|
|
],
|
|
|
|
'arrayMapCallableMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function bar(string $a): string {
|
2017-08-15 01:30:11 +02:00
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function baz(string $a): string {
|
2017-08-15 01:30:11 +02:00
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = array_map("A::bar", ["one", "two"]);
|
|
|
|
$b = array_map(["A", "bar"], ["one", "two"]);
|
|
|
|
$c = array_map([A::class, "bar"], ["one", "two"]);
|
|
|
|
$d = array_map([new A(), "bar"], ["one", "two"]);
|
|
|
|
$a_instance = new A();
|
|
|
|
$e = array_map([$a_instance, "bar"], ["one", "two"]);
|
|
|
|
$f = array_map("baz", ["one", "two"]);',
|
|
|
|
'assertions' => [
|
2018-02-26 17:01:30 +01:00
|
|
|
'$a' => 'array{0:string, 1:string}',
|
|
|
|
'$b' => 'array{0:string, 1:string}',
|
|
|
|
'$c' => 'array{0:string, 1:string}',
|
|
|
|
'$d' => 'array{0:string, 1:string}',
|
|
|
|
'$e' => 'array{0:string, 1:string}',
|
|
|
|
'$f' => 'array{0:string, 1:string}',
|
2017-08-15 01:30:11 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayCallableMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function bar(string $a): string {
|
2017-08-15 01:30:11 +02:00
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|
|
|
foo(["A", "bar"]);',
|
2017-08-12 00:30:58 +02:00
|
|
|
],
|
|
|
|
'callableFunction' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {}
|
2017-08-12 00:30:58 +02:00
|
|
|
|
|
|
|
foo("trim");',
|
|
|
|
],
|
2017-08-14 21:46:01 +02:00
|
|
|
'inlineCallableFunction' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(): void {
|
|
|
|
function foobar(int $a, int $b): int {
|
2017-08-14 21:46:01 +02:00
|
|
|
return $a > $b ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = [5, 4, 3, 1, 2];
|
|
|
|
|
2017-08-18 23:23:12 +02:00
|
|
|
usort($arr, "fooBar");
|
2017-08-14 21:46:01 +02:00
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-11-17 02:06:00 +01:00
|
|
|
'closureSelf' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
2017-11-22 03:50:39 +01:00
|
|
|
/**
|
|
|
|
* @var self[]
|
|
|
|
*/
|
|
|
|
private $subitems;
|
|
|
|
|
2017-11-17 02:06:00 +01:00
|
|
|
/**
|
|
|
|
* @param self[] $in
|
|
|
|
*/
|
|
|
|
public function __construct(array $in = [])
|
|
|
|
{
|
|
|
|
array_map(function(self $i): self { return $i; }, $in);
|
2017-11-22 03:50:39 +01:00
|
|
|
|
|
|
|
$this->subitems = array_map(
|
|
|
|
function(self $i): self {
|
|
|
|
return $i;
|
|
|
|
},
|
|
|
|
$in
|
|
|
|
);
|
2017-11-17 02:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new A([new A, new A]);',
|
|
|
|
],
|
2018-03-20 13:58:05 +01:00
|
|
|
'possiblyUndefinedFunction' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|callable $middlewareOrPath
|
|
|
|
*/
|
|
|
|
function pipe($middlewareOrPath, ?callable $middleware = null): void { }
|
|
|
|
|
|
|
|
pipe("zzzz", function() : void {});',
|
|
|
|
],
|
2018-03-20 14:30:37 +01:00
|
|
|
'callableWithNonInvokable' => [
|
|
|
|
'<?php
|
|
|
|
function asd(): void {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable|B $p
|
|
|
|
*/
|
|
|
|
function passes($p): void {}
|
|
|
|
|
|
|
|
passes("asd");',
|
|
|
|
],
|
2018-03-20 18:24:16 +01:00
|
|
|
'callableWithInvokable' => [
|
2018-03-20 14:30:37 +01:00
|
|
|
'<?php
|
|
|
|
function asd(): void {}
|
|
|
|
class A { public function __invoke(): void {} }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable|A $p
|
|
|
|
*/
|
|
|
|
function fails($p): void {}
|
|
|
|
|
|
|
|
fails("asd");',
|
|
|
|
],
|
2018-03-20 18:24:16 +01:00
|
|
|
'isCallableArray' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
public function callMeMaybe(string $method): void
|
|
|
|
{
|
|
|
|
$handleMethod = [$this, $method];
|
|
|
|
|
|
|
|
if (is_callable($handleMethod)) {
|
|
|
|
$handleMethod();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo(): void {}
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
$a->callMeMaybe("foo");',
|
|
|
|
],
|
|
|
|
'isCallableString' => [
|
|
|
|
'<?php
|
|
|
|
function foo(): void {}
|
|
|
|
|
|
|
|
function callMeMaybe(string $method): void {
|
|
|
|
if (is_callable($method)) {
|
|
|
|
$method();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callMeMaybe("foo");',
|
|
|
|
],
|
2018-03-21 14:04:07 +01:00
|
|
|
'arrayMapVariadicClosureArg' => [
|
|
|
|
'<?php
|
|
|
|
$a = array_map(
|
|
|
|
function(int $type, string ...$args):string {
|
|
|
|
return "hello";
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
|
|
|
);',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 18:40:01 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-01-13 18:40:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'wrongArg' => [
|
|
|
|
'<?php
|
|
|
|
$bar = ["foo", "bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bam = array_map(
|
2018-01-11 21:50:45 +01:00
|
|
|
function(int $a): int {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a + 1;
|
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'noReturn' => [
|
|
|
|
'<?php
|
|
|
|
$bar = ["foo", "bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bam = array_map(
|
2018-01-11 21:50:45 +01:00
|
|
|
function(string $a): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedCallableClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function getFoo(): Foo
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return new Foo([]);
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-09-02 17:18:56 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $argOne
|
|
|
|
* @param mixed $argTwo
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function bar($argOne, $argTwo)
|
|
|
|
{
|
|
|
|
$this->getFoo()($argOne, $argTwo);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidFunctionCall',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['UndefinedClass'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-08-12 00:30:58 +02:00
|
|
|
'undefinedCallableMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function bar(string $a): string {
|
2017-08-12 00:30:58 +02:00
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {}
|
2017-08-12 00:30:58 +02:00
|
|
|
|
|
|
|
foo("A::barr");',
|
|
|
|
'error_message' => 'UndefinedMethod',
|
|
|
|
],
|
2017-08-12 00:48:58 +02:00
|
|
|
'undefinedCallableMethodClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function bar(string $a): string {
|
2017-08-12 00:48:58 +02:00
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {}
|
2017-08-12 00:48:58 +02:00
|
|
|
|
|
|
|
foo("B::bar");',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2017-08-12 00:30:58 +02:00
|
|
|
'undefinedCallableFunction' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(callable $c): void {}
|
2017-08-12 00:30:58 +02:00
|
|
|
|
|
|
|
foo("trime");',
|
|
|
|
'error_message' => 'UndefinedFunction',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'possiblyNullFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @var Closure|null $foo
|
|
|
|
*/
|
|
|
|
$foo = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
|
|
|
|
2017-09-02 17:18:56 +02:00
|
|
|
$foo =
|
|
|
|
/** @param mixed $bar */
|
2018-01-11 21:50:45 +01:00
|
|
|
function ($bar) use (&$foo): string
|
2017-09-02 17:18:56 +02:00
|
|
|
{
|
|
|
|
if (is_array($bar)) {
|
|
|
|
return $foo($bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bar;
|
|
|
|
};',
|
2018-01-12 18:33:26 +01:00
|
|
|
'error_message' => 'MixedReturnStatement',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'stringFunctionCall' => [
|
|
|
|
'<?php
|
|
|
|
$bad_one = "hello";
|
|
|
|
$a = $bad_one(1);',
|
2018-02-12 02:56:34 +01:00
|
|
|
'error_message' => 'MixedAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'wrongParamType' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
$take_string = function(string $s): string { return $s; };
|
2017-04-25 05:45:02 +02:00
|
|
|
$take_string(42);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2017-12-22 18:56:59 +01:00
|
|
|
'missingClosureReturnType' => [
|
|
|
|
'<?php
|
|
|
|
$a = function() {
|
|
|
|
return "foo";
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingClosureReturnType',
|
|
|
|
],
|
2018-03-27 06:12:41 +02:00
|
|
|
'wrongCallableReturnType' => [
|
|
|
|
'<?php
|
|
|
|
$add_one = function(int $a): int {
|
|
|
|
return $a + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable(int) : int $c
|
|
|
|
*/
|
|
|
|
function bar(callable $c) : string {
|
|
|
|
return $c(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bar($add_one);',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|