,ignored_issues?:list}> */ public function providerValidCodeParse(): iterable { return [ 'byRefUseVar' => [ 'code' => ' [ 'code' => ' [ 'code' => ' $a . "blah", $bar );', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '7.4', ], 'inferArgFromClassContext' => [ 'code' => ' $a + $b);', 'assertions' => [ '$a' => 'int', ], 'ignored_issues' => [], 'php_version' => '7.4', ], 'inferArgFromClassContextWithNamedArguments' => [ 'code' => ' $_a + $_b, bar: fn($_a, $_b) => $_a + $_b, );', 'assertions' => [ '$a' => 'int', ], 'ignored_issues' => [], 'php_version' => '7.4', ], 'inferArgFromClassContextInGenericContext' => [ 'code' => ' */ public function map(Closure $ab): ArrayList { throw new RuntimeException("???"); } } /** * @template T * @param ArrayList $list * @return ArrayList */ function asTupled(ArrayList $list): ArrayList { return $list->map(function ($_a) { return [$_a]; }); } /** @var ArrayList $a */ $a = new ArrayList(); $b = asTupled($a);', 'assertions' => [ '$b' => 'ArrayList', ], ], 'inferArgByPreviousFunctionArg' => [ 'code' => ' $_collection * @param callable(A): B $_ab * @return list */ function map(iterable $_collection, callable $_ab) { return []; } /** @template T */ final class Foo { /** @return Foo */ public function toInt() { throw new RuntimeException("???"); } } /** @var list> */ $items = []; $inferred = map($items, function ($i) { return $i->toInt(); });', 'assertions' => [ '$inferred' => 'list>', ], ], 'inferTemplateForExplicitlyTypedArgByPreviousFunctionArg' => [ 'code' => ' $_collection * @param callable(A): B $_ab * @return list */ function map(iterable $_collection, callable $_ab) { return []; } /** @template T */ final class Foo { /** @return Foo */ public function toInt() { throw new RuntimeException("???"); } } /** @var list> */ $items = []; $inferred = map($items, function (Foo $i) { return $i->toInt(); });', 'assertions' => [ '$inferred' => 'list>', ], ], 'doNotInferTemplateForExplicitlyTypedWithPhpdocArgByPreviousFunctionArg' => [ 'code' => ' $_collection * @param callable(A): B $_ab * @return list */ function map(iterable $_collection, callable $_ab) { return []; } /** @template T */ final class Foo { } /** @var list> */ $items = []; $inferred = map($items, /** @param Foo $i */ function ($i) { return $i; } );', 'assertions' => [ '$inferred' => 'list', ], ], 'inferTemplateOfHighOrderFunctionArgByPreviousArg' => [ 'code' => ' */ function getList() { throw new RuntimeException("???"); } /** * @template T * @return Closure(T): T */ function id() { throw new RuntimeException("???"); } /** * @template A * @template B * * @param list $_items * @param callable(A): B $_ab * @return list */ function map(array $_items, callable $_ab) { throw new RuntimeException("???"); } $result = map(getList(), id()); ', 'assertions' => [ '$result' => 'list', ], ], 'inferTemplateOfHighOrderFunctionArgByPreviousArgInClassContext' => [ 'code' => ' */ public function map(callable $ab) { throw new RuntimeException("???"); } } /** * @return ArrayList */ function getList() { throw new RuntimeException("???"); } /** * @template T * @return Closure(T): T */ function id() { throw new RuntimeException("???"); } $result = getList()->map(id()); ', 'assertions' => [ '$result' => 'ArrayList', ], ], 'inferTemplateOfHighOrderFunctionFromMethodArgByPreviousArg' => [ 'code' => '): T */ public function flatten() { throw new RuntimeException("???"); } } /** * @return list> */ function getList() { throw new RuntimeException("???"); } /** * @template T * @return Closure(list): T */ function flatten() { throw new RuntimeException("???"); } /** * @template A * @template B * * @param list $_a * @param callable(A): B $_ab * @return list */ function map(array $_a, callable $_ab) { throw new RuntimeException("???"); } $ops = new Ops; $result = map(getList(), $ops->flatten()); ', 'assertions' => [ '$result' => 'list', ], ], 'inferTemplateOfHighOrderFunctionFromStaticMethodArgByPreviousArg' => [ 'code' => '): T */ public static function flatten() { throw new RuntimeException("???"); } } /** * @return list> */ function getList() { throw new RuntimeException("???"); } /** * @template T * @return Closure(list): T */ function flatten() { throw new RuntimeException("???"); } /** * @template A * @template B * * @param list $_a * @param callable(A): B $_ab * @return list */ function map(array $_a, callable $_ab) { throw new RuntimeException("???"); } $result = map(getList(), StaticOps::flatten()); ', 'assertions' => [ '$result' => 'list', ], ], 'PipeTest' => [ 'code' => ' $a * @return list */ 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 */ 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', '$result2' => 'list', ], 'error_levels' => [], '8.0', ], 'inferPipelineWithPartiallyAppliedFunctions' => [ 'code' => '): list */ function filter(callable $_predicate): Closure { throw new RuntimeException("???"); } /** * @template A * @template B * * @param callable(A): B $_ab * @return Closure(list): list */ function map(callable $_ab): Closure { throw new RuntimeException("???"); } /** * @template T * @return (Closure(list): (non-empty-list | 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 */ 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>|null', ], 'error_levels' => [], '8.0', ], 'varReturnType' => [ 'code' => ' [ '$a' => 'int', ], ], 'varReturnTypeArray' => [ 'code' => ' $a + 1; $a = $add_one(1);', 'assertions' => [ '$a' => 'int', ], 'ignored_issues' => [], 'php_version' => '7.4', ], 'varCallableParamReturnType' => [ 'code' => ' [ 'code' => ' [ 'code' => ' $a . "blah"; }', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '7.4', ], 'callable' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ '$a' => 'array{string, string}', '$b' => 'array{string, string}', '$c' => 'array{string, string}', '$d' => 'array{string, string}', '$e' => 'array{string, string}', '$f' => 'array{string, string}', ], ], 'arrayCallableMethod' => [ 'code' => ' [ 'code' => ' [ 'code' => ' $b ? 1 : 0; } $arr = [5, 4, 3, 1, 2]; usort($arr, "fooBar"); } }', ], 'closureSelf' => [ 'code' => 'subitems = array_map( function(self $i): self { return $i; }, $in ); } } new A([new A, new A]);', ], 'possiblyUndefinedFunction' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => 'callMeMaybe("foo");', ], 'isCallableString' => [ 'code' => ' [ 'code' => ' [ 'code' => 'callable = $callable; } public function callTheCallableDirectly(): bool { return ($this->callable)(); } public function callTheCallableIndirectly(): bool { $r = $this->callable; return $r(); } }', ], 'invokableProperties' => [ 'code' => 'invokable = $invokable; } public function callTheInvokableDirectly(): bool { return ($this->invokable)(); } public function callTheInvokableIndirectly(): bool { $r = $this->invokable; return $r(); } }', ], 'nullableReturnTypeShorthand' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' $b; } } f("strcmp"); f([new C, "m"]); f([C::class, "m"]);', ], 'callableWithSpaces' => [ 'code' => ' [ 'code' => ' [ 'code' => 'func2(function(B $x): void {}); $c->func2(function(B $x): void {}); class A {} class B extends A { /** * @param callable(self) $f */ function func2(callable $f): void { $f($this); } }', ], 'callableParentArg' => [ 'code' => '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' => [ 'code' => '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); } }', ], 'callableStaticReturn' => [ 'code' => 'func1(function(): C { return new C(); });', ], 'callableSelfReturn' => [ 'code' => 'func2(function() { return new B(); }); $c->func2(function() { return new C(); });', ], 'callableParentReturn' => [ 'code' => 'func3(function() { return new A(); });', ], 'selfArrayMapCallableWrongClass' => [ 'code' => ' */ public function bar() { return array_map([Foo::class, "foo"], [1,2,3]); } /** @return array */ public function bat() { return array_map([Foo::class, "baz"], [1]); } }', ], 'dynamicCallableArray' => [ 'code' => 'value = $value; } }', ], 'callableIsArrayAssertion' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => '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 */ private $ids; /** * @psalm-param non-empty-list $ids */ private function __construct(array $ids) { $this->ids = $ids; } /** * @psalm-param non-empty-list $ids */ public static function fromStrings(array $ids): self { return new self(array_map([CriterionId::class, "fromString"], $ids)); } }' ], 'offsetOnCallable' => [ 'code' => ' [ 'code' => ' [ '$classOrObject' => 'class-string|object', '$method' => 'string' ] ], 'callableInterface' => [ 'code' => ' [ 'code' => ' "ASC", "start_time" => "ASC"]);' ], 'callOnInvokableOrCallable' => [ 'code' => ' [ 'code' => 'takesACall(function() {return $this;}); } }' ], 'returnClosureReturningStatic' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ',php_version?:string}> */ public function providerInvalidCodeParse(): iterable { return [ 'undefinedCallableClass' => [ 'code' => 'getFoo()($argOne, $argTwo); } }', 'error_message' => 'InvalidFunctionCall', 'ignored_issues' => ['UndefinedClass', 'MixedInferredReturnType'], ], 'undefinedCallableMethodFullString' => [ 'code' => ' 'UndefinedMethod', ], 'undefinedCallableMethodClassConcat' => [ 'code' => ' 'UndefinedMethod', ], 'undefinedCallableMethodArray' => [ 'code' => ' 'InvalidArgument', ], 'undefinedCallableMethodArrayWithoutClass' => [ 'code' => ' 'InvalidArgument', ], 'undefinedCallableMethodClass' => [ 'code' => ' 'UndefinedClass', ], 'undefinedCallableFunction' => [ 'code' => ' 'UndefinedFunction', ], 'stringFunctionCall' => [ 'code' => ' 'MixedAssignment', ], 'wrongCallableReturnType' => [ 'code' => ' 'InvalidReturnStatement', ], 'checkCallableTypeString' => [ 'code' => ' 'InvalidScalarArgument', ], 'checkCallableTypeArrayInstanceFirstArg' => [ 'code' => ' $b; } } f([new C, "m"]);', 'error_message' => 'InvalidScalarArgument', ], 'checkCallableTypeArrayClassStringFirstArg' => [ 'code' => ' $b; } } f([C::class, "m"]);', 'error_message' => 'InvalidScalarArgument', ], 'callableWithSpaceAfterColonBadVarArg' => [ 'code' => 'p = function (string $s, string $t): stdClass { return new stdClass; }; } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'callableWithSpaceBeforeColonBadVarArg' => [ 'code' => 'p = function (string $s, string $t): stdClass { return new stdClass; }; } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'callableWithSpacesEitherSideOfColonBadVarArg' => [ 'code' => 'p = function (string $s, string $t): stdClass { return new stdClass; }; } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'badArrayMapArrayCallable' => [ 'code' => ' 'InvalidArgument', ], 'noFatalErrorOnMissingClassWithSlash' => [ 'code' => ' 'InvalidArgument' ], 'noFatalErrorOnMissingClassWithoutSlash' => [ 'code' => ' 'InvalidArgument' ], 'preventStringDocblockType' => [ 'code' => ' 'MismatchingDocblockParamType', ], 'moreSpecificCallable' => [ 'code' => ' 'MixedArgumentTypeCoercion' ], 'undefinedVarInBareCallable' => [ 'code' => ' 'UndefinedVariable', ], 'dontQualifyStringCallables' => [ 'code' => ' 'UndefinedFunction', ], 'badCustomFunction' => [ 'code' => ' 'InvalidScalarArgument', ], 'emptyCallable' => [ 'code' => ' 'InvalidFunctionCall', ], 'ImpureFunctionCall' => [ 'code' => ' $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', 'ignored_issues' => [], ], 'constructCallableFromClassStringArray' => [ 'code' => ' $c */ function foo(string $c) : void { takesCallableReturningString([$c, "bar"]); }', 'error_message' => 'InvalidArgument', ], 'inexistantCallableinCallableString' => [ 'code' => ' 'InvalidArgument', ], 'mismatchParamTypeFromDocblock' => [ 'code' => ' */ public function map(Closure $effect): ArrayList { throw new RuntimeException("???"); } } /** * @template T * @template B * * @param ArrayList $list * @return ArrayList */ function genericContext(ArrayList $list): ArrayList { return $list->map( /** @param B $_a */ function ($_a) { return [$_a]; } ); }', 'error_message' => 'InvalidArgument', ] ]; } }