1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/CallableTest.php

967 lines
32 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
class CallableTest extends TestCase
{
2018-11-06 03:57:36 +01:00
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
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()
{
return [
'byRefUseVar' => [
'<?php
/** @return void */
function run_function(\Closure $fnc) {
$fnc();
}
2017-06-29 16:22:49 +02:00
/**
* @return void
* @psalm-suppress MixedArgument
*/
function f() {
run_function(
/**
* @return void
*/
function() use(&$data) {
$data = 1;
}
);
echo $data;
}
2017-06-29 16:22:49 +02:00
f();',
],
'inferredArg' => [
'<?php
$bar = ["foo", "bar"];
2017-06-29 16:22:49 +02:00
$bam = array_map(
function(string $a) {
return $a . "blah";
},
$bar
2017-05-27 02:05:57 +02:00
);',
],
'inferredArgArrowFunction' => [
'<?php
$bar = ["foo", "bar"];
$bam = array_map(
fn(string $a) => $a . "blah",
$bar
);',
],
'varReturnType' => [
'<?php
$add_one = function(int $a) : int {
return $a + 1;
};
2017-06-29 16:22:49 +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
],
],
'varReturnTypeArray' => [
'<?php
$add_one = fn(int $a) : int => $a + 1;
$a = $add_one(1);',
'assertions' => [
'$a' => 'int',
],
],
2018-03-27 07:05:11 +02:00
'varCallableParamReturnType' => [
'<?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);',
],
'callableToClosure' => [
'<?php
/**
* @return callable
*/
function foo() {
2018-01-11 21:50:45 +01:00
return function(string $a): string {
return $a . "blah";
};
2017-05-27 02:05:57 +02:00
}',
],
'callableToClosureArrow' => [
'<?php
/**
* @return callable
*/
function foo() {
return fn(string $a): string => $a . "blah";
}',
],
'callable' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {
echo (string)$c();
2017-05-27 02:05:57 +02:00
}',
],
'callableClass' => [
'<?php
class C {
2018-01-11 21:50:45 +01:00
public function __invoke(): string {
return "You ran?";
}
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {
echo (string)$c();
}
2017-06-29 16:22:49 +02:00
foo(new C());
2017-06-29 16:22:49 +02:00
$c2 = new C();
2017-05-27 02:05:57 +02:00
$c2();',
],
'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");',
],
'callableMethodStringCallable' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public static function bar(string $a): string {
return $a . "b";
}
}
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {}
foo("A::bar");
foo(A::class . "::bar");',
],
'callableMethodArrayCallable' => [
'<?php
class A {
public static function bar(string $a): string {
return $a . "b";
}
}
function foo(callable $c): void {}
foo(["A", "bar"]);
foo([A::class, "bar"]);
$a = new A();
foo([$a, "bar"]);',
],
'callableMethodArrayCallableMissingTypes' => [
'<?php
function foo(callable $c): void {}
/** @psalm-suppress MissingParamType */
function bar($a, $b) : void {
foo([$a, $b]);
}',
],
'arrayMapCallableMethod' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public static function bar(string $a): string {
return $a . "b";
}
}
2018-01-11 21:50:45 +01:00
function baz(string $a): string {
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}',
],
],
'arrayCallableMethod' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public static function bar(string $a): string {
return $a . "b";
}
}
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {}
foo(["A", "bar"]);',
],
'callableFunction' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {}
foo("trim");',
],
'inlineCallableFunction' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
function bar(): void {
function foobar(int $a, int $b): int {
return $a > $b ? 1 : 0;
}
$arr = [5, 4, 3, 1, 2];
2017-08-18 23:23:12 +02:00
usort($arr, "fooBar");
}
}',
],
'closureSelf' => [
'<?php
class A
{
/**
* @var self[]
*/
private $subitems;
/**
* @param self[] $in
*/
public function __construct(array $in = [])
{
array_map(function(self $i): self { return $i; }, $in);
$this->subitems = array_map(
function(self $i): self {
return $i;
},
$in
);
}
}
new A([new A, new A]);',
],
'possiblyUndefinedFunction' => [
'<?php
/**
* @param string|callable $middlewareOrPath
*/
function pipe($middlewareOrPath, ?callable $middleware = null): void { }
pipe("zzzz", function() : void {});',
],
'callableWithNonInvokable' => [
'<?php
function asd(): void {}
class B {}
/**
* @param callable|B $p
*/
function passes($p): void {}
passes("asd");',
],
'callableWithInvokable' => [
'<?php
function asd(): void {}
class A { public function __invoke(): void {} }
/**
* @param callable|A $p
*/
function fails($p): void {}
fails("asd");',
],
'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");',
],
'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-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 {}',
],
'callablesCanBeObjects' => [
'<?php
function foo(callable $c) : void {
if (is_object($c)) {
$c();
}
2019-03-23 19:27:54 +01:00
}',
],
'objectsCanBeCallable' => [
'<?php
function foo(object $c) : void {
if (is_callable($c)) {
$c();
}
2019-03-23 19:27:54 +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
}',
],
'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"]);',
],
'callableWithSpaces' => [
'<?php
/**
* @param callable(string, string) : int $p
*/
2019-03-23 19:27:54 +01:00
function f(callable $p): void {}',
],
'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
}',
],
'callableSelfArg' => [
'<?php
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 {
/**
* @param callable(self) $f
*/
function func2(callable $f): void {
$f($this);
}
}',
],
'callableParentArg' => [
'<?php
class C extends B {}
$b = new B();
$c = new C();
$b->func3(function(A $x): void {});
$c->func3(function(A $x): void {});
class A {}
class B extends A {
/**
* @param callable(parent) $f
*/
function func3(callable $f): void {
$f($this);
}
}',
],
'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 {});
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(); });',
],
'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
}',
],
'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
}',
],
'callableIsArrayAssertion' => [
'<?php
function foo(callable $c) : void {
if (is_array($c)) {
echo $c[1];
}
2019-03-23 19:27:54 +01:00
}',
],
'callableOrArrayIsArrayAssertion' => [
'<?php
/**
* @param callable|array $c
*/
function foo($c) : void {
if (is_array($c) && is_string($c[1])) {
echo $c[1];
}
2019-03-23 19:27:54 +01:00
}',
],
2019-08-12 21:04:43 +02:00
'dontInferMethodIdWhenFormatDoesntFit' => [
'<?php
/** @param string|callable $p */
function f($p): array {
return [];
}
f("#b::a");'
],
'removeCallableAssertionAfterReassignment' => [
'<?php
function foo(string $key) : void {
$setter = "a" . $key;
if (is_callable($setter)) {
return;
}
$setter = "b" . $key;
if (is_callable($setter)) {}
}'
],
'noExceptionOnSelfString' => [
'<?php
class Fish {
public static function example(array $vals): void {
usort($vals, ["self", "compare"]);
}
/**
* @param mixed $a
* @param mixed $b
*/
public static function compare($a, $b): int {
return -1;
}
}',
],
'noFatalErrorOnClassWithSlash' => [
'<?php
class Func {
public function __construct(string $name, callable $callable) {}
}
class Foo {
public static function bar(): string { return "asd"; }
}
new Func("f", ["\Foo", "bar"]);',
],
'staticReturningCallable' => [
'<?php
abstract class Id
{
/**
* @var string
*/
private $id;
final protected function __construct(string $id)
{
$this->id = $id;
}
/**
* @return static
*/
final public static function fromString(string $id): self
{
return new static($id);
}
}
final class CriterionId extends Id
{
}
final class CriterionIds
{
/**
* @psalm-var non-empty-list<CriterionId>
*/
private $ids;
/**
* @psalm-param non-empty-list<CriterionId> $ids
*/
private function __construct(array $ids)
{
$this->ids = $ids;
}
/**
* @psalm-param non-empty-list<string> $ids
*/
public static function fromStrings(array $ids): self
{
return new self(array_map([CriterionId::class, "fromString"], $ids));
}
}'
],
];
}
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
{
return [
'undefinedCallableClass' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function getFoo(): Foo
{
return new Foo([]);
}
2017-06-29 16:22:49 +02:00
/**
* @param mixed $argOne
* @param mixed $argTwo
* @return void
*/
public function bar($argOne, $argTwo)
{
$this->getFoo()($argOne, $argTwo);
}
}',
'error_message' => 'InvalidFunctionCall',
'error_levels' => ['UndefinedClass', 'MixedInferredReturnType'],
],
'undefinedCallableMethodFullString' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public static function bar(string $a): string {
return $a . "b";
}
}
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {}
foo("A::barr");',
'error_message' => 'UndefinedMethod',
],
'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"]);',
'error_message' => 'InvalidArgument',
],
'undefinedCallableMethodArrayWithoutClass' => [
'<?php
class A {
public static function bar(string $a): string {
return $a . "b";
}
}
function foo(callable $c): void {}
foo(["A", "::barr"]);',
'error_message' => 'InvalidArgument',
],
'undefinedCallableMethodClass' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public static function bar(string $a): string {
return $a . "b";
}
}
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {}
foo("B::bar");',
'error_message' => 'UndefinedClass',
],
'undefinedCallableFunction' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(callable $c): void {}
foo("trime");',
'error_message' => 'UndefinedFunction',
],
'stringFunctionCall' => [
'<?php
$bad_one = "hello";
$a = $bad_one(1);',
'error_message' => 'MixedAssignment',
],
'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',
],
'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',
],
'callableWithSpaceAfterColonBadVarArg' => [
'<?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',
],
'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;
public function __construct() {
$this->p = function (string $s, string $t): stdClass {
return new stdClass;
};
}
}',
'error_message' => 'InvalidPropertyAssignmentValue',
],
'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',
],
'noFatalErrorOnMissingClassWithSlash' => [
'<?php
class Func {
public function __construct(string $name, callable $callable) {}
}
new Func("f", ["\Foo", "bar"]);',
'error_message' => 'InvalidArgument'
],
'noFatalErrorOnMissingClassWithoutSlash' => [
'<?php
class Func {
public function __construct(string $name, callable $callable) {}
}
new Func("f", ["Foo", "bar"]);',
'error_message' => 'InvalidArgument'
],
'preventStringDocblockType' => [
'<?php
/**
* @param string $mapper
*/
function map2(callable $mapper): void {}
map2("foo");',
'error_message' => 'MismatchingDocblockParamType',
],
2019-12-14 17:55:26 +01:00
'moreSpecificCallable' => [
'<?php
/** @param callable(string):void $c */
function takesSpecificCallable(callable $c) : void {
$c("foo");
}
function takesCallable(callable $c) : void {
takesSpecificCallable($c);
}',
'error_message' => 'MixedArgumentTypeCoercion'
],
];
}
}