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
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-01-13 18:40:01 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
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
|
|
|
|
*/
|
2019-07-19 05:36:39 +02:00
|
|
|
function f() {
|
2017-04-25 05:45:02 +02:00
|
|
|
run_function(
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function() use(&$data) {
|
|
|
|
$data = 1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
echo $data;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2019-07-19 05:36:39 +02:00
|
|
|
f();',
|
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(
|
|
|
|
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 07:05:11 +02:00
|
|
|
'varCallableParamReturnType' => [
|
2018-03-27 06:12:41 +02:00
|
|
|
'<?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");',
|
|
|
|
],
|
2019-02-22 06:50:41 +01:00
|
|
|
'callableMethodStringCallable' => [
|
2017-08-12 00:30:58 +02:00
|
|
|
'<?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");
|
2019-02-22 06:50:41 +01:00
|
|
|
foo(A::class . "::bar");',
|
|
|
|
],
|
|
|
|
'callableMethodArrayCallable' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function bar(string $a): string {
|
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(callable $c): void {}
|
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
foo(["A", "bar"]);
|
|
|
|
foo([A::class, "bar"]);
|
|
|
|
$a = new A();
|
|
|
|
foo([$a, "bar"]);',
|
|
|
|
],
|
2019-04-24 18:54:35 +02:00
|
|
|
'callableMethodArrayCallableMissingTypes' => [
|
|
|
|
'<?php
|
|
|
|
function foo(callable $c): void {}
|
|
|
|
|
|
|
|
/** @psalm-suppress MissingParamType */
|
|
|
|
function bar($a, $b) : void {
|
|
|
|
foo([$a, $b]);
|
|
|
|
}',
|
|
|
|
],
|
2017-08-15 01:30:11 +02:00
|
|
|
'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' => [
|
2019-06-16 15:42:34 +02: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
|
|
|
],
|
|
|
|
],
|
2018-06-11 22:54:20 +02:00
|
|
|
'arrayMapClosureVar' => [
|
|
|
|
'<?php
|
|
|
|
$mirror = function(int $i) : int { return $i; };
|
|
|
|
$a = array_map($mirror, [1, 2, 3]);',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a' => 'array{0: int, 1: int, 2: int}',
|
2018-06-11 22:54:20 +02:00
|
|
|
],
|
|
|
|
],
|
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(
|
2018-04-10 21:47:04 +02:00
|
|
|
function(int $type, string ...$args):string {
|
|
|
|
return "hello";
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
2018-03-21 14:04:07 +01:00
|
|
|
);',
|
|
|
|
],
|
2018-04-08 16:02:41 +02:00
|
|
|
'returnsTypedClosure' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure(int):int $f
|
|
|
|
* @param Closure(int):int $g
|
2018-04-08 18:03:35 +02:00
|
|
|
*
|
2018-04-08 16:02:41 +02:00
|
|
|
* @return Closure(int):int
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (int $x) use ($f, $g) : int {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-04-08 16:02:41 +02:00
|
|
|
],
|
2018-04-08 18:03:35 +02:00
|
|
|
'returnsTypedClosureWithClasses' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return Closure(C):A
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (C $x) use ($f, $g) : A {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = foo(
|
|
|
|
function(B $b) : A { return new A;},
|
|
|
|
function(C $c) : B { return new B;}
|
|
|
|
)(new C);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'returnsTypedClosureWithSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
class C2 extends C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
2019-04-12 06:44:10 +02:00
|
|
|
* @return Closure(C2):A
|
2018-04-08 18:03:35 +02:00
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
2019-04-12 06:44:10 +02:00
|
|
|
return function (C $x) use ($f, $g) : A {
|
2018-04-08 18:03:35 +02:00
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = foo(
|
|
|
|
function(B $b) : A { return new A;},
|
|
|
|
function(C $c) : B { return new B;}
|
|
|
|
)(new C2);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'returnsTypedClosureWithParentReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
class A2 extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A2 $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return Closure(C):A
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (C $x) use ($f, $g) : A2 {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = foo(
|
|
|
|
function(B $b) : A2 { return new A2;},
|
|
|
|
function(C $c) : B { return new B;}
|
|
|
|
)(new C);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'returnsTypedCallableFromClosure' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return callable(C):A
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : callable {
|
|
|
|
return function (C $x) use ($f, $g) : A {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = foo(
|
|
|
|
function(B $b) : A { return new A;},
|
|
|
|
function(C $c) : B { return new B;}
|
|
|
|
)(new C);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
|
|
|
],
|
|
|
|
],
|
2018-04-10 04:00:36 +02:00
|
|
|
'inferClosureTypeWithTypehint' => [
|
|
|
|
'<?php
|
|
|
|
$adder1 = function(int $i) : callable {
|
|
|
|
return function(int $j) use ($i) : int {
|
|
|
|
return $i + $j;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
$adder2 = function(int $i) {
|
|
|
|
return function(int $j) use ($i) : int {
|
|
|
|
return $i + $j;
|
|
|
|
};
|
|
|
|
};',
|
|
|
|
'assertions' => [
|
|
|
|
'$adder1' => 'Closure(int):Closure(int):int',
|
|
|
|
'$adder2' => 'Closure(int):Closure(int):int',
|
|
|
|
],
|
|
|
|
'error_levels' => ['MissingClosureReturnType'],
|
|
|
|
],
|
2018-04-10 19:05:31 +02:00
|
|
|
'inferArrayMapReturnTypeWithoutTypehints' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{0:string,1:string}[] $ret
|
|
|
|
* @return array{0:string,1:int}[]
|
|
|
|
*/
|
|
|
|
function f(array $ret) : array
|
|
|
|
{
|
|
|
|
return array_map(
|
2018-04-10 21:47:04 +02:00
|
|
|
/**
|
|
|
|
* @param array{0:string,1:string} $row
|
|
|
|
*/
|
|
|
|
function (array $row) {
|
|
|
|
return [
|
|
|
|
strval($row[0]),
|
|
|
|
intval($row[1]),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
$ret
|
|
|
|
);
|
2018-04-10 19:05:31 +02:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MissingClosureReturnType'],
|
|
|
|
],
|
|
|
|
'inferArrayMapReturnTypeWithTypehints' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{0:string,1:string}[] $ret
|
|
|
|
* @return array{0:string,1:int}[]
|
|
|
|
*/
|
|
|
|
function f(array $ret): array
|
|
|
|
{
|
|
|
|
return array_map(
|
2018-04-10 21:47:04 +02:00
|
|
|
/**
|
|
|
|
* @param array{0:string,1:string} $row
|
|
|
|
*/
|
|
|
|
function (array $row): array {
|
|
|
|
return [
|
|
|
|
strval($row[0]),
|
|
|
|
intval($row[1]),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
$ret
|
|
|
|
);
|
2018-04-10 19:05:31 +02:00
|
|
|
}',
|
|
|
|
],
|
2018-04-12 02:27:25 +02:00
|
|
|
'allowVoidCallable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable():void $p
|
|
|
|
*/
|
|
|
|
function doSomething($p): void {}
|
|
|
|
doSomething(function(): bool { return false; });',
|
|
|
|
],
|
2018-05-09 01:49:25 +02:00
|
|
|
'callableProperties' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/** @psalm-var callable():bool */
|
|
|
|
private $callable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-param 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();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'invokableProperties' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __invoke(): bool { return true; }
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
/** @var A $invokable */
|
|
|
|
private $invokable;
|
|
|
|
|
|
|
|
public function __construct(A $invokable) {
|
|
|
|
$this->invokable = $invokable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function callTheInvokableDirectly(): bool {
|
|
|
|
return ($this->invokable)();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function callTheInvokableIndirectly(): bool {
|
|
|
|
$r = $this->invokable;
|
|
|
|
return $r();
|
|
|
|
}
|
|
|
|
}',
|
2018-05-20 18:47:18 +02:00
|
|
|
],
|
2018-05-20 19:20:15 +02:00
|
|
|
'PHP71-mirrorCallableParams' => [
|
2018-05-20 18:47:18 +02:00
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
use Closure;
|
|
|
|
/** @param Closure(int):bool $c */
|
|
|
|
function acceptsIntToBool(Closure $c): void {}
|
|
|
|
|
2018-09-17 18:15:45 +02:00
|
|
|
acceptsIntToBool(Closure::fromCallable(function(int $n): bool { return $n > 0; }));',
|
2018-05-20 18:47:18 +02:00
|
|
|
],
|
2018-05-25 10:21:54 +02:00
|
|
|
'singleLineClosures' => [
|
|
|
|
'<?php
|
2018-09-17 18:15:45 +02:00
|
|
|
$a = function() : Closure { return function() : string { return "hello"; }; };
|
2018-05-25 10:21:54 +02:00
|
|
|
$b = $a()();',
|
|
|
|
'assertions' => [
|
2018-12-08 20:10:06 +01:00
|
|
|
'$a' => 'Closure():Closure():string(hello)',
|
2018-05-25 10:21:54 +02:00
|
|
|
'$b' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2018-06-09 05:54:07 +02:00
|
|
|
'nullableReturnTypeShorthand' => [
|
|
|
|
'<?php
|
2019-02-11 07:57:28 +01:00
|
|
|
class A {}
|
2018-06-09 05:54:07 +02:00
|
|
|
/** @param callable(mixed):?A $a */
|
|
|
|
function foo(callable $a): void {}',
|
|
|
|
],
|
2018-06-11 23:00:26 +02:00
|
|
|
'voidReturningArrayMap' => [
|
|
|
|
'<?php
|
|
|
|
array_map(
|
|
|
|
function(int $i) : void {
|
|
|
|
echo $i;
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
|
|
|
);',
|
|
|
|
],
|
2019-01-04 14:04:19 +01:00
|
|
|
'callablesCanBeObjects' => [
|
|
|
|
'<?php
|
|
|
|
function foo(callable $c) : void {
|
|
|
|
if (is_object($c)) {
|
|
|
|
$c();
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-04 14:04:19 +01:00
|
|
|
],
|
|
|
|
'objectsCanBeCallable' => [
|
|
|
|
'<?php
|
|
|
|
function foo(object $c) : void {
|
|
|
|
if (is_callable($c)) {
|
|
|
|
$c();
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-04 14:04:19 +01:00
|
|
|
],
|
|
|
|
'unionCanBeCallable' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {
|
|
|
|
public function __invoke() : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param A|B $c
|
|
|
|
*/
|
|
|
|
function foo($c) : void {
|
|
|
|
if (is_callable($c)) {
|
|
|
|
$c();
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-04 14:04:19 +01:00
|
|
|
],
|
2019-01-20 01:49:58 +01:00
|
|
|
'goodCallableArgs' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable(string,string):int $_p
|
|
|
|
*/
|
|
|
|
function f(callable $_p): void {}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public static function m(string $a, string $b): int { return $a <=> $b; }
|
|
|
|
}
|
|
|
|
|
|
|
|
f("strcmp");
|
|
|
|
f([new C, "m"]);
|
2019-03-23 19:27:54 +01:00
|
|
|
f([C::class, "m"]);',
|
2019-01-20 01:49:58 +01:00
|
|
|
],
|
2019-02-01 13:50:48 +01:00
|
|
|
'callableWithSpaces' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable(string, string) : int $p
|
|
|
|
*/
|
2019-03-23 19:27:54 +01:00
|
|
|
function f(callable $p): void {}',
|
2019-02-01 13:50:48 +01:00
|
|
|
],
|
2019-01-20 01:49:58 +01:00
|
|
|
'fileExistsCallable' => [
|
|
|
|
'<?php
|
|
|
|
/** @return string[] */
|
|
|
|
function foo(string $prospective_file_path) : array {
|
|
|
|
return array_filter(
|
|
|
|
glob($prospective_file_path),
|
|
|
|
"file_exists"
|
|
|
|
);
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-20 01:49:58 +01:00
|
|
|
],
|
2019-01-22 00:01:15 +01:00
|
|
|
'PHP71-closureFromCallableInvokableNamedClass' => [
|
2019-01-21 23:08:12 +01:00
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
use Closure;
|
|
|
|
|
|
|
|
/** @param Closure(int):bool $c */
|
|
|
|
function acceptsIntToBool(Closure $c): void {}
|
|
|
|
|
|
|
|
class NamedInvokable {
|
|
|
|
public function __invoke(int $p): bool {
|
|
|
|
return $p > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
acceptsIntToBool(Closure::fromCallable(new NamedInvokable));',
|
2019-01-21 23:08:12 +01:00
|
|
|
],
|
2019-01-22 00:01:15 +01:00
|
|
|
'PHP71-closureFromCallableInvokableAnonymousClass' => [
|
2019-01-21 23:08:12 +01:00
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
use Closure;
|
|
|
|
|
|
|
|
/** @param Closure(int):bool $c */
|
|
|
|
function acceptsIntToBool(Closure $c): void {}
|
|
|
|
|
|
|
|
$anonInvokable = new class {
|
|
|
|
public function __invoke(int $p):bool {
|
|
|
|
return $p > 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
acceptsIntToBool(Closure::fromCallable($anonInvokable));',
|
2019-01-21 23:08:12 +01:00
|
|
|
],
|
2019-02-17 19:14:59 +01:00
|
|
|
'callableSelfArg' => [
|
|
|
|
'<?php
|
2019-04-12 06:44:10 +02:00
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$c = new C();
|
|
|
|
|
|
|
|
$b->func2(function(B $x): void {});
|
|
|
|
$c->func2(function(B $x): void {});
|
|
|
|
|
2019-03-08 00:02:31 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
2019-02-17 19:14:59 +01:00
|
|
|
/**
|
|
|
|
* @param callable(self) $f
|
|
|
|
*/
|
|
|
|
function func2(callable $f): void {
|
|
|
|
$f($this);
|
|
|
|
}
|
2019-04-12 06:44:10 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'callableParentArg' => [
|
|
|
|
'<?php
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$c = new C();
|
2019-02-17 19:14:59 +01:00
|
|
|
|
2019-04-12 06:44:10 +02:00
|
|
|
$b->func3(function(A $x): void {});
|
|
|
|
$c->func3(function(A $x): void {});
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
2019-02-17 19:14:59 +01:00
|
|
|
/**
|
|
|
|
* @param callable(parent) $f
|
|
|
|
*/
|
|
|
|
function func3(callable $f): void {
|
|
|
|
$f($this);
|
|
|
|
}
|
2019-04-12 06:44:10 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'callableStaticArg' => [
|
|
|
|
'<?php
|
2019-03-08 00:02:31 +01:00
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$c = new C();
|
|
|
|
|
|
|
|
$b->func1(function(B $x): void {});
|
|
|
|
$c->func1(function(C $x): void {});
|
2019-04-12 06:44:10 +02:00
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @param callable(static) $f
|
|
|
|
*/
|
|
|
|
function func1(callable $f): void {
|
|
|
|
$f($this);
|
|
|
|
}
|
|
|
|
}',
|
2019-03-08 00:02:31 +01:00
|
|
|
],
|
|
|
|
'callableSelfReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @param callable():static $f
|
|
|
|
*/
|
|
|
|
function func1(callable $f): void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable():self $f
|
|
|
|
*/
|
|
|
|
function func2(callable $f): void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable():parent $f
|
|
|
|
*/
|
|
|
|
function func3(callable $f): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$c = new C();
|
|
|
|
|
|
|
|
$b->func1(function(): B { return new B(); });
|
|
|
|
$c->func1(function(): C { return new C(); });
|
|
|
|
$b->func2(function(): B { return new B(); });
|
|
|
|
$c->func2(function(): B { return new B(); });',
|
2019-02-17 19:14:59 +01:00
|
|
|
],
|
2019-02-19 07:25:36 +01:00
|
|
|
'selfArrayMapCallableWrongClass' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function __construct(int $param) {}
|
|
|
|
|
|
|
|
public static function foo(int $param): Foo {
|
|
|
|
return new self($param);
|
|
|
|
}
|
|
|
|
public static function baz(int $param): self {
|
|
|
|
return new self($param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
/**
|
|
|
|
* @return array<int, Foo>
|
|
|
|
*/
|
|
|
|
public function bar() {
|
|
|
|
return array_map([Foo::class, "foo"], [1,2,3]);
|
|
|
|
}
|
|
|
|
/** @return array<int, Foo> */
|
|
|
|
public function bat() {
|
|
|
|
return array_map([Foo::class, "baz"], [1]);
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-19 07:25:36 +01:00
|
|
|
],
|
2019-02-22 06:50:41 +01:00
|
|
|
'dynamicCallableArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $value = "default";
|
|
|
|
|
|
|
|
private function modify(string $name, string $value): void {
|
|
|
|
call_user_func([$this, "modify" . $name], $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function modifyFoo(string $value): void {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-22 06:50:41 +01:00
|
|
|
],
|
2019-03-06 00:27:25 +01:00
|
|
|
'PHP71-publicCallableFromInside' => [
|
2019-03-06 00:02:42 +01:00
|
|
|
'<?php
|
|
|
|
class Base {
|
|
|
|
public function publicMethod() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Example extends Base {
|
|
|
|
public function test() : Closure {
|
|
|
|
return Closure::fromCallable([$this, "publicMethod"]);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-03-06 00:27:25 +01:00
|
|
|
'PHP71-protectedCallableFromInside' => [
|
2019-03-06 00:02:42 +01:00
|
|
|
'<?php
|
|
|
|
class Base {
|
|
|
|
protected function protectedMethod() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Example extends Base {
|
|
|
|
public function test() : Closure {
|
|
|
|
return Closure::fromCallable([$this, "protectedMethod"]);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-03-07 00:24:52 +01:00
|
|
|
'callableIsArrayAssertion' => [
|
|
|
|
'<?php
|
|
|
|
function foo(callable $c) : void {
|
|
|
|
if (is_array($c)) {
|
|
|
|
echo $c[1];
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-07 00:24:52 +01:00
|
|
|
],
|
2019-03-11 16:39:33 +01:00
|
|
|
'callableOrArrayIsArrayAssertion' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable|array $c
|
|
|
|
*/
|
|
|
|
function foo($c) : void {
|
2019-06-21 01:46:42 +02:00
|
|
|
if (is_array($c) && is_string($c[1])) {
|
|
|
|
echo $c[1];
|
2019-03-11 16:39:33 +01:00
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-03-11 16:39:33 +01:00
|
|
|
],
|
2019-04-12 06:44:10 +02:00
|
|
|
'allowCallableWithNarrowerReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure():A $x
|
|
|
|
*/
|
|
|
|
function accept_closure($x) : void {
|
|
|
|
$x();
|
|
|
|
}
|
|
|
|
accept_closure(
|
|
|
|
function () : B {
|
|
|
|
return new B();
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
);',
|
2019-04-12 06:44:10 +02:00
|
|
|
],
|
|
|
|
'allowCallableWithWiderParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B $a):A $x
|
|
|
|
*/
|
|
|
|
function accept_closure($x) : void {
|
|
|
|
$x(new B());
|
|
|
|
}
|
|
|
|
accept_closure(
|
|
|
|
function (A $a) : A {
|
|
|
|
return $a;
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
);',
|
2019-04-12 06:44:10 +02:00
|
|
|
],
|
|
|
|
'allowCallableWithOptionalArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure():int $x
|
|
|
|
*/
|
|
|
|
function accept_closure($x) : void {
|
|
|
|
$x();
|
|
|
|
}
|
|
|
|
accept_closure(
|
|
|
|
function (int $x = 5) : int {
|
|
|
|
return $x;
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
);',
|
2019-04-12 06:44:10 +02:00
|
|
|
],
|
2019-08-12 21:04:43 +02:00
|
|
|
'dontInferMethodIdWhenFormatDoesntFit' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string|callable $p */
|
|
|
|
function f($p): array {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
f("#b::a");'
|
|
|
|
],
|
2019-08-16 16:32:03 +02:00
|
|
|
'removeCallableAssertionAfterReassignment' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $key) : void {
|
|
|
|
$setter = "a" . $key;
|
|
|
|
if (is_callable($setter)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$setter = "b" . $key;
|
|
|
|
if (is_callable($setter)) {}
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-01-13 18:40:01 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
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',
|
2019-01-05 18:59:12 +01:00
|
|
|
'error_levels' => ['UndefinedClass', 'MixedInferredReturnType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-10-17 19:22:57 +02:00
|
|
|
'undefinedCallableMethodFullString' => [
|
2017-08-12 00:30:58 +02:00
|
|
|
'<?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',
|
|
|
|
],
|
2018-10-17 19:22:57 +02:00
|
|
|
'undefinedCallableMethodClassConcat' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function bar(string $a): string {
|
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(callable $c): void {}
|
|
|
|
|
|
|
|
foo(A::class . "::barr");',
|
|
|
|
'error_message' => 'UndefinedMethod',
|
|
|
|
],
|
|
|
|
'undefinedCallableMethodArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function bar(string $a): string {
|
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(callable $c): void {}
|
|
|
|
|
|
|
|
foo([A::class, "::barr"]);',
|
2019-02-22 06:50:41 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2018-10-17 19:22:57 +02:00
|
|
|
],
|
|
|
|
'undefinedCallableMethodArrayWithoutClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function bar(string $a): string {
|
|
|
|
return $a . "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(callable $c): void {}
|
|
|
|
|
|
|
|
foo(["A", "::barr"]);',
|
2019-02-22 06:50:41 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2018-10-17 19:22:57 +02:00
|
|
|
],
|
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 =
|
2019-02-10 22:15:52 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $bar
|
|
|
|
* @psalm-suppress MixedFunctionCall
|
|
|
|
*/
|
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";
|
2018-09-17 18:15:45 +02:00
|
|
|
};',
|
2017-12-22 18:56:59 +01:00
|
|
|
'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',
|
|
|
|
],
|
2018-04-08 18:03:35 +02:00
|
|
|
'returnsTypedClosureWithBadReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure(int):int $f
|
|
|
|
* @param Closure(int):int $g
|
|
|
|
*
|
|
|
|
* @return Closure(int):string
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (int $x) use ($f, $g) : int {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'returnsTypedCallableWithBadReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure(int):int $f
|
|
|
|
* @param Closure(int):int $g
|
|
|
|
*
|
|
|
|
* @return callable(int):string
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : callable {
|
|
|
|
return function (int $x) use ($f, $g) : int {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'returnsTypedClosureWithBadParamType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure(int):int $f
|
|
|
|
* @param Closure(int):int $g
|
|
|
|
*
|
|
|
|
* @return Closure(string):int
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (int $x) use ($f, $g) : int {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'returnsTypedCallableWithBadParamType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure(int):int $f
|
|
|
|
* @param Closure(int):int $g
|
|
|
|
*
|
|
|
|
* @return callable(string):int
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : callable {
|
|
|
|
return function (int $x) use ($f, $g) : int {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'returnsTypedClosureWithBadCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
class D {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return Closure(C):A
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (int $x) use ($f, $g) : int {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
|
|
|
'returnsTypedClosureWithSubclassParam' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
class C2 extends C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
2019-04-12 06:44:10 +02:00
|
|
|
* @return Closure(C):A
|
2018-04-08 18:03:35 +02:00
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
2019-04-12 06:44:10 +02:00
|
|
|
return function (C2 $x) use ($f, $g) : A {
|
2018-04-08 18:03:35 +02:00
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
|
|
|
'returnsTypedClosureWithSubclassReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
class A2 extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return Closure(C):A2
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : Closure {
|
|
|
|
return function (C $x) use ($f, $g) : A {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
|
|
|
'returnsTypedClosureFromCallable' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return callable(C):A
|
|
|
|
*/
|
|
|
|
function foo(Closure $f, Closure $g) : callable {
|
|
|
|
return function (C $x) use ($f, $g) : A {
|
|
|
|
return $f($g($x));
|
2018-09-17 18:15:45 +02:00
|
|
|
};
|
2018-04-08 18:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure(B):A $f
|
|
|
|
* @param Closure(C):B $g
|
|
|
|
*
|
|
|
|
* @return Closure(C):A
|
|
|
|
*/
|
|
|
|
function bar(Closure $f, Closure $g) : Closure {
|
|
|
|
return foo($f, $g);
|
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
2018-05-08 23:42:02 +02:00
|
|
|
'undefinedVariable' => [
|
|
|
|
'<?php
|
|
|
|
$a = function() use ($i) {};',
|
|
|
|
'error_message' => 'UndefinedVariable',
|
|
|
|
],
|
2018-06-11 23:00:26 +02:00
|
|
|
'voidReturningArrayMap' => [
|
|
|
|
'<?php
|
|
|
|
$arr = array_map(
|
|
|
|
function(int $i) : void {
|
|
|
|
echo $i;
|
|
|
|
},
|
|
|
|
[1, 2, 3]
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($arr as $a) {
|
|
|
|
if ($a) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2019-01-20 01:49:58 +01:00
|
|
|
'checkCallableTypeString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable(int,int):int $_p
|
|
|
|
*/
|
|
|
|
function f(callable $_p): void {}
|
|
|
|
|
|
|
|
f("strcmp");',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'checkCallableTypeArrayInstanceFirstArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable(int,int):int $_p
|
|
|
|
*/
|
|
|
|
function f(callable $_p): void {}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public static function m(string $a, string $b): int { return $a <=> $b; }
|
|
|
|
}
|
|
|
|
|
|
|
|
f([new C, "m"]);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'checkCallableTypeArrayClassStringFirstArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param callable(int,int):int $_p
|
|
|
|
*/
|
|
|
|
function f(callable $_p): void {}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public static function m(string $a, string $b): int { return $a <=> $b; }
|
|
|
|
}
|
|
|
|
|
|
|
|
f([C::class, "m"]);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2019-01-22 00:01:15 +01:00
|
|
|
'PHP71-closureFromCallableInvokableNamedClassWrongArgs' => [
|
2019-01-21 23:08:12 +01:00
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
use Closure;
|
|
|
|
|
|
|
|
/** @param Closure(string):bool $c */
|
|
|
|
function acceptsIntToBool(Closure $c): void {}
|
|
|
|
|
|
|
|
class NamedInvokable {
|
|
|
|
public function __invoke(int $p): bool {
|
|
|
|
return $p > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptsIntToBool(Closure::fromCallable(new NamedInvokable));',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2019-01-23 19:02:07 +01:00
|
|
|
'undefinedClassForCallable' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function __construct(UndefinedClass $o) {}
|
|
|
|
}
|
|
|
|
new Foo(function() : void {});',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2019-01-30 17:36:21 +01:00
|
|
|
'useDuplicateName' => [
|
|
|
|
'<?php
|
|
|
|
$foo = "bar";
|
|
|
|
|
|
|
|
$a = function (string $foo) use ($foo) : string {
|
|
|
|
return $foo;
|
|
|
|
};',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'DuplicateParam',
|
2019-01-30 17:36:21 +01:00
|
|
|
],
|
2019-02-01 14:58:52 +01:00
|
|
|
'callableWithSpaceAfterColonBadVarArg' => [
|
2019-02-01 13:50:48 +01:00
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @var callable(string, string): bool $p
|
|
|
|
*/
|
|
|
|
public $p;
|
|
|
|
|
2019-02-01 14:58:52 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->p = function (string $s, string $t): stdClass {
|
|
|
|
return new stdClass;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'callableWithSpaceBeforeColonBadVarArg' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @var callable(string, string) :bool $p
|
|
|
|
*/
|
|
|
|
public $p;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->p = function (string $s, string $t): stdClass {
|
|
|
|
return new stdClass;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'callableWithSpacesEitherSideOfColonBadVarArg' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @var callable(string, string) : bool $p
|
|
|
|
*/
|
|
|
|
public $p;
|
|
|
|
|
2019-02-01 13:50:48 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->p = function (string $s, string $t): stdClass {
|
|
|
|
return new stdClass;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
2019-02-15 18:02:44 +01:00
|
|
|
'badArrayMapArrayCallable' => [
|
|
|
|
'<?php
|
|
|
|
class one { public function two(string $_p): void {} }
|
|
|
|
array_map(["two", "three"], ["one", "two"]);',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-02-15 18:02:44 +01:00
|
|
|
],
|
2019-03-06 00:27:25 +01:00
|
|
|
'PHP71-privateCallable' => [
|
2019-03-06 00:02:42 +01:00
|
|
|
'<?php
|
|
|
|
class Base {
|
|
|
|
private function privateMethod() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Example extends Base {
|
|
|
|
public function test() : Closure {
|
|
|
|
return Closure::fromCallable([$this, "privateMethod"]);
|
|
|
|
}
|
|
|
|
}',
|
2019-03-12 17:58:04 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-03-06 00:02:42 +01:00
|
|
|
],
|
2019-04-12 06:44:10 +02:00
|
|
|
'prohibitCallableWithRequiredArg' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param Closure():int $x
|
|
|
|
*/
|
|
|
|
function accept_closure($x) : void {
|
|
|
|
$x();
|
|
|
|
}
|
|
|
|
accept_closure(
|
|
|
|
function (int $x) : int {
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
);',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-04-12 06:44:10 +02:00
|
|
|
],
|
2019-09-01 16:10:38 +02:00
|
|
|
'useClosureDocblockType' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
function takesA(A $_a) : void {}
|
|
|
|
function takesB(B $_b) : void {}
|
|
|
|
|
|
|
|
$getAButReallyB = /** @return A */ function() {
|
|
|
|
return new B;
|
|
|
|
};
|
|
|
|
|
|
|
|
takesA($getAButReallyB());
|
|
|
|
takesB($getAButReallyB());',
|
|
|
|
'error_message' => 'ArgumentTypeCoercion - src/somefile.php:13:28 - Argument 1 of takesB expects B, parent type A provided',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|