2016-10-30 03:17:46 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-10-30 03:17:46 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2018-03-20 18:24:16 +01:00
|
|
|
class CallableTest extends TestCase
|
2016-10-30 03:17:46 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-01-13 18:40:01 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-01-13 18:40:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'byRefUseVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
function run_function(\Closure $fnc) {
|
|
|
|
$fnc();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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
|
|
|
],
|
2019-09-01 06:56:46 +02:00
|
|
|
'inferredArgArrowFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-01 06:56:46 +02:00
|
|
|
$bar = ["foo", "bar"];
|
|
|
|
|
|
|
|
$bam = array_map(
|
|
|
|
fn(string $a) => $a . "blah",
|
|
|
|
$bar
|
|
|
|
);',
|
2020-08-23 16:32:07 +02:00
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2019-09-01 06:56:46 +02:00
|
|
|
],
|
2021-12-11 20:51:18 +01:00
|
|
|
'inferArgFromClassContext' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-11 20:51:18 +01:00
|
|
|
final class Calc
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Closure(int, int): int $_fn
|
|
|
|
*/
|
2021-12-11 20:51:18 +01:00
|
|
|
public function __invoke(Closure $_fn): int
|
2021-12-11 20:51:18 +01:00
|
|
|
{
|
|
|
|
return $_fn(42, 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$calc = new Calc();
|
|
|
|
|
|
|
|
$a = $calc(fn($a, $b) => $a + $b);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2021-12-11 20:51:18 +01:00
|
|
|
],
|
2021-12-12 11:17:15 +01:00
|
|
|
'inferArgFromClassContextWithNamedArguments' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-12 11:17:15 +01:00
|
|
|
final class Calc
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Closure(int, int): int ...$_fn
|
|
|
|
*/
|
|
|
|
public function __invoke(Closure ...$_fn): int
|
|
|
|
{
|
|
|
|
throw new RuntimeException("???");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$calc = new Calc();
|
|
|
|
|
|
|
|
$a = $calc(
|
|
|
|
foo: fn($_a, $_b) => $_a + $_b,
|
|
|
|
bar: fn($_a, $_b) => $_a + $_b,
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2021-12-12 11:17:15 +01:00
|
|
|
],
|
2021-12-22 13:03:39 +01:00
|
|
|
'inferArgFromClassContextInGenericContext' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-22 13:03:39 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
*/
|
|
|
|
final class ArrayList
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @template B
|
|
|
|
* @param Closure(A): B $ab
|
|
|
|
* @return ArrayList<B>
|
|
|
|
*/
|
|
|
|
public function map(Closure $ab): ArrayList
|
|
|
|
{
|
|
|
|
throw new RuntimeException("???");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param ArrayList<T> $list
|
|
|
|
* @return ArrayList<array{T}>
|
|
|
|
*/
|
|
|
|
function asTupled(ArrayList $list): ArrayList
|
|
|
|
{
|
|
|
|
return $list->map(function ($_a) {
|
|
|
|
return [$_a];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/** @var ArrayList<int> $a */
|
|
|
|
$a = new ArrayList();
|
2021-12-22 13:10:56 +01:00
|
|
|
$b = asTupled($a);',
|
2021-12-22 13:03:39 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'ArrayList<array{int}>',
|
|
|
|
],
|
|
|
|
],
|
2021-12-27 17:35:37 +01:00
|
|
|
'inferArgByPreviousFunctionArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-27 17:35:37 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param iterable<array-key, A> $_collection
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
function map(iterable $_collection, callable $_ab) { return []; }
|
|
|
|
|
|
|
|
/** @template T */
|
|
|
|
final class Foo
|
|
|
|
{
|
|
|
|
/** @return Foo<int> */
|
|
|
|
public function toInt() { throw new RuntimeException("???"); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var list<Foo<string>> */
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
$inferred = map($items, function ($i) {
|
|
|
|
return $i->toInt();
|
|
|
|
});',
|
|
|
|
'assertions' => [
|
|
|
|
'$inferred' => 'list<Foo<int>>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'inferTemplateForExplicitlyTypedArgByPreviousFunctionArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-27 17:35:37 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param iterable<array-key, A> $_collection
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
function map(iterable $_collection, callable $_ab) { return []; }
|
|
|
|
|
|
|
|
/** @template T */
|
|
|
|
final class Foo
|
|
|
|
{
|
|
|
|
/** @return Foo<int> */
|
|
|
|
public function toInt() { throw new RuntimeException("???"); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var list<Foo<string>> */
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
$inferred = map($items, function (Foo $i) {
|
|
|
|
return $i->toInt();
|
|
|
|
});',
|
|
|
|
'assertions' => [
|
|
|
|
'$inferred' => 'list<Foo<int>>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doNotInferTemplateForExplicitlyTypedWithPhpdocArgByPreviousFunctionArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-27 17:35:37 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param iterable<array-key, A> $_collection
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
function map(iterable $_collection, callable $_ab) { return []; }
|
|
|
|
|
|
|
|
/** @template T */
|
2021-12-27 17:43:06 +01:00
|
|
|
final class Foo { }
|
2021-12-27 17:35:37 +01:00
|
|
|
|
|
|
|
/** @var list<Foo<string>> */
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
$inferred = map($items,
|
|
|
|
/** @param Foo $i */
|
|
|
|
function ($i) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$inferred' => 'list<Foo>',
|
|
|
|
],
|
|
|
|
],
|
2022-01-18 10:48:53 +01:00
|
|
|
'inferTemplateOfHighOrderFunctionArgByPreviousArg' => [
|
2022-01-20 22:18:03 +01:00
|
|
|
'code' => '<?php
|
2022-01-18 10:48:53 +01:00
|
|
|
/**
|
|
|
|
* @return list<int>
|
|
|
|
*/
|
|
|
|
function getList() { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(T): T
|
|
|
|
*/
|
|
|
|
function id() { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param list<A> $_items
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
function map(array $_items, callable $_ab) { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
$result = map(getList(), id());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$result' => 'list<int>',
|
|
|
|
],
|
|
|
|
],
|
2022-01-18 11:12:01 +01:00
|
|
|
'inferTemplateOfHighOrderFunctionArgByPreviousArgInClassContext' => [
|
2022-01-20 22:18:03 +01:00
|
|
|
'code' => '<?php
|
2022-01-18 11:12:01 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
*/
|
|
|
|
final class ArrayList
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param callable(A): B $ab
|
|
|
|
* @return ArrayList<B>
|
|
|
|
*/
|
|
|
|
public function map(callable $ab) { throw new RuntimeException("???"); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayList<int>
|
|
|
|
*/
|
|
|
|
function getList() { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(T): T
|
|
|
|
*/
|
|
|
|
function id() { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
$result = getList()->map(id());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$result' => 'ArrayList<int>',
|
|
|
|
],
|
|
|
|
],
|
2022-01-18 16:54:47 +01:00
|
|
|
'inferTemplateOfHighOrderFunctionFromMethodArgByPreviousArg' => [
|
2022-01-20 22:18:03 +01:00
|
|
|
'code' => '<?php
|
2022-01-18 16:54:47 +01:00
|
|
|
final class Ops
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(list<T>): T
|
|
|
|
*/
|
|
|
|
public function flatten() { throw new RuntimeException("???"); }
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return list<list<int>>
|
|
|
|
*/
|
|
|
|
function getList() { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(list<T>): T
|
|
|
|
*/
|
|
|
|
function flatten() { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param list<A> $_a
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
function map(array $_a, callable $_ab) { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
$ops = new Ops;
|
|
|
|
$result = map(getList(), $ops->flatten());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$result' => 'list<int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'inferTemplateOfHighOrderFunctionFromStaticMethodArgByPreviousArg' => [
|
2022-01-20 22:18:03 +01:00
|
|
|
'code' => '<?php
|
2022-01-18 16:54:47 +01:00
|
|
|
final class StaticOps
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(list<T>): T
|
|
|
|
*/
|
|
|
|
public static function flatten() { throw new RuntimeException("???"); }
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return list<list<int>>
|
|
|
|
*/
|
|
|
|
function getList() { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(list<T>): T
|
|
|
|
*/
|
|
|
|
function flatten() { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param list<A> $_a
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
function map(array $_a, callable $_ab) { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
$result = map(getList(), StaticOps::flatten());
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$result' => 'list<int>',
|
|
|
|
],
|
|
|
|
],
|
2022-01-20 22:18:03 +01:00
|
|
|
'PipeTest' => [
|
|
|
|
'code' => '<?php
|
2022-01-18 22:37:05 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*/
|
|
|
|
final class MapOperator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Closure(A): B $ab
|
|
|
|
*/
|
|
|
|
public function __construct(private Closure $ab) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param list<A> $a
|
|
|
|
* @return list<B>
|
|
|
|
*/
|
|
|
|
public function __invoke($a): array
|
|
|
|
{
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
foreach ($a as $item) {
|
|
|
|
$b[] = ($this->ab)($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param Closure(A): B $ab
|
|
|
|
* @return MapOperator<A, B>
|
|
|
|
*/
|
|
|
|
function map(Closure $ab): MapOperator
|
|
|
|
{
|
|
|
|
return new MapOperator($ab);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param A $_a
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return B
|
|
|
|
*/
|
|
|
|
function pipe(array $_a, callable $_ab): array
|
|
|
|
{
|
|
|
|
throw new RuntimeException("???");
|
|
|
|
}
|
|
|
|
$result1 = pipe(
|
|
|
|
["1", "2", "3"],
|
|
|
|
map(fn ($i) => (int) $i)
|
|
|
|
);
|
|
|
|
$result2 = pipe(
|
|
|
|
["1", "2", "3"],
|
|
|
|
new MapOperator(fn ($i) => (int) $i)
|
|
|
|
);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$result1' => 'list<int>',
|
|
|
|
'$result2' => 'list<int>',
|
|
|
|
],
|
|
|
|
'error_levels' => [],
|
|
|
|
'8.0',
|
|
|
|
],
|
2022-01-18 10:48:53 +01:00
|
|
|
'inferPipelineWithPartiallyAppliedFunctions' => [
|
2022-01-20 22:18:03 +01:00
|
|
|
'code' => '<?php
|
2022-01-18 01:02:21 +01:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @param callable(T, int): bool $_predicate
|
|
|
|
* @return Closure(list<T>): list<T>
|
|
|
|
*/
|
|
|
|
function filter(callable $_predicate): Closure { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param callable(A): B $_ab
|
|
|
|
* @return Closure(list<A>): list<B>
|
|
|
|
*/
|
|
|
|
function map(callable $_ab): Closure { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return (Closure(list<T>): (non-empty-list<T> | null))
|
|
|
|
*/
|
|
|
|
function asNonEmptyList(): Closure { throw new RuntimeException("???"); }
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @return Closure(T): T
|
|
|
|
*/
|
|
|
|
function id(): Closure { throw new RuntimeException("???"); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
* @template B
|
|
|
|
* @template C
|
|
|
|
* @template D
|
|
|
|
* @template E
|
|
|
|
* @template F
|
|
|
|
*
|
|
|
|
* @param A $arg
|
|
|
|
* @param callable(A): B $ab
|
|
|
|
* @param callable(B): C $bc
|
|
|
|
* @param callable(C): D $cd
|
|
|
|
* @param callable(D): E $de
|
|
|
|
* @param callable(E): F $ef
|
|
|
|
* @return F
|
|
|
|
*/
|
|
|
|
function pipe4(mixed $arg, callable $ab, callable $bc, callable $cd, callable $de, callable $ef): mixed
|
|
|
|
{
|
|
|
|
return $ef($de($cd($bc($ab($arg)))));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TFoo of string
|
|
|
|
* @template TBar of bool
|
|
|
|
*/
|
|
|
|
final class Item
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param TFoo $foo
|
|
|
|
* @param TBar $bar
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
public string $foo,
|
|
|
|
public bool $bar,
|
|
|
|
) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return list<Item>
|
|
|
|
*/
|
|
|
|
function getList(): array { return []; }
|
|
|
|
|
|
|
|
$result = pipe4(
|
|
|
|
getList(),
|
|
|
|
filter(fn($i) => $i->bar),
|
|
|
|
filter(fn(Item $i) => $i->foo !== "bar"),
|
|
|
|
map(fn($i) => new Item("test: " . $i->foo, $i->bar)),
|
|
|
|
asNonEmptyList(),
|
|
|
|
id(),
|
|
|
|
);',
|
|
|
|
'assertions' => [
|
|
|
|
'$result' => 'non-empty-list<Item<string, bool>>|null',
|
|
|
|
],
|
|
|
|
'error_levels' => [],
|
|
|
|
'8.0',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'varReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-01 06:56:46 +02: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
|
|
|
],
|
2019-09-01 06:56:46 +02:00
|
|
|
'varReturnTypeArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-01 06:56:46 +02:00
|
|
|
$add_one = fn(int $a) : int => $a + 1;
|
|
|
|
|
|
|
|
$a = $add_one(1);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2019-09-01 06:56:46 +02:00
|
|
|
],
|
2018-03-27 07:05:11 +02:00
|
|
|
'varCallableParamReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-27 06:12:41 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
],
|
2019-09-01 06:56:46 +02:00
|
|
|
'callableToClosureArrow' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-01 06:56:46 +02:00
|
|
|
/**
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return fn(string $a): string => $a . "blah";
|
|
|
|
}',
|
2020-08-23 16:32:07 +02:00
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.4',
|
2019-09-01 06:56:46 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'callable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
],
|
2022-01-06 21:22:18 +01:00
|
|
|
'invokeMethodExists' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-06 21:22:18 +01:00
|
|
|
function call(object $obj): void {
|
|
|
|
if (!method_exists($obj, "__invoke")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$obj();
|
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'correctParamType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-12 00:30:58 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-22 06:50:41 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-24 18:54:35 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-15 01:30:11 +02:00
|
|
|
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' => [
|
2022-01-16 16:07:56 +01:00
|
|
|
'$a' => 'array{string, string}<string>',
|
|
|
|
'$b' => 'array{string, string}<string>',
|
|
|
|
'$c' => 'array{string, string}<string>',
|
|
|
|
'$d' => 'array{string, string}<string>',
|
|
|
|
'$e' => 'array{string, string}<string>',
|
|
|
|
'$f' => 'array{string, string}<string>',
|
2017-08-15 01:30:11 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'arrayCallableMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-15 01:30:11 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-14 21:46:01 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-17 02:06:00 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-20 13:58:05 +01:00
|
|
|
/**
|
|
|
|
* @param string|callable $middlewareOrPath
|
|
|
|
*/
|
|
|
|
function pipe($middlewareOrPath, ?callable $middleware = null): void { }
|
|
|
|
|
|
|
|
pipe("zzzz", function() : void {});',
|
|
|
|
],
|
2018-03-20 14:30:37 +01:00
|
|
|
'callableWithNonInvokable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-20 14:30:37 +01:00
|
|
|
function asd(): void {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable|B $p
|
|
|
|
*/
|
|
|
|
function passes($p): void {}
|
|
|
|
|
|
|
|
passes("asd");',
|
|
|
|
],
|
2018-03-20 18:24:16 +01:00
|
|
|
'callableWithInvokable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-20 14:30:37 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-20 18:24:16 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-20 18:24:16 +01:00
|
|
|
function foo(): void {}
|
|
|
|
|
|
|
|
function callMeMaybe(string $method): void {
|
|
|
|
if (is_callable($method)) {
|
|
|
|
$method();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callMeMaybe("foo");',
|
|
|
|
],
|
2018-04-12 02:27:25 +02:00
|
|
|
'allowVoidCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-12 02:27:25 +02:00
|
|
|
/**
|
|
|
|
* @param callable():void $p
|
|
|
|
*/
|
|
|
|
function doSomething($p): void {}
|
|
|
|
doSomething(function(): bool { return false; });',
|
|
|
|
],
|
2018-05-09 01:49:25 +02:00
|
|
|
'callableProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 01:49:25 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 01:49:25 +02:00
|
|
|
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-06-09 05:54:07 +02:00
|
|
|
'nullableReturnTypeShorthand' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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 {}',
|
|
|
|
],
|
2019-01-04 14:04:19 +01:00
|
|
|
'callablesCanBeObjects' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-04 14:04:19 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-04 14:04:19 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-04 14:04:19 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 01:49:58 +01:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-01 13:50:48 +01:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 01:49:58 +01:00
|
|
|
/** @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-02-17 19:14:59 +01:00
|
|
|
'callableSelfArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-12 06:44:10 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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
|
|
|
],
|
2020-03-25 14:18:49 +01:00
|
|
|
'callableStaticReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-08 00:02:31 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @param callable():static $f
|
|
|
|
*/
|
|
|
|
function func1(callable $f): void {}
|
2020-03-25 14:18:49 +01:00
|
|
|
}
|
2019-03-08 00:02:31 +01:00
|
|
|
|
2020-03-25 14:18:49 +01:00
|
|
|
final class C extends B {}
|
|
|
|
|
|
|
|
$c = new C();
|
|
|
|
|
|
|
|
$c->func1(function(): C { return new C(); });',
|
|
|
|
],
|
|
|
|
'callableSelfReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-25 14:18:49 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
2019-03-08 00:02:31 +01:00
|
|
|
/**
|
|
|
|
* @param callable():self $f
|
|
|
|
*/
|
|
|
|
function func2(callable $f): void {}
|
2020-03-25 14:18:49 +01:00
|
|
|
}
|
2019-03-08 00:02:31 +01:00
|
|
|
|
2020-03-25 14:18:49 +01:00
|
|
|
final class C extends B {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$c = new C();
|
|
|
|
|
|
|
|
$b->func2(function() { return new B(); });
|
|
|
|
$c->func2(function() { return new C(); });',
|
|
|
|
],
|
|
|
|
'callableParentReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-25 14:18:49 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {
|
2019-03-08 00:02:31 +01:00
|
|
|
/**
|
|
|
|
* @param callable():parent $f
|
|
|
|
*/
|
|
|
|
function func3(callable $f): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
|
2020-03-25 14:18:49 +01:00
|
|
|
$b->func3(function() { return new A(); });',
|
2019-02-17 19:14:59 +01:00
|
|
|
],
|
2019-02-19 07:25:36 +01:00
|
|
|
'selfArrayMapCallableWrongClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-19 07:25:36 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-22 06:50:41 +01:00
|
|
|
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-07 00:24:52 +01:00
|
|
|
'callableIsArrayAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-07 00:24:52 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-11 16:39:33 +01:00
|
|
|
/**
|
|
|
|
* @param callable|array $c
|
|
|
|
*/
|
|
|
|
function foo($c) : void {
|
2020-04-09 15:27:14 +02:00
|
|
|
if (is_array($c) && isset($c[1]) && is_string($c[1])) {
|
2019-06-21 01:46:42 +02:00
|
|
|
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-08-12 21:04:43 +02:00
|
|
|
'dontInferMethodIdWhenFormatDoesntFit' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-12 21:04:43 +02:00
|
|
|
/** @param string|callable $p */
|
|
|
|
function f($p): array {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
f("#b::a");'
|
|
|
|
],
|
2019-08-16 16:32:03 +02:00
|
|
|
'removeCallableAssertionAfterReassignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-16 16:32:03 +02:00
|
|
|
function foo(string $key) : void {
|
|
|
|
$setter = "a" . $key;
|
|
|
|
if (is_callable($setter)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$setter = "b" . $key;
|
|
|
|
if (is_callable($setter)) {}
|
|
|
|
}'
|
|
|
|
],
|
2019-11-11 20:59:05 +01:00
|
|
|
'noExceptionOnSelfString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-11 20:59:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-11-21 14:56:47 +01:00
|
|
|
'noFatalErrorOnClassWithSlash' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-21 14:56:47 +01:00
|
|
|
class Func {
|
|
|
|
public function __construct(string $name, callable $callable) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
public static function bar(): string { return "asd"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
new Func("f", ["\Foo", "bar"]);',
|
|
|
|
],
|
2019-12-16 01:55:20 +01:00
|
|
|
'staticReturningCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-16 01:55:20 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}'
|
2020-01-02 14:31:19 +01:00
|
|
|
],
|
|
|
|
'offsetOnCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-02 14:31:19 +01:00
|
|
|
function c(callable $c) : void {
|
|
|
|
if (is_array($c)) {
|
|
|
|
new ReflectionClass($c[0]);
|
|
|
|
}
|
|
|
|
}'
|
2019-12-16 01:55:20 +01:00
|
|
|
],
|
2020-01-17 16:02:58 +01:00
|
|
|
'destructureCallableArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-17 16:02:58 +01:00
|
|
|
function getCallable(): callable {
|
|
|
|
return [DateTimeImmutable::class, "createFromFormat"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$callable = getCallable();
|
|
|
|
|
|
|
|
if (!is_array($callable)) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
[$classOrObject, $method] = $callable;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-01-17 16:02:58 +01:00
|
|
|
'$classOrObject' => 'class-string|object',
|
|
|
|
'$method' => 'string'
|
|
|
|
]
|
|
|
|
],
|
2020-01-30 04:33:28 +01:00
|
|
|
'callableInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-30 04:33:28 +01:00
|
|
|
interface CallableInterface{
|
|
|
|
public function __invoke(): bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInvokableInterface(CallableInterface $c): void{
|
|
|
|
takesCallable($c);
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesCallable(callable $c): void {
|
|
|
|
$c();
|
|
|
|
}'
|
|
|
|
],
|
2020-02-24 22:19:33 +01:00
|
|
|
'notCallableArrayNoUndefinedClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-24 22:19:33 +01:00
|
|
|
/**
|
|
|
|
* @psalm-param array|callable $_fields
|
|
|
|
*/
|
|
|
|
function f($_fields): void {}
|
|
|
|
|
|
|
|
f(["instance_date" => "ASC", "start_time" => "ASC"]);'
|
|
|
|
],
|
2020-03-29 05:41:05 +02:00
|
|
|
'callOnInvokableOrCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-29 05:41:05 +02:00
|
|
|
interface Callback {
|
|
|
|
public function __invoke(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Callback|callable */
|
|
|
|
$test = function (): void {};
|
|
|
|
|
|
|
|
$test();'
|
|
|
|
],
|
2020-05-22 20:26:37 +02:00
|
|
|
'resolveTraitClosureReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-22 20:26:37 +02:00
|
|
|
class B {
|
|
|
|
/**
|
|
|
|
* @psalm-param callable(mixed...):static $i
|
|
|
|
*/
|
|
|
|
function takesACall(callable $i) : void {}
|
|
|
|
|
|
|
|
public function call() : void {
|
|
|
|
$this->takesACall(function() {return $this;});
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-08-06 01:39:27 +02:00
|
|
|
'returnClosureReturningStatic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2020-05-22 20:26:37 +02:00
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @return Closure():static
|
|
|
|
*/
|
|
|
|
public static function foo() {
|
|
|
|
return function() {
|
|
|
|
return new static();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-06-12 20:26:31 +02:00
|
|
|
'returnsVoidAcceptableForNullable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-12 20:26:31 +02:00
|
|
|
/** @param callable():?bool $c */
|
|
|
|
function takesCallable(callable $c) : void {}
|
|
|
|
|
|
|
|
takesCallable(function() { return; });',
|
|
|
|
],
|
2020-06-14 17:58:50 +02:00
|
|
|
'byRefUsesAlwaysMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-14 17:58:50 +02:00
|
|
|
$callback = function() use (&$isCalled) : void {
|
|
|
|
$isCalled = true;
|
|
|
|
};
|
|
|
|
$isCalled = false;
|
|
|
|
$callback();
|
|
|
|
|
|
|
|
if ($isCalled === true) {}'
|
|
|
|
],
|
2020-07-20 10:50:07 +02:00
|
|
|
'notCallableListNoUndefinedClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-20 10:50:07 +02:00
|
|
|
/**
|
|
|
|
* @param array|callable $arg
|
|
|
|
*/
|
|
|
|
function foo($arg): void {}
|
|
|
|
|
|
|
|
foo(["a", "b"]);'
|
|
|
|
],
|
2020-09-19 20:18:52 +02:00
|
|
|
'abstractInvokeInTrait' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-19 20:18:52 +02:00
|
|
|
function testFunc(callable $func) : void {}
|
|
|
|
|
|
|
|
trait TestTrait {
|
|
|
|
abstract public function __invoke() : void;
|
|
|
|
|
|
|
|
public function apply() : void {
|
|
|
|
testFunc($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class TestClass {
|
|
|
|
use TestTrait;
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
2017-01-13 18:40:01 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-01-13 18:40:01 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'undefinedCallableClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['UndefinedClass', 'MixedInferredReturnType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-10-17 19:22:57 +02:00
|
|
|
'undefinedCallableMethodFullString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-12 00:30:58 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-10-17 19:22:57 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-10-17 19:22:57 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-10-17 19:22:57 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-12 00:48:58 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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
|
|
|
'stringFunctionCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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
|
|
|
],
|
2018-03-27 06:12:41 +02:00
|
|
|
'wrongCallableReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-27 06:12:41 +02:00
|
|
|
$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',
|
|
|
|
],
|
2019-01-20 01:49:58 +01:00
|
|
|
'checkCallableTypeString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 01:49:58 +01:00
|
|
|
/**
|
|
|
|
* @param callable(int,int):int $_p
|
|
|
|
*/
|
|
|
|
function f(callable $_p): void {}
|
|
|
|
|
|
|
|
f("strcmp");',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'checkCallableTypeArrayInstanceFirstArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 01:49:58 +01:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 01:49:58 +01:00
|
|
|
/**
|
|
|
|
* @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-02-01 14:58:52 +01:00
|
|
|
'callableWithSpaceAfterColonBadVarArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-01 13:50:48 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-01 14:58:52 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-01 14:58:52 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-15 18:02:44 +01:00
|
|
|
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-11-21 14:56:47 +01:00
|
|
|
'noFatalErrorOnMissingClassWithSlash' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-21 14:56:47 +01:00
|
|
|
class Func {
|
|
|
|
public function __construct(string $name, callable $callable) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Func("f", ["\Foo", "bar"]);',
|
|
|
|
'error_message' => 'InvalidArgument'
|
|
|
|
],
|
|
|
|
'noFatalErrorOnMissingClassWithoutSlash' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-21 14:56:47 +01:00
|
|
|
class Func {
|
|
|
|
public function __construct(string $name, callable $callable) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Func("f", ["Foo", "bar"]);',
|
|
|
|
'error_message' => 'InvalidArgument'
|
|
|
|
],
|
2019-11-27 15:18:47 +01:00
|
|
|
'preventStringDocblockType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-27 15:18:47 +01:00
|
|
|
/**
|
|
|
|
* @param string $mapper
|
|
|
|
*/
|
|
|
|
function map2(callable $mapper): void {}
|
|
|
|
|
|
|
|
map2("foo");',
|
|
|
|
'error_message' => 'MismatchingDocblockParamType',
|
|
|
|
],
|
2019-12-14 17:55:26 +01:00
|
|
|
'moreSpecificCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-14 17:55:26 +01:00
|
|
|
/** @param callable(string):void $c */
|
|
|
|
function takesSpecificCallable(callable $c) : void {
|
|
|
|
$c("foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesCallable(callable $c) : void {
|
|
|
|
takesSpecificCallable($c);
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedArgumentTypeCoercion'
|
|
|
|
],
|
2020-01-24 16:32:28 +01:00
|
|
|
'undefinedVarInBareCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-24 16:32:28 +01:00
|
|
|
$fn = function(int $a): void{};
|
|
|
|
function a(callable $fn): void{
|
|
|
|
$fn(++$a);
|
|
|
|
}
|
|
|
|
a($fn);',
|
|
|
|
'error_message' => 'UndefinedVariable',
|
|
|
|
],
|
2020-04-20 03:21:18 +02:00
|
|
|
'dontQualifyStringCallables' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-20 03:21:18 +02:00
|
|
|
namespace NS;
|
|
|
|
|
|
|
|
function ff() : void {}
|
|
|
|
|
|
|
|
function run(callable $f) : void {
|
|
|
|
$f();
|
|
|
|
}
|
|
|
|
|
|
|
|
run("ff");',
|
|
|
|
'error_message' => 'UndefinedFunction',
|
|
|
|
],
|
2020-05-19 21:48:20 +02:00
|
|
|
'badCustomFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-19 21:48:20 +02:00
|
|
|
/**
|
|
|
|
* @param callable(int):bool $func
|
|
|
|
*/
|
|
|
|
function takesFunction(callable $func) : void {}
|
|
|
|
|
|
|
|
function myFunction( string $foo ) : bool {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
takesFunction("myFunction");',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2020-06-04 21:40:44 +02:00
|
|
|
'emptyCallable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-04 21:40:44 +02:00
|
|
|
$a = "";
|
|
|
|
$a();',
|
|
|
|
'error_message' => 'InvalidFunctionCall',
|
|
|
|
],
|
2020-08-26 22:51:22 +02:00
|
|
|
'ImpureFunctionCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* @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',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
2020-08-30 04:02:58 +02:00
|
|
|
],
|
|
|
|
'constructCallableFromClassStringArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 04:02:58 +02:00
|
|
|
interface Foo {
|
|
|
|
public function bar() : int;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable():string $c
|
|
|
|
*/
|
|
|
|
function takesCallableReturningString(callable $c) : void {
|
|
|
|
$c();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<Foo> $c
|
|
|
|
*/
|
|
|
|
function foo(string $c) : void {
|
|
|
|
takesCallableReturningString([$c, "bar"]);
|
|
|
|
}',
|
2021-12-19 12:17:25 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2020-08-30 04:02:58 +02:00
|
|
|
],
|
2021-11-23 20:04:52 +01:00
|
|
|
'inexistantCallableinCallableString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-23 20:04:52 +01:00
|
|
|
/**
|
|
|
|
* @param callable-string $c
|
|
|
|
*/
|
|
|
|
function c(string $c): void {
|
|
|
|
$c();
|
|
|
|
}
|
|
|
|
|
|
|
|
c("hii");',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2021-12-22 14:34:25 +01:00
|
|
|
'mismatchParamTypeFromDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-22 14:34:25 +01:00
|
|
|
/**
|
|
|
|
* @template A
|
|
|
|
*/
|
|
|
|
final class ArrayList
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @template B
|
|
|
|
* @param Closure(A): B $effect
|
|
|
|
* @return ArrayList<B>
|
|
|
|
*/
|
|
|
|
public function map(Closure $effect): ArrayList
|
|
|
|
{
|
|
|
|
throw new RuntimeException("???");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template B
|
|
|
|
*
|
|
|
|
* @param ArrayList<T> $list
|
|
|
|
* @return ArrayList<array{T}>
|
|
|
|
*/
|
|
|
|
function genericContext(ArrayList $list): ArrayList
|
|
|
|
{
|
|
|
|
return $list->map(
|
|
|
|
/** @param B $_a */
|
|
|
|
function ($_a) {
|
|
|
|
return [$_a];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
]
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-13 18:40:01 +01:00
|
|
|
}
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|