[ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => 'foo("string"));', ], 'genericArrayKeys' => [ 'code' => ' $arr * @return list */ function my_array_keys($arr) { return array_keys($arr); } $a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);', 'assertions' => [ '$a' => 'list', ], ], 'genericNonEmptyArrayKeys' => [ 'code' => ' $arr * @return non-empty-list */ function my_array_keys($arr) { return array_keys($arr); } $a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);', 'assertions' => [ '$a' => 'non-empty-list', ], ], 'genericArrayFlip' => [ 'code' => ' $arr * @return array */ function my_array_flip($arr) { return array_flip($arr); } $b = my_array_flip(["hello" => 5, "goodbye" => 6]);', 'assertions' => [ '$b' => 'array', ], ], 'byRefKeyValueArray' => [ 'code' => ' $arr */ function byRef(array &$arr) : void {} $b = ["a" => 5, "c" => 6]; byRef($b);', 'assertions' => [ '$b' => 'array', ], ], 'byRefMixedKeyArray' => [ 'code' => ' $arr */ function byRef(array &$arr) : void {} $b = ["a" => 5, "c" => 6]; byRef($b);', 'assertions' => [ '$b' => 'array', ], ], 'mixedArrayPop' => [ 'code' => ' $arr * @return TValue|null */ function my_array_pop(array &$arr) { return array_pop($arr); } /** @var mixed */ $b = ["a" => 5, "c" => 6]; $a = my_array_pop($b);', 'assertions' => [ '$a' => 'mixed', '$b' => 'array', ], 'ignored_issues' => ['MixedAssignment', 'MixedArgument'], ], 'genericArrayPop' => [ 'code' => ' $arr * @return TValue|null */ function my_array_pop(array &$arr) { return array_pop($arr); } $b = ["a" => 5, "c" => 6]; $a = my_array_pop($b);', 'assertions' => [ '$a' => 'int|null', '$b' => 'array', ], ], 'templateCallableReturnType' => [ 'code' => ' [ 'code' => ' [ 'code' => ' $t * @return array */ function f(Traversable $t): array { $ret = []; foreach ($t as $k => $v) $ret[$k] = $v; return $ret; } /** @return Generator */ function g():Generator { yield new stdClass; } takesArrayOfStdClass(f(g())); /** @param array $p */ function takesArrayOfStdClass(array $p): void {}', ], 'splatTemplateParam' => [ 'code' => ' $arr * @param array $arr2 * @return array */ function splat_proof(array $arr, array $arr2) { return $arr; } $foo = [ [1, 2, 3], [1, 2], ]; $a = splat_proof(...$foo);', 'assertions' => [ '$a' => 'array, int>', ], ], 'passArrayByRef' => [ 'code' => ' $_arr * @return null|TValue * @psalm-ignore-nullable-return */ function fRef(array &$_arr) { return array_shift($_arr); } /** * @template TKey as array-key * @template TValue * * @param array $_arr * @return null|TValue * @psalm-ignore-nullable-return */ function fNoRef(array $_arr) { return array_shift($_arr); }', ], 'classTemplateAsCorrect' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => 'bar(); /** @var T&D */ $b = $some_t; $b->bar(); /** @var D&T */ $b = $some_t; $b->bar(); return $a; }', 'assertions' => [], 'ignored_issues' => ['MixedAssignment', 'MissingParamType'], ], 'bindFirstTemplatedClosureParameterValid' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => 'getIterator();', 'assertions' => [ '$i' => 'Traversable', ], ], 'upcastArrayToIterable' => [ 'code' => ' $collection * @return V * @psalm-suppress InvalidReturnType */ function first($collection) {} $one = first([1,2,3]);', 'assertions' => [ '$one' => 'int', ], ], 'templateIntersectionLeft' => [ 'code' => ' [ 'code' => ' [ 'code' => '|TReturn) $gen * @return array */ function call(callable $gen) : array { $return = $gen(); if ($return instanceof Generator) { return [$return->getReturn()]; } /** @var array */ $wrapped_gen = [$gen]; return $wrapped_gen; } $arr = call( function() { yield 1; return "hello"; } );', 'assertions' => [ '$arr' => 'array', ], ], 'templateOfWithSpace' => [ 'code' => ' */ class Foo { } /** * @param Foo> $a */ function bar(Foo $a) : void {}', ], 'allowUnionTypeParam' => [ 'code' => ' $y */ function example($x, $y): void {} example( /** * @param int|false $x */ function($x): void {}, [rand(0, 1) ? 5 : false] );', ], 'functionTemplateUnionType' => [ 'code' => ' [ '$s' => 'string', '$i' => 'int', ], ], 'reconcileTraversableTemplatedAndNormal' => [ 'code' => 'getIterator(); $t = $a; } if (!$t instanceof Iterator) { return; } if (rand(0, 1) && rand(0, 1)) { $t->next(); } }', ], 'keyOfTemplate' => [ 'code' => ' * * @param T $o * @param K $name * * @return T[K] */ function getOffset(array $o, $name) { return $o[$name]; } $a = ["foo" => "hello", "bar" => 2]; $b = getOffset($a, "foo"); $c = getOffset($a, "bar");', 'assertions' => [ '$b' => 'string', '$c' => 'int', ], ], 'dontGeneraliseBoundParamWithWiderCallable' => [ 'code' => ' [ '$c' => 'C', ], ], 'allowTemplateTypeBeingUsedInsideFunction' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => 'id = $id; } public static function fromString(string $id): self { return new self($id); } } /** * @template T * @psalm-param callable(string): T $generator * @psalm-return callable(): T */ function idGenerator(callable $generator) { return static function () use ($generator) { return $generator("random id"); }; } function client(Id $id): void { } $staticIdGenerator = idGenerator([Id::class, "fromString"]); client($staticIdGenerator());', ], 'noCrashWhenTemplatedClassIsStatic' => [ 'code' => 'newInstance(); }', ], 'unboundVariableIsEmpty' => [ 'code' => ' [ 'code' => ' $arr * @param-out list $arr */ function example_sort_by_ref(array &$arr): bool { $arr = array_values($arr); return true; } /** * @param array $array * @return list */ function example(array $array): array { example_sort_by_ref($array); return $array; }', ], 'narrowTemplateTypeWithIsObject' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' $collection * @psalm-param callable(T, T): int $sorter * @psalm-return array */ function order(array $collection, callable $sorter): array { usort($collection, $sorter); return $collection; }', ], 'callableInference' => [ 'code' => ' $a * @return list */ function baz(array $a) { return array_map("from_other", $a); }', ], 'templateFlipIntersection' => [ 'code' => ' [ 'code' => ' ...$iterators * @return Generator */ function joinBySplat(array ...$iterators): iterable { foreach ($iterators as $iter) { foreach ($iter as $value) { yield $value; } } } /** * @return Generator> */ function genIters(): Generator { yield [1,2,3]; yield [4,5,6]; } $values = joinBySplat(...genIters()); foreach ($values as $value) { echo $value; }', ], 'allowTemplatedCast' => [ 'code' => ' [ 'code' => ' $result * @param (callable(Tk, Tk): int) $comparator * * @preturn array */ function sort_by_key(array $result, callable $comparator): array { \uksort($result, $comparator); return $result; }', ], 'uksortNamespaced' => [ 'code' => ' $result * @param (callable(Tk, Tk): int) $comparator * * @preturn array */ function sort_by_key(array $result, callable $comparator): array { \uksort($result, $comparator); return $result; }', ], 'mockObject' => [ 'code' => ' $originalClassName * @psalm-return MockObject&T1 */ function foo(string $originalClassName): MockObject { return createMock($originalClassName); } /** * @psalm-suppress InvalidReturnType * @psalm-suppress InvalidReturnStatement * * @psalm-template T2 of object * @psalm-param class-string $originalClassName * @psalm-return MockObject&T2 */ function createMock(string $originalClassName): MockObject { return new MockObject; }', ], 'testStringCallableInference' => [ 'code' => ' $iter * @return list */ function toArray(iterable $iter): array { $data = []; foreach ($iter as $val) { $data[] = $val; } return $data; } /** * @template T * @template U * @param callable(T): U $predicate * @return callable(iterable): iterable */ function map(callable $predicate): callable { return /** @param iterable $iter */ function(iterable $iter) use ($predicate): iterable { foreach ($iter as $key => $value) { yield $key => $predicate($value); } }; } /** @param list $strings */ function _test(array $strings): void {} $a = map([A::class, "dup"])(["a", "b", "c"]);', 'assertions' => [ '$a' => 'iterable', ], ], 'testClosureCallableInference' => [ 'code' => ' $iter * @return list */ function toArray(iterable $iter): array { $data = []; foreach ($iter as $val) { $data[] = $val; } return $data; } /** * @template T * @template U * @param callable(T): U $predicate * @return callable(iterable): iterable */ function map(callable $predicate): callable { return /** @param iterable $iter */ function(iterable $iter) use ($predicate): iterable { foreach ($iter as $key => $value) { yield $key => $predicate($value); } }; } /** @param list $strings */ function _test(array $strings): void {} $a = map( function (string $a) { return $a . $a; } )(["a", "b", "c"]);', 'assertions' => [ '$a' => 'iterable', ], ], 'possiblyNullMatchesTemplateType' => [ 'code' => ' [ '$a' => 'A', ], ], 'possiblyNullMatchesAnotherTemplateType' => [ 'code' => ' $className * @psalm-param Closure( * RealObjectType|null * ) : void $initializer */ function createProxy( string $className, Closure $initializer ) : void {} class Foo {} createProxy(Foo::class, function (?Foo $f) : void {});', ], 'assertIntersectionsOnTemplatedTypes' => [ 'code' => ' [ 'code' => ' $className * @param Closure(T):void $outmaker * @return T */ function createProxy( string $className, Closure $outmaker ) : object { /** @psalm-suppress MixedMethodCall */ $t = new $className(); $outmaker($t); return $t; } class A { public function bar() : void {} } createProxy(A::class, function(object $o):void {})->bar();', ], 'bottomTypeInNamespacedCallableShouldMatch' => [ 'code' => ' $className * @param callable(T):void $outmaker * @return T */ function createProxy( string $className, callable $outmaker ) : object { /** @psalm-suppress MixedMethodCall */ $t = new $className(); $outmaker($t); return $t; } class A { public function bar() : void {} } function foo(A $o):void {} createProxy(A::class, \'Ns\foo\')->bar();', ], 'compareToEmptyArray' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' $func($arg1, $arg2); } /** * @template TArg1 * @template TArg2 * @template TRes * * @template T as (Closure(): TRes | Closure(TArg1): TRes | Closure(TArg1, TArg2): TRes) * * @psalm-param T $fn * @psalm-param TArg1 $arg */ function foo(Closure $fn, $arg): void { $a = partial($fn, $arg); }', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '7.4', ], 'mixedDoesntSwallowNull' => [ 'code' => ' [ 'code' => ' [ 'code' => ' $input * @psalm-return Iterator */ function to_iterator(iterable $input): Iterator { if (\is_array($input)) { return new \ArrayIterator($input); } elseif ($input instanceof Iterator) { return $input; } else { return new \IteratorIterator($input); } }', ], 'doublyNestedFunctionTemplates' => [ 'code' => ' $iterable * @psalm-param (callable(Tk, Tv): bool)|null $predicate * * @psalm-return iterable */ function filter_with_key(iterable $iterable, ?callable $predicate = null): iterable { return (static function () use ($iterable, $predicate): Generator { $predicate = $predicate ?? /** * @psalm-param Tk $_k * @psalm-param Tv $v * * @return bool */ function($_k, $v) { return (bool) $v; }; foreach ($iterable as $k => $v) { if ($predicate($k, $v)) { yield $k => $v; } } })(); }', ], 'allowClosureParamLowerBoundAndUpperBound' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [], 'ignored_issues' => [], 'php_version' => '8.0', ], 'templateChildClass' => [ 'code' => 'add($default); return $default; } }', ], 'isArrayCheckOnTemplated' => [ 'code' => ' [ 'code' => ' * @param TArray $arr * @return TValue */ function toList(array $arr): array { return reset($arr); }', ], 'callTemplatedFunctionWithTemplatedClassString' => [ 'code' => ' $type * @psalm-return Tb * @psalm-suppress InvalidReturnType */ function deserialize_object(string $data, string $type) {}', ], 'arrayKeyInTemplateOfArrayKey' => [ 'code' => ' $iterable * @psalm-param callable(TKey): iterable $mapper * @psalm-return \Generator */ function map(iterable $iterable, callable $mapper): Generator { foreach ($iterable as $key => $_) { yield from $mapper($key); } } /** * @psalm-return iterable */ function iter(): iterable { return []; } /** * @template TKey of array-key * @psalm-param TKey $key * @psalm-return Generator */ function mapper($key): Generator { yield $key => "a"; } map(iter(), "mapper");', ], 'dontScreamForArithmeticsOnIntTemplates' => [ 'code' => ' [ 'code' => ' [ 'code' => ' */ class StringNorm implements Norm { public function normalize(mixed $input): mixed { return strtolower($input); } } /** * @template TNorm * * @param TNorm $value * @param Norm $n */ function normalizeField(mixed $value, Norm $n): void { $n->normalize($value); } normalizeField("foo", new StringNorm());', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '8.0', ], 'templateWithCommentAfterSimpleType' => [ 'code' => ' [ 'code' => ' 'InvalidScalarArgument', ], 'invalidTemplatedStaticMethodType' => [ 'code' => ' 'InvalidScalarArgument', ], 'invalidTemplatedInstanceMethodType' => [ 'code' => 'foo(4));', 'error_message' => 'InvalidScalarArgument', ], 'replaceChildTypeNoHint' => [ 'code' => ' $t * @return array */ function f(Traversable $t): array { $ret = []; foreach ($t as $k => $v) $ret[$k] = $v; return $ret; } function g():Generator { yield new stdClass; } takesArrayOfStdClass(f(g())); /** @param array $p */ function takesArrayOfStdClass(array $p): void {}', 'error_message' => 'MixedArgumentTypeCoercion', ], 'classTemplateAsIncorrectClass' => [ 'code' => ' 'InvalidArgument', ], 'classTemplateAsIncorrectInterface' => [ 'code' => ' 'InvalidArgument', ], 'templateFunctionMethodCallWithoutMethod' => [ 'code' => 'bar(); }', 'error_message' => 'PossiblyUndefinedMethod', ], 'templateFunctionMethodCallWithoutAsType' => [ 'code' => 'bar(); }', 'error_message' => 'MixedMethodCall', ], 'bindFirstTemplatedClosureParameterInvalidScalar' => [ 'code' => ' 'InvalidScalarArgument', ], 'bindFirstTemplatedClosureParameterTypeCoercion' => [ 'code' => ' 'ArgumentTypeCoercion', ], 'callableDoesNotReturnItself' => [ 'code' => ' 'InvalidArgument', ], 'multipleArgConstraintWithMoreRestrictiveFirstArg' => [ 'code' => ' 'ArgumentTypeCoercion', ], 'multipleArgConstraintWithMoreRestrictiveSecondArg' => [ 'code' => ' 'ArgumentTypeCoercion', ], 'multipleArgConstraintWithLessRestrictiveThirdArg' => [ 'code' => ' 'ArgumentTypeCoercion', ], 'possiblyInvalidArgumentWithUnionFirstArg' => [ 'code' => ' 'PossiblyInvalidArgument', ], 'possiblyInvalidArgumentWithUnionSecondArg' => [ 'code' => ' 'PossiblyInvalidArgument', ], 'preventTemplateTypeAsBeingUsedInsideFunction' => [ 'code' => ' 'InvalidArgument', ], 'preventWrongTemplateBeingPassed' => [ 'code' => ' 'InvalidArgument', ], 'preventTemplateTypeReturnMoreGeneral' => [ 'code' => ' 'InvalidReturnStatement', ], 'preventReturningString' => [ 'code' => ' 'InvalidReturnStatement', ], 'unTemplatedVarOnReturn' => [ 'code' => ' 'InvalidReturnStatement', ], 'templateReturnTypeOfCallableWithIncompatibleType' => [ 'code' => ' 'InvalidArgument', ], 'templateInvokeArg' => [ 'code' => ' 'InvalidArgument', ], 'invalidTemplateDocblock' => [ 'code' => ' 'MissingDocblockType', ], 'returnNamedObjectWhereTemplateIsExpected' => [ 'code' => ' 'InvalidReturnStatement', ], 'returnIntersectionWhenTemplateIsExpectedForward' => [ 'code' => ' 'LessSpecificReturnStatement', ], 'returnIntersectionWhenTemplateIsExpectedBackward' => [ 'code' => ' 'LessSpecificReturnStatement', ], 'bottomTypeInClosureShouldClash' => [ 'code' => ' $className * @param Closure(T):void $outmaker * @return T */ function createProxy( string $className, Closure $outmaker ) : object { /** @psalm-suppress MixedMethodCall */ $t = new $className(); $outmaker($t); return $t; } class A { public function bar() : void {} } class B {} createProxy(A::class, function(B $o):void {})->bar();', 'error_message' => 'InvalidArgument', ], 'bottomTypeInNamespacedCallableShouldClash' => [ 'code' => ' $className * @param callable(T):void $outmaker * @return T */ function createProxy( string $className, callable $outmaker ) : object { /** @psalm-suppress MixedMethodCall */ $t = new $className(); $outmaker($t); return $t; } class A { public function bar() : void {} } class B {} function foo(B $o):void {} createProxy(A::class, \'Ns\foo\')->bar();', 'error_message' => 'InvalidArgument', ], 'preventBadArraySubtyping' => [ 'code' => ' 123]; return $b; }', 'error_message' => 'InvalidReturnStatement', ], 'modifyTemplatedShape' => [ 'code' => ' 'InvalidReturnStatement', ], 'preventArrayOverwriting' => [ 'code' => ' 'InvalidReturnStatement', ], 'catchIssueInTemplatedFunctionInsideClass' => [ 'code' => ' $c */ function jsonFromEntityCollection(Container $c): void { $c->take("foo"); } }', 'error_message' => 'InvalidArgument', ], ]; } }