,ignored_issues?:list,php_version?:string}> */ public function providerValidCodeParse(): iterable { return [ 'cachingIterator' => [ 'code' => 'hasNext(); $key = $decoratorIterator->key(); $value = $decoratorIterator->current(); ', 'assertions' => [ '$key' => 'int|null', '$value' => 'null|string', '$next' => 'bool', ], ], 'infiniteIterator' => [ 'code' => 'key(); $value = $decoratorIterator->current(); ', 'assertions' => [ '$key' => 'int|null', '$value' => 'null|string', ], ], 'limitIterator' => [ 'code' => 'key(); $value = $decoratorIterator->current(); ', 'assertions' => [ '$key' => 'int|null', '$value' => 'null|string', ], ], 'callbackFilterIterator' => [ 'code' => 'key(); $value = $decoratorIterator->current(); ', 'assertions' => [ '$key' => 'int|null', '$value' => 'null|string', ], ], 'noRewindIterator' => [ 'code' => 'key(); $value = $decoratorIterator->current(); ', 'assertions' => [ '$key' => 'int|null', '$value' => 'null|string', ], ], 'classTemplate' => [ 'code' => ' $T */ public function __construct(string $T) { $this->T = $T; } /** * @return T * @psalm-suppress MixedMethodCall */ public function bar() { $t = $this->T; return new $t(); } } $at = "A"; /** * @var Foo * @psalm-suppress ArgumentTypeCoercion */ $afoo = new Foo($at); $afoo_bar = $afoo->bar(); $bfoo = new Foo(B::class); $bfoo_bar = $bfoo->bar(); // this shouldn’t cause a problem as it’s a docbblock type if (!($bfoo_bar instanceof B)) {} $c = C::class; $cfoo = new Foo($c); $cfoo_bar = $cfoo->bar();', 'assertions' => [ '$afoo' => 'Foo', '$afoo_bar' => 'A', '$bfoo' => 'Foo', '$bfoo_bar' => 'B', '$cfoo' => 'Foo', '$cfoo_bar' => 'C', ], 'ignored_issues' => [ 'MixedReturnStatement', 'LessSpecificReturnStatement', 'DocblockTypeContradiction', ], ], 'classTemplateSelfs' => [ 'code' => ' */ public $T; /** * @param class-string $T */ public function __construct(string $T) { $this->T = $T; } /** * @return T * @psalm-suppress MixedMethodCall */ public function bar() { $t = $this->T; return new $t(); } } class E { /** * @return Foo */ public static function getFoo() { return new Foo(__CLASS__); } /** * @return Foo */ public static function getFoo2() { return new Foo(self::class); } /** * @return Foo */ public static function getFoo3() { return new Foo(static::class); } } class G extends E {} $efoo = E::getFoo(); $efoo2 = E::getFoo2(); $efoo3 = E::getFoo3(); $gfoo = G::getFoo(); $gfoo2 = G::getFoo2(); $gfoo3 = G::getFoo3();', 'assertions' => [ '$efoo' => 'Foo', '$efoo2' => 'Foo', '$efoo3' => 'Foo', '$gfoo' => 'Foo', '$gfoo2' => 'Foo', '$gfoo3' => 'Foo', ], 'ignored_issues' => [ 'LessSpecificReturnStatement', 'RedundantConditionGivenDocblockType', ], ], 'classTemplateExternalClasses' => [ 'code' => ' $T */ public function __construct(string $T) { $this->T = $T; } /** * @return T * @psalm-suppress MixedMethodCall */ public function bar() { $t = $this->T; return new $t(); } } $efoo = new Foo(\Exception::class); $efoo_bar = $efoo->bar(); $ffoo = new Foo(\LogicException::class); $ffoo_bar = $ffoo->bar();', 'assertions' => [ '$efoo' => 'Foo', '$efoo_bar' => 'Exception', '$ffoo' => 'Foo', '$ffoo_bar' => 'LogicException', ], 'ignored_issues' => ['LessSpecificReturnStatement'], ], 'classTemplateContainerSimpleCall' => [ 'code' => 'obj = $obj; } /** * @return T */ public function bar() { return $this->obj; } } $afoo = new Foo(new A()); $afoo_bar = $afoo->bar();', 'assertions' => [ '$afoo' => 'Foo', '$afoo_bar' => 'A', ], ], 'classTemplateContainerThisCall' => [ 'code' => 'obj = $obj; } /** * @return T */ public function bar() { return $this->obj; } /** * @return T */ public function bat() { return $this->bar(); } public function __toString(): string { return "hello " . $this->obj; } }', 'assertions' => [], 'ignored_issues' => ['MixedOperand'], ], 'validPsalmTemplatedClassType' => [ 'code' => 'bar(new A());', ], 'intersectionTemplatedTypes' => [ 'code' => ' */ private $data; /** @psalm-param iterable $data */ public function __construct(iterable $data) { $this->data = $data; } } class Item {} /** @psalm-param Collection $c */ function takesCollectionOfItems(Collection $c): void {} /** @psalm-var iterable $data2 */ $data2 = []; takesCollectionOfItems(new Collection($data2)); /** @psalm-var iterable&Countable $data */ $data = []; takesCollectionOfItems(new Collection($data));', ], 'repeatedCall' => [ 'code' => ' */ private $data; /** @param array $data */ public function __construct(array $data) { $this->data = $data; } /** * @template T * @param Closure(TValue):T $func * @return ArrayCollection */ public function map(Closure $func) { return new static(array_map($func, $this->data)); } } class Item {} /** * @param ArrayCollection $i */ function takesCollectionOfItems(ArrayCollection $i): void {} $c = new ArrayCollection([ new Item ]); takesCollectionOfItems($c); takesCollectionOfItems($c->map(function(Item $i): Item { return $i;})); takesCollectionOfItems($c->map(function(Item $i): Item { return $i;}));', ], 'noRepeatedTypeException' => [ 'code' => ' */ private $type; /** @var array */ private $items; /** * @param class-string $type */ public function __construct(string $type) { if (!in_array($type, [A::class, B::class], true)) { throw new \InvalidArgumentException; } $this->type = $type; $this->items = []; } /** @param T $item */ public function add($item): void { $this->items[] = $item; } } class FooFacade { /** * @template T as object * @param T $item */ public function add(object $item): void { $foo = $this->ensureFoo([$item]); $foo->add($item); } /** * @template T as object * @param array $items * @return Foo */ private function ensureFoo(array $items): Foo { /** @var class-string */ $type = $items[0] instanceof A ? A::class : B::class; return new Foo($type); } } class A {} class B {}', ], 'collectionOfClosure' => [ 'code' => ' */ public function filter(Closure $p) { return $this; } } class I {} /** @var Collection> $c */ $c = new Collection; $c->filter( /** @param Collection $elt */ function(Collection $elt): bool { return (bool) rand(0,1); } ); $c->filter( /** @param Collection $elt */ function(Collection $elt): bool { return true; } );', ], 'templatedInterfaceIteration' => [ 'code' => ' */ interface ICollection extends \IteratorAggregate { /** @return \Traversable */ public function getIterator(); } /** * @template TKey as array-key * @template TValue * * @implements ICollection */ class Collection implements ICollection { /** @var array */ private $data; /** * @param array $data */ public function __construct(array $data) { $this->data = $data; } /** * @return \Traversable */ public function getIterator(): \Traversable { return new \ArrayIterator($this->data); } } $c = new Collection(["a" => 1]); foreach ($c as $k => $v) { atan($v); strlen($k); }', ], 'allowTemplatedIntersectionToExtend' => [ 'code' => 'bar = $bar; } /** * @return T&Foo */ public function makeFoo() { return $this->bar; } }', ], 'restrictTemplateInputWithTClassGoodInput' => [ 'code' => ' */ private $items; /** * @param T::class $type */ public function __construct(string $type) { if (!in_array($type, [A::class, B::class], true)) { throw new \InvalidArgumentException; } $this->type = $type; $this->items = []; } /** @param T $item */ public function add($item): void { $this->items[] = $item; } } class A {} class B {} $foo = new Foo(A::class); $foo->add(new A);', ], 'classTemplateFunctionImplementsInterface' => [ 'code' => 't = $t; } /** * @return T */ public function getFoo() { return $this->t; } } function passFoo(Foo $f) : Foo { return (new FooGetter($f))->getFoo(); }', ], 'getPropertyOnClass' => [ 'code' => ' */ private $type; /** * @param class-string $type */ public function __construct(string $type) { $this->type = $type; } /** * @return class-string */ public function getType() { return $this->type; } /** * @param T $object */ public function bar(Foo $object) : void { if ($this->getType() !== get_class($object)) { return; } echo $object->id; } } class FooChild extends Foo {} /** @param Collection $c */ function handleCollectionOfFoo(Collection $c) : void { if ($c->getType() === FooChild::class) {} }', ], 'getMagicPropertyOnClass' => [ 'code' => 'x; $b_y = $b->y; ', 'assertions' => [ '$b_x' => 'A|null', '$b_y' => 'A|null', ], ], 'getMagicPropertyOnThis' => [ 'code' => 'x instanceof X) {} if ($this->y instanceof X) {} } } ', ], 'getEquateClass' => [ 'code' => 'obj = $obj; } /** * @param T $object */ public function bar(Foo $object) : void { if ($this->obj === $object) {} } }', ], 'allowComparisonGetTypeResult' => [ 'code' => ' */ private $type; /** * @param class-string $type */ public function __construct(string $type) { $this->type = $type; } /** * @return class-string|null */ public function getType() { return $this->type; } } function foo(Collection $c) : void { $val = $c->getType(); if (!$val) {} if ($val) {} }', ], 'mixedTemplatedParamOutWithNoExtendedTemplate' => [ 'code' => 'v = $v; } /** * @return TValue */ public function getValue() { return $this->v; } } /** * @psalm-suppress MissingTemplateParam * @template TKey * @template TValue */ class KeyValueContainer extends ValueContainer { /** * @var TKey */ private $k; /** * @param TKey $k * @param TValue $v */ public function __construct($k, $v) { $this->k = $k; parent::__construct($v); } /** * @return TKey */ public function getKey() { return $this->k; } } $a = new KeyValueContainer("hello", 15); $b = $a->getValue();', 'assertions' => [ '$a' => 'KeyValueContainer', '$b' => 'mixed', ], 'ignored_issues' => ['MixedAssignment'], ], 'mixedTemplatedParamOutDifferentParamName' => [ 'code' => 'v = $v; } /** * @return TValue */ public function getValue() { return $this->v; } } /** * @template TKey * @template Tv * * @psalm-suppress MissingTemplateParam */ class KeyValueContainer extends ValueContainer { /** * @var TKey */ private $k; /** * @param TKey $k * @param Tv $v */ public function __construct($k, $v) { $this->k = $k; parent::__construct($v); } /** * @return TKey */ public function getKey() { return $this->k; } } $a = new KeyValueContainer("hello", 15); $b = $a->getValue();', 'assertions' => [ '$a' => 'KeyValueContainer', '$b' => 'mixed', ], 'ignored_issues' => ['MixedAssignment'], ], 'doesntExtendTemplateAndDoesNotOverride' => [ 'code' => 'id = $id; } /** * @return T */ public function getID() { return $this->id; } } /** * @psalm-suppress MissingTemplateParam */ class AppUser extends User {} $au = new AppUser(-1); $id = $au->getId();', 'assertions' => [ '$au' => 'AppUser', '$id' => 'array-key', ], ], 'templateTKeyedArrayValues' => [ 'code' => ',1:Collection} * @psalm-suppress InvalidReturnType */ public function partition() {} } /** @var Collection $c */ $c = new Collection; [$partA, $partB] = $c->partition();', 'assertions' => [ '$partA' => 'Collection', '$partB' => 'Collection', ], ], 'doublyLinkedListConstructor' => [ 'code' => 'add(5, "hello"); $list->add("hello", 5); /** @var SplDoublyLinkedList */ $templated_list = new SplDoublyLinkedList(); $templated_list->add(5, "hello"); $a = $templated_list->bottom();', 'assertions' => [ '$a' => 'string', ], ], 'templateDefaultSimpleString' => [ 'code' => 't = $t; } } $c = new C();', 'assertions' => [ '$c===' => "C<'hello'>", ], ], 'SKIPPED-templateDefaultConstant' => [ 'code' => 't = $t; } } $e = new E();', 'assertions' => [ '$e===' => 'E', ], ], 'SKIPPED-templateDefaultClassMemberConstant' => [ 'code' => 't = $t; } } $e = new E();', 'assertions' => [ '$e===' => 'E', ], ], 'templateDefaultClassConstant' => [ 'code' => ' */ public $t; /** * @param class-string $t */ function __construct(string $t = D::class) { $this->t = $t; } } $e = new E();', 'assertions' => [ '$e===' => 'E', ], ], 'allowNullablePropertyAssignment' => [ 'code' => ' $foo */ public function __construct(I $foo) { $this->bar = $foo->get(); } }', ], 'reflectionClass' => [ 'code' => ' $name */ class CustomReflectionClass { /** * @var class-string */ public $name; /** * @param T|class-string $argument */ public function __construct($argument) { if (is_object($argument)) { $this->name = get_class($argument); } else { $this->name = $argument; } } } /** * @template T as object * @param class-string $className * @return CustomReflectionClass */ function getTypeOf(string $className) { return new CustomReflectionClass($className); }', ], 'psalmReflectionClass' => [ 'code' => ' $name */ class CustomReflectionClass { /** * @var class-string */ public $name; /** * @param T|class-string $argument */ public function __construct($argument) { if (is_object($argument)) { $this->name = get_class($argument); } else { $this->name = $argument; } } } /** * @template T as object * @param class-string $className * @return CustomReflectionClass */ function getTypeOf(string $className) { return new CustomReflectionClass($className); }', ], 'ignoreTooManyGenericObjectArgs' => [ 'code' => 't = $t; } } /** @param C $c */ function takesC(C $c) : void {} /** * @psalm-suppress TooManyTemplateParams * @var C */ $c = new C(5); takesC($c);', ], 'classTemplateUnionType' => [ 'code' => ' $c */ function foo(C $c) : void {} /** @param C $c */ function bar(C $c) : void {}', ], 'unionAsTypeReturnType' => [ 'code' => ' */ public function filter(Closure $p); }', ], 'converterObject' => [ 'code' => ' $records */ public $records; /** * @param array $records */ public function __construct(array $records) { $this->records = $records; } /** * @template Q2 as object * * @param Q2 $obj2 * * @return array */ private function appender(object $obj2): array { $arr = []; foreach ($this->records as $key => $obj) { if (rand(0, 1)) { $obj = $obj2; } $arr[$key] = $obj; } return $arr; } /** * @template Q1 as object * * @param Q1 $obj * * @return array */ public function appendProperty(object $obj): array { return $this->appender($obj); } }', ], 'converterClassString' => [ 'code' => ' $records */ public $records; /** * @param array $records */ public function __construct(array $records) { $this->records = $records; } /** * @template Q as object * * @param class-string $obj * * @return array */ public function appendProperty(string $obj): array { return $this->appender($obj); } /** * @template Q as object * * @param class-string $obj2 * * @return array * * @psalm-suppress MixedMethodCall */ private function appender(string $obj2): array { $arr = []; foreach ($this->records as $key => $obj) { if (rand(0, 1)) { $obj = new $obj2; } $arr[$key] = $obj; } return $arr; } }', ], 'allowTemplateReconciliation' => [ 'code' => ' [ 'code' => ' */ private $elements; /** * @param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } } /** @psalm-suppress MixedArgument */ $c = new ArrayCollection($_GET["a"]);', 'assertions' => [ '$c' => 'ArrayCollection', ], ], 'doNotCombineTypes' => [ 'code' => 't = $t; } /** * @return T */ public function get() { return $this->t; } } /** * @param C $a * @param C $b * @return C|C */ function randomCollection(C $a, C $b) : C { if (rand(0, 1)) { return $a; } return $b; } $random_collection = randomCollection(new C(new A), new C(new B)); $a_or_b = $random_collection->get();', 'assertions' => [ '$random_collection' => 'C|C', '$a_or_b' => 'A|B', ], ], 'doNotCombineTypesWhenMemoized' => [ 'code' => 't = $t; } /** * @return T * @psalm-mutation-free */ public function get() { return $this->t; } } /** @var C|C $random_collection **/ $a_or_b = $random_collection->get();', 'assertions' => [ '$random_collection' => 'C|C', '$a_or_b' => 'A|B', ], ], 'inferClosureParamTypeFromContext' => [ 'code' => ' */ function map(callable $action): self; } /** * @template T */ interface Optional { /** * @return T */ function get(); } /** * @param Collection> $collection * @return Collection */ function expandOptions(Collection $collection) : Collection { return $collection->map( function ($optional) { return $optional->get(); } ); }', ], 'templateEmptyParamCoercion' => [ 'code' => ' */ private $data; /** @psalm-param iterable $data */ public function __construct(iterable $data = []) { $this->data = $data; } } class Item {} /** @psalm-param Collection $c */ function takesCollectionOfItems(Collection $c): void {} takesCollectionOfItems(new Collection()); takesCollectionOfItems(new Collection([]));', ], 'templatedGet' => [ 'code' => ' */ protected $data = []; /** @param array $data */ public function __construct(array $data) { $this->data = $data; } /** @param P $name */ public function __isset(string $name): bool { return isset($this->data[$name]); } /** * @param P $name * @return V */ public function __get(string $name) { return $this->data[$name]; } } $p = new PropertyBag(["a" => "data for a", "b" => "data for b"]); $a = $p->a;', 'assertions' => [ '$a' => 'string', ], ], 'templateAsArray' => [ 'code' => ' */ abstract class Foo { /** * @var DATA */ protected $data; /** * @param DATA $data */ public function __construct(array $data) { $this->data = $data; } /** * @return scalar|array|object|null */ public function __get(string $property) { return isset($this->data[$property]) ? $this->data[$property] : null; } }', ], 'keyOfClassTemplateAcceptingIndexedAccess' => [ 'code' => 'data = $data; } /** * @template K as key-of * * @param K $property * @param TData[K] $value */ public function __set(string $property, $value) { $this->data[$property] = $value; } }', ], 'keyOfClassTemplateReturningIndexedAccess' => [ 'code' => 'data = $data; } /** * @template K as key-of * * @param K $property * * @return TData[K] */ public function __get(string $property) { return $this->data[$property]; } }', ], 'SKIPPED-templatedInterfaceIntersectionFirst' => [ 'code' => '&IChild */ function makeConcrete() : IChild { return new class() implements IChild { public function foo() { return new C(); } }; } $a = makeConcrete()->foo();', 'assertions' => [ '$a' => 'C', ], ], 'templatedInterfaceIntersectionSecond' => [ 'code' => ' */ function makeConcrete() : IChild { return new class() implements IChild { public function foo() { return new C(); } }; } $a = makeConcrete()->foo();', 'assertions' => [ '$a' => 'C', ], ], 'returnTemplateIntersectionGenericObjectAndTemplate' => [ 'code' => ' $className * * @psalm-return T&I * * @psalm-suppress MissingTemplateParam */ function makeConcrete(string $className) : object { /** @var T&I */ return new class() extends C implements I { public function getMe() { return $this; } }; } $a = makeConcrete(C::class);', 'assertions' => [ '$a' => 'C&I', ], ], 'keyOfArrayGet' => [ 'code' => ' */ abstract class Foo { /** * @var DATA */ protected $data; /** * @param DATA $data */ public function __construct(array $data) { $this->data = $data; } /** * @template K as key-of * * @param K $property * * @return DATA[K] */ public function __get(string $property) { return $this->data[$property]; } }', ], 'keyOfArrayRandomKey' => [ 'code' => ' */ abstract class Foo { /** * @var DATA */ protected $data; /** * @param DATA $data */ public function __construct(array $data) { $this->data = $data; } /** * @return key-of */ abstract public function getRandomKey() : string; }', ], 'allowBoolTemplateCoercion' => [ 'code' => ' */ function test(): TestPromise { return new TestPromise(true); }', ], 'classTemplatedPropertyEmptyAssignment' => [ 'code' => ' */ private $FooArray; public function __construct() { $this->FooArray = new Foo(function(): array { return []; }); } }', ], 'classTemplatedPropertyAssignmentWithMoreSpecificArray' => [ 'code' => ' */ private $FooArray; public function __construct() { $this->FooArray = new Foo(function(): array { return []; }); } }', ], 'insideClosureVarTemplate' => [ 'code' => ' [ 'code' => ' $a * @psalm-return ReflectionClass */ public function reflection(string $a) { return new ReflectionClass($a); } }', ], 'anonymousClassMustNotBreakParentTemplate' => [ 'code' => 'value = $val; /** @psalm-suppress MissingTemplateParam */ new class extends Foo {}; } /** @psalm-return ?T */ public function get() { return $this->value; } }' ], 'templatedInvoke' => [ 'code' => 'value = $val; } /** @return T */ public function get() { return $this->value; } /** * @param T $val * @return Foo */ public function __invoke($val) { return new static($val); } /** * @param T $val * @return Foo */ public function create($val) { return new static($val); } } function bar(string $s) : string { $foo = new Foo($s); $bar = $foo($s); return $bar->get(); }' ], 'templatedLiteralStringReplacement' => [ 'code' => 'value = $value; } /** * @psalm-return T */ public function value() { return $this->value; } } /** * @template T * @psalm-param T $value * @psalm-return Value */ function value($value): Value { return new Value($value); } /** * @psalm-param Value $value */ function client($value): void {} client(value("awdawd"));' ], 'yieldFromGenericObjectNotExtendingIterator' => [ 'code' => ' */ class Foo extends \ArrayObject {} class A { /** * @var Foo */ public Foo $vector; /** * @param Foo $v */ public function __construct(Foo $v) { $this->vector = $v; } public function getIterator(): Iterator { yield from $this->vector; } }', 'assertions' => [], 'ignored_issues' => ['TooManyTemplateParams'] ], 'coerceEmptyArrayToGeneral' => [ 'code' => ' */ private $FooArray; public function __construct() { $this->FooArray = new Foo(function(string $s): array { /** @psalm-suppress MixedAssignment */ $json = \json_decode($s, true); if (! \is_array($json)) { return []; } return $json; }); takesFooArray($this->FooArray); } } /** @param Foo $_ */ function takesFooArray($_): void {}', ], 'allowListAcceptance' => [ 'code' => ' */ public $values; /** @param list $values */ function __construct(array $values) { $this->values = $values; } } /** @return Collection */ function makeStringCollection() { return new Collection(getStringList()); // gets typed as Collection for some reason } /** @return list */ function getStringList(): array { return ["foo", "baz"]; }' ], 'allowListAcceptanceIntoArray' => [ 'code' => ' */ public $values; /** @param array $values */ function __construct(array $values) { $this->values = $values; } } /** @return Collection */ function makeStringCollection() { return new Collection(getStringList()); // gets typed as Collection for some reason } /** @return list */ function getStringList(): array { return ["foo", "baz"]; }' ], 'allowInternalNullCheck' => [ 'code' => 'parent = $parent; } public function hasNoParent() : bool { return $this->parent === null; // So TP does contain null } }' ], 'useMethodWithExistingGenericParam' => [ 'code' => ' */ public function filter(Closure $p); } /** * @param Collection $c * @psalm-return Collection */ function filter(Collection $c, string $name) { return $c->filter( function (Bar $f) use ($name) { return $f->getFoo() === "foo"; } ); }' ], 'unboundVariableIsEmptyInInstanceMethod' => [ 'code' => 'collectInstance("a");' ], 'unboundVariableIsEmptyInStaticMethod' => [ 'code' => ' [ 'code' => ' $x * * @return array */ function iterableToArray (iterable $x): array { if (is_array($x)) { return $x; } else { return iterator_to_array($x); } } /** * @param Traversable $t * @return array */ function withParams(Traversable $t) : array { return iterableToArray($t); }', ], 'templateStaticWithParam' => [ 'code' => ' */ private $elements; /** * @param list $elements */ public function __construct(array $elements) { $this->elements = $elements; } /** * @template U * @param callable(T=):U $callback * @return static */ public function map(callable $callback) { /** @psalm-suppress RedundantFunctionCall */ return new static(array_values(array_map($callback, $this->elements))); } } /** @param ArrayCollection $ints */ function takesInts(ArrayCollection $ints) :void {} /** @param ArrayCollection $ints */ function takesIntsOrStrings(ArrayCollection $ints) :void {} takesInts((new ArrayCollection([ "a", "bc" ]))->map("strlen")); /** @return ($s is "string" ? string : int) */ function foo(string $s) { if ($s === "string") { return "hello"; } return 5; } takesIntsOrStrings((new ArrayCollection([ "a", "bc" ]))->map("foo")); /** * @template T * @extends ArrayCollection */ class LazyArrayCollection extends ArrayCollection {}' ], 'weakReferenceIsTyped' => [ 'code' => 'get(); ', 'assertions' => [ '$ex' => 'Exception|null' ], ], 'weakReferenceIsCovariant' => [ 'code' => ' $_ref */ function acceptsThrowableRef(WeakReference $_ref): void {} acceptsThrowableRef(WeakReference::create(new Exception)); ' ], 'mapTypeParams' => [ 'code' => ' */ public $arr; /** @param array $arr */ function __construct(array $arr) { $this->arr = $arr; } } /** * @template TInputKey as array-key * @template TInputValue * @param Map $map * @return Map */ function copyMapUsingProperty(Map $map): Map { return new Map($map->arr); }', ], 'mapStaticClassTemplatedFromClassString' => [ 'code' => ' $t * @return T */ function f(string $t) { return $t::factory(); } /** @template T of Base */ class C { /** @var class-string */ private string $t; /** @param class-string $t */ public function __construct($t) { $this->t = $t; } /** @return T */ public function f(): Base { $t = $this->t; return $t::factory(); } }' ], 'uasortCallableInMethod' => [ 'code' => ' $collection * @psalm-param callable(T, T): int $sorter * @psalm-return array */ function order(array $collection, callable $sorter): array { usort($collection, $sorter); return $collection; } }' ], 'intersectOnTOfObject' => [ 'code' => 'setClosure( function(A $i) : string { return "hello"; } ); }' ], 'assertionOnTemplatedClassString' => [ 'code' => ' $type * @psalm-return EQB */ public function createEQB(string $type) { if (!class_exists($type)) { throw new InvalidArgumentException(); } return new EQB($type); } } /** * @template Entity as object */ class EQB { /** * @psalm-var class-string */ protected $type; /** * @psalm-param class-string $type */ public function __construct(string $type) { $this->type = $type; } }' ], 'createEmptyArrayCollection' => [ 'code' => ' * @var array */ private $elements = []; /** * Initializes a new ArrayCollection. * * @param array $elements * * @psalm-param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } /** * @param TKey $key * @param T $t */ public function add($key, $t) : void { $this->elements[$key] = $t; } }', 'assertions' => [ '$a' => 'ArrayCollection' ] ], 'newGenericBecomesPropertyTypeValidArg' => [ 'code' => ' */ public ArrayCollection $b_collection; public function __construct() { $this->b_collection = new ArrayCollection([]); $this->b_collection->add(5, new B()); } } /** * @psalm-template TKey of array-key * @psalm-template T */ class ArrayCollection { /** * An array containing the entries of this collection. * * @psalm-var array * @var array */ private $elements = []; /** * Initializes a new ArrayCollection. * * @param array $elements * * @psalm-param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } /** * @param TKey $key * @param T $t */ public function add($key, $t) : void { $this->elements[$key] = $t; } }' ], 'allowPropertyCoercion' => [ 'code' => ' */ private $c; public function __construct() { $this->c = new ArrayCollection(); $this->c->filter(function (DateTime $dt): bool { return $dt === $dt; }); } } /** * @psalm-template TKey of array-key * @psalm-template T */ class ArrayCollection { /** * @psalm-var array * @var array */ private $elements; /** * @param array $elements * * @psalm-param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } /** * @psalm-param Closure(T=):bool $p * @psalm-return self */ public function filter(Closure $p) { return $this; } }' ], 'unionClassStringInferenceAndDefaultEmptyArray' => [ 'code' => ' $items */ protected $items = []; /** * @param array $items */ public function __construct(array $items = []) { $this->items = $items; } /** * @template C as object * @param class-string $classString * @param array $elements * @return Collection */ public static function fromClassString(string $classString, array $elements = []) : Collection { return new Collection($elements); } }', 'assertions' => [ '$packages' => 'Collection' ] ], 'assertSameOnTemplatedProperty' => [ 'code' => 'contents = $contents; } /** @param E $thing */ public function contains(object $thing) : bool { if ($this->contents !== $thing) { return false; } return true; } }' ], 'assertNotNullOnTemplatedProperty' => [ 'code' => ' */ $a = new A(); if (null !== $a->filter) {}' ], 'setTemplatedPropertyOutsideClass' => [ 'code' => 'value = $value; } } /** @psalm-var Watcher $watcher */ $watcher = new Watcher(0); $watcher->value = 0;' ], 'callableAsClassStringArray' => [ 'code' => 'id = $id; } /** * @return static */ final public static function fromString(string $id): self { return new static($id); } } /** * @template T of Id */ final class Ids { /** * @psalm-var list */ private array $ids; /** * @psalm-param list $ids */ private function __construct(array $ids) { $this->ids = $ids; } /** * @template T1 of Id * @psalm-param T1 $class * @psalm-param list $ids * @psalm-return self */ public static function fromObjects(Id $class, array $ids): self { return new self(array_map([$class, "fromString"], $ids)); } /** * @template T1 of Id * @psalm-param class-string $class * @psalm-param list $ids * @psalm-return self */ public static function fromStrings(string $class, array $ids): self { return new self(array_map([$class, "fromString"], $ids)); } }' ], 'noCrashTemplateInsideGenerator' => [ 'code' => ' */ private \Iterator $values; /** * @param \Iterator $values */ public function __construct(\Iterator $values) { $this->values = $values; } /** * @param T $element * * @return self */ public function __invoke($element): self { return new self( ( function($values, $element): \Generator { /** @var T $value */ foreach ($values as $value) { yield $value; } yield $element; } )($this->values, $element), ); } }', 'assertions' => [], 'ignored_issues' => ['MissingClosureParamType'] ], 'templatedPropertyAllowsNull' => [ 'code' => 'key = $key; } }' ], 'templatePropertyWithoutParams' => [ 'code' => ' */ public iterable $objects = []; /** * @var callable(T): void */ public $onEach; public function __construct() { $this->onEach = function (): void {}; } } function handle(Batch $message, object $o): void { $fn = $message->onEach; $fn($o); }' ], 'changePropertyTypeOfTemplate' => [ 'code' => 'x = 1; }' ], 'multipleMatchingObjectsInUnion' => [ 'code' => '> $containers * @return T */ function unwrap(array $containers) { return array_map( fn($container) => $container->get(), $containers )[0]; } /** * @param array|Container> $typed_containers */ function takesDifferentTypes(array $typed_containers) : void { $ret = unwrap($typed_containers); if (is_string($ret)) {} if (is_int($ret)) {} }', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '7.4' ], 'templateWithLateResolvedType' => [ 'code' => ' $foo */ $foo = new Foo();' ], 'SKIPPED-extendedPropertyTypeParameterised' => [ 'code' => 'createMap(); $date = $map->get("test"); echo $date->format("Y"); } /** * @return Map */ abstract protected function createMap(): Map; }' ], 'looseEquality' => [ 'code' => 'level = $level; } /** * @psalm-return self */ public static function readUncommitted(): self { return new self(self::READ_UNCOMMITTED); } /** * @psalm-return T */ public function toString(): string { return $this->level; } /** * @psalm-template TResult * @psalm-param pure-callable(self::READ_UNCOMMITTED): TResult $readUncommitted * @psalm-return TResult */ public function resolve(callable $readUncommitted) { if ($this->level == self::READ_UNCOMMITTED) { return $readUncommitted($this->level); } throw new \LogicException("bad"); } }' ], 'narrowTemplateTypeWithInstanceof' => [ 'code' => 'entity = $qux; } } }' ], 'flippedParamsMethodInside' => [ 'code' => ' */ public abstract function getTraversable() : Traversable; /** * @param Foo $flipped * @return Traversable */ public function getFlippedTraversable(Foo $flipped): Traversable { return $flipped->getTraversable(); } }' ], 'flippedParamsMethodOutside' => [ 'code' => ' $flipped * @return Traversable */ function getFlippedTraversable(Foo $flipped): Traversable { return $flipped->getTraversable(); } /** * @template A * @template B */ abstract class Foo { /** @return Traversable */ public abstract function getTraversable() : Traversable; }' ], 'flippedParamsPropertyInside' => [ 'code' => ' */ public $traversable; /** * @param Foo $flipped * @return Traversable */ public function getFlippedTraversable(Foo $flipped): Traversable { return $flipped->traversable; } }' ], 'flippedParamsPropertyOutside' => [ 'code' => ' $flipped * @return Traversable */ function getFlippedTraversable(Foo $flipped): Traversable { return $flipped->traversable; } /** * @template A * @template B */ abstract class Foo { /** @var Traversable */ public $traversable; }' ], 'simpleTemplate' => [ 'code' => ' $f */ function takesFMixed(F $f) : void {} function sendsF(F $f) : void { takesFMixed($f); }' ], 'arrayCollectionMapInternal' => [ 'code' => ' */ private $elements; /** @psalm-param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } /** * @template TNewKey of array-key * @template TNew * @psalm-param array $elements * @psalm-return static */ protected static function createFrom(array $elements) { return new static($elements); } /** * @psalm-template U * @psalm-param Closure(T=):U $func * @psalm-return static */ public function map(Closure $func) { $new_elements = array_map($func, $this->elements); return self::createFrom($new_elements); } }' ], 'arrayCollectionMapExternal' => [ 'code' => ' */ private $elements; /** @psalm-param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } /** * @psalm-template U * @psalm-param Closure(T=):U $func * @psalm-return ArrayCollection */ public function map(Closure $func) { $new_elements = array_map($func, $this->elements); return Creator::createFrom($new_elements); } } class Creator { /** * @template TNewKey of array-key * @template TNew * @psalm-param array $elements * @psalm-return ArrayCollection */ public static function createFrom(array $elements) { return new ArrayCollection($elements); } }' ], 'templateWithClassConstants' => [ 'code' => 'level = $level; } } /** * @psalm-return Foo */ function getFooA(): Foo { return new Foo(Foo::A); }' ], 'callTemplatedMethodOnSameClass' => [ 'code' => 'foo($e); } }' ], 'templatedStaticUnion' => [ 'code' => 'v = $v; } /** * @return static */ public function foo(): A { if (rand(0, 1)) { return new static($this->v); } else { return new static($this->v); } } }' ], 'templatedTypeWithLimitGoesIntoTemplatedType' => [ 'code' => ' [ 'code' => 'value = $value; } /** @return T */ public function getValue() { return $this->value; } }' ], 'newWithoutInferredTemplate' => [ 'code' => ' [ '$f' => 'Foo' ] ], 'PHP80-weakmapIsGeneric' => [ 'code' => ' $wm */ function isCountable(WeakMap $wm): int { return count($wm); } /** * @param WeakMap $wm * @return array{Throwable,int} */ function isTraverable(WeakMap $wm): array { foreach ($wm as $k => $v) { return [$k, $v]; } throw new RuntimeException; } /** * @param WeakMap $wm * @return Traversable */ function hasAggregateIterator(WeakMap $wm): Traversable { return $wm->getIterator(); } /** * @param WeakMap $wm */ function readsLikeAnArray(WeakMap $wm): int { $ex = new Exception; if (isset($wm[$ex])) { return $wm[$ex]; } throw new RuntimeException; } /** * @param WeakMap $wm */ function writesLikeAnArray(WeakMap $wm): void { $ex = new Exception; $wm[$ex] = 42; } ', ], 'combineTwoTemplatedArrays' => [ 'code' => 'v : $else; } } $opt = new Option([1, 3]); $b = $opt->getOrElse([2, 4])[0];', 'assertions' => [ '$b===' => '1|2' ] ], 'generaliseTemplatedString' => [ 'code' => 'data = $data; } } /** @param Container $r */ function takesContainer(Container $r): void { $r->data = "David"; } $me = new Container("Matthew"); takesContainer($me); if ($me->data === "David") {}' ], 'generaliseTemplatedArray' => [ 'code' => 'data = $data; } } /** @param Container $r */ function takesContainer(Container $r): void { $r->data = ["name" => "David"]; } $me = new Container(["name" => "Matthew"]); takesContainer($me); if ($me->data["name"] === "David") {}' ], 'allowCovariantBoundsMismatchSameContainers' => [ 'code' => ' $c * @param Collection $d */ function bar(Collection $c, Collection $d): Dog|Cat { return foo($c, $d); } /** @template-covariant T of object */ interface Collection { /** @return T */ public function get(): object; } class Cat {} class Dog {} /** * @template T of object * @param Collection $c * @param Collection $d * @return T */ function foo(Collection $c, Collection $d): object { return rand(0, 1) ? $c->get() : $d->get(); }', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '8.0', ], 'allowCovariantBoundsMismatchDifferentContainers' => [ 'code' => ' $c * @param Collection2 $d */ function bar(Collection1 $c, Collection2 $d): Dog|Cat { return foo($c, $d); } /** @template-covariant T of object */ interface Collection1 { /** @return T */ public function get(): object; } /** @template-covariant T of object */ interface Collection2 { /** @return T */ public function get(): object; } class Cat {} class Dog {} /** * @template T of object * @param Collection1 $c * @param Collection2 $d * @return T */ function foo(Collection1 $c, Collection2 $d): object { return rand(0, 1) ? $c->get() : $d->get(); }', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '8.0', ], 'allowCovariantBoundsMismatchContainerAndObject' => [ 'code' => ' $d */ function bar(Dog $c, Collection $d): Dog|Cat { $animal = foo($c, $d); if ($animal instanceof Dog) {} if ($animal instanceof Cat) {} return $animal; } /** @template-covariant T of object */ interface Collection { /** @return T */ public function get(): object; } class Cat {} class Dog {} /** * @template T of object * @param T $c * @param Collection $d * @return T */ function foo(object $c, Collection $d): object { return rand(0, 1) ? $c : $d->get(); }', 'assertions' => [], 'ignored_issues' => [], 'php_version' => '8.0', ], 'allowCompatibleGenerics' => [ 'code' => ' $a * @param B $b */ function foo(A $a, B $b): void {} /** * @param A $a * @param B $b */ function bar(A $a, B $b): void { foo($a, $b); }' ], 'templateOnDocblockMethod' => [ 'code' => ' $container */ $container = new Container(); $container->set(new A()); foo($container->get()); ' ], 'templateOnDocblockMethodOnInterface' => [ 'code' => ' $container */ $container->set(new A()); foo($container->get()); ' ], 'refineTemplateTypeOfUnion' => [ 'code' => 't instanceof One || $this->t instanceof Two) {} } } final class One {} final class Two {} final class Three {}', ], 'refineTemplateTypeOfUnionMoreComplex' => [ 'code' => 't instanceof One && rand(0, 1)) {} if ($this->t instanceof Two) {} } } final class One {} final class Two {} final class Three {}', ], 'issue7825' => [ 'code' => ' */ private $pager; /** * @phpstan-param T $query * @phpstan-param PagerInterface $pager */ public function __construct( ProxyQueryInterface $query, PagerInterface $pager ) { $this->pager = $pager; $this->query = $query; } } interface FormBuilderInterface {} /** @template T of FieldDescriptionInterface */ class FieldDescriptionCollection {} interface FieldDescriptionInterface {} abstract class Test { /** @var Datagrid */ private Datagrid $datagrid; /** @var PagerInterface&MockObject */ private $pager; /** @var ProxyQueryInterface&Stub */ private $query; /** @var FieldDescriptionCollection */ private FieldDescriptionCollection $columns; private FormBuilderInterface $formBuilder; /** * @psalm-template RealInstanceType of object * @psalm-param class-string $originalClassName * @psalm-return MockObject&RealInstanceType */ abstract protected function createMock(string $originalClassName): MockObject; /** * @psalm-template RealInstanceType of object * @psalm-param class-string $originalClassName * @psalm-return Stub&RealInstanceType */ abstract protected function createStub(string $originalClassName): Stub; protected function setUp(): void { $this->query = $this->createStub(ProxyQueryInterface::class); $this->columns = new FieldDescriptionCollection(); /** @var PagerInterface&MockObject $pager */ $pager = $this->createMock(PagerInterface::class); $this->pager = $pager; $this->datagrid = new Datagrid($this->query, $pager); } }', ], 'complexTypes' => [ 'code' => 'v; } } /** * @template TTObject * * @extends Future> */ class FutureB extends Future { /** @param TTObject $data */ public function __construct($data) { parent::__construct(new ArrayObject([$data])); } } $a = new FutureB(123); $r = $a->get();', 'assertions' => [ '$a===' => 'FutureB<123>', '$r===' => 'ArrayObject' ] ] ]; } /** * @return iterable,php_version?:string}> */ public function providerInvalidCodeParse(): iterable { return [ 'restrictTemplateInputWithClassString' => [ 'code' => ' */ private $items; /** * @param T::class $type */ public function __construct(string $type) { if (!in_array($type, [A::class, B::class], true)) { throw new \InvalidArgumentException; } $this->type = $type; $this->items = []; } /** @param T $item */ public function add($item): void { $this->items[] = $item; } } class A {} class B {} $foo = new Foo(A::class); $foo->add(new B);', 'error_message' => 'InvalidArgument', ], 'restrictTemplateInputWithTClassBadInput' => [ 'code' => ' */ private $items; /** * @param T::class $type */ public function __construct(string $type) { if (!in_array($type, [A::class, B::class], true)) { throw new \InvalidArgumentException; } $this->type = $type; $this->items = []; } /** @param T $item */ public function add($item): void { $this->items[] = $item; } } class A {} class B {} $foo = new Foo(A::class); $foo->add(new B);', 'error_message' => 'InvalidArgument', ], 'templatedClosureProperty' => [ 'code' => ' 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:20:34 - Argument 1 of type expects string, callable(State):(T:AlmostFooMap as mixed)&Foo provided', ], 'templateWithNoReturn' => [ 'code' => ' 'InvalidReturnType', ], 'templateInvalidDocblockArgument' => [ 'code' => ' * @psalm-suppress InvalidReturnType */ function violate($p) {}', 'error_message' => 'InvalidTemplateParam', ], 'doublyLinkedListBadParam' => [ 'code' => ' */ $templated_list = new SplDoublyLinkedList(); $templated_list->add(5, []);', 'error_message' => 'InvalidArgument', ], 'copyScopedClassInNamespacedClass' => [ 'code' => ' 'ReservedWord', ], 'duplicateTemplateFunction' => [ 'code' => ' */ static function of($value): self { return new self($value); } /** * @param T $value */ private function __construct($value) { $this->value = $value; } }', 'error_message' => 'InvalidDocblock', ], 'preventDogCatSnafu' => [ 'code' => ' $list */ function addAnimal(Collection $list) : void { $list->add(new Cat()); } /** * @param Collection $list */ function takesDogList(Collection $list) : void { addAnimal($list); // this should be an error }', 'error_message' => 'InvalidArgument', ], 'templateEmptyParamCoercionChangeVariable' => [ 'code' => ' */ private $data; /** @psalm-param iterable $data */ public function __construct(iterable $data = []) { $this->data = $data; } } /** @psalm-param Collection $c */ function takesStringCollection(Collection $c): void {} /** @psalm-param Collection $c */ function takesIntCollection(Collection $c): void {} $collection = new Collection(); takesStringCollection($collection); takesIntCollection($collection);', 'error_message' => 'InvalidArgument', ], 'argumentExpectsFleshOutTIndexedAccess' => [ 'code' => 'data = $data; } /** * @template K as key-of * * @param K $property * * @return TData[K] */ public function __get(string $property) { // validation logic would go here return $this->data[$property]; } /** * @template K as key-of * * @param K $property * @param TData[K] $value */ public function __set(string $property, $value) { // data updating would go here $this->data[$property] = $value; } } /** @extends Row */ class CharacterRow extends Row {} $mario = new CharacterRow(["id" => 5, "name" => "Mario", "height" => 3.5]); $mario->ame = "Luigi";', 'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . "somefile.php:47:29 - Argument 1 of CharacterRow::__set expects 'height'|'id'|'name', 'ame' provided", ], 'specialiseTypeBeforeReturning' => [ 'code' => ' */ function returnFooBase() { $f = new Foo(new Derived()); takesFooDerived($f); return $f; } /** * @param Foo $foo */ function takesFooDerived($foo): void {}', 'error_message' => 'InvalidReturnStatement' ], 'possiblySpecialiseTypeBeforeReturning' => [ 'code' => ' */ function returnFooBase() { $f = new Foo(new Derived()); if (rand(0, 1)) { takesFooDerived($f); } return $f; } /** * @param Foo $foo */ function takesFooDerived($foo): void {}', 'error_message' => 'InvalidReturnStatement' ], 'preventUseWithMoreSpecificParamInt' => [ 'code' => ' $col */ function usesCollection(Collection $col): void { $col->add(456); }', 'error_message' => 'InvalidArgument' ], 'preventUseWithMoreSpecificParamEmptyArray' => [ 'code' => ' $col */ function usesCollection(Collection $col): void { $col->add([]); }', 'error_message' => 'InvalidArgument' ], 'preventTemplatedCorrectionBeingWrittenTo' => [ 'code' => ' */ private $data; /** @param array $data */ public function __construct(array $data) { $this->data = $data; } /** * @param TKey $key * @param TValue $value */ public function addItem($key, $value) : void { $this->data[$key] = $value; } } class Item {} class SubItem extends Item {} class OtherSubItem extends Item {} /** * @param ArrayCollection $i */ function takesCollectionOfItems(ArrayCollection $i): void { $i->addItem(10, new OtherSubItem); } $subitem_collection = new ArrayCollection([ new SubItem ]); takesCollectionOfItems($subitem_collection);', 'error_message' => 'InvalidArgument' ], 'noClassTemplatesInStaticMethods' => [ 'code' => ' 'UndefinedDocblockClass' ], 'newGenericBecomesPropertyTypeInvalidArg' => [ 'code' => ' */ public ArrayCollection $b_collection; public function __construct() { $this->b_collection = new ArrayCollection([]); $this->b_collection->add(5, new C()); } } /** * @psalm-template TKey * @psalm-template T */ class ArrayCollection { /** * An array containing the entries of this collection. * * @psalm-var array * @var array */ private $elements = []; /** * Initializes a new ArrayCollection. * * @param array $elements * * @psalm-param array $elements */ public function __construct(array $elements = []) { $this->elements = $elements; } /** * @param TKey $key * @param T $t */ public function add($key, $t) : void { $this->elements[$key] = $t; } }', 'error_message' => 'InvalidArgument' ], 'preventIteratorAggregateToIterableWithDifferentTypes' => [ 'code' => ' $foos */ function consume(iterable $foos): void {} /** @param IteratorAggregate $t */ function foo(IteratorAggregate $t) : void { consume($t); }', 'error_message' => 'InvalidArgument', ], 'preventPassingToBoundParam' => [ 'code' => 't = $t; } /** * @param T $t * @return T */ protected function makeNew($t) { return $t; } /** * @template U * @param U $u */ public function map($u) : void { $this->makeNew($u); } }', 'error_message' => 'InvalidArgument', ], 'bindRedirectedTemplate' => [ 'code' => ' * @param TIn2 $in * @return list */ public function __invoke(array $in) : array { return array_map( $this->c, $in ); } } $m = new Map(fn(int $num) => (string) $num); $m(["a"]);', 'error_message' => 'InvalidScalarArgument', 'ignored_issues' => [], 'php_version' => '8.0' ], 'bindClosureParamAccurately' => [ 'code' => ' */ public function map(Closure $func); } /** * @param Collection $c */ function f(Collection $c): void { $fn = function(int $_p): bool { return true; }; $c->map($fn); }', 'error_message' => 'InvalidScalarArgument', ], 'limitTemplateTypeWithSameName' => [ 'code' => ' $a */ function foo(A $a) : void { takesA($a); }', 'error_message' => 'InvalidArgument', ], 'limitTemplateTypeExtended' => [ 'code' => ' */ class AChild extends A {} function takesA(A $a) : void {} $child = new AChild(); takesA($child);', 'error_message' => 'InvalidArgument', ], 'noCrashTemplatedClosure' => [ 'code' => 'callback = $callback; } /** @param TCallback $callback */ public function setCallback(Closure $callback): void { $this->callback = $callback; } } $a = new A(function() { return "a";}); $a->setCallback(function() { return "b";});', 'error_message' => 'InvalidScalarArgument', ], 'preventBoundsMismatchDifferentContainers' => [ 'code' => ' $c * @param Collection2 $d */ function bar(Collection1 $c, Collection2 $d): void { foo($c, $d); } /** @template T of object */ interface Collection1 { /** @param T $item */ public function add(object $item): void; } /** @template T of object */ interface Collection2 { /** @param T $item */ public function add(object $item): void; /** @return T */ public function get(): object; } class Cat {} class Dog {} /** * @template T of object * @param Collection1 $c * @param Collection2 $d */ function foo(Collection1 $c, Collection2 $d): void { $c->add($d->get()); }', 'error_message' => 'InvalidArgument', ], 'preventBoundsMismatchSameContainers' => [ 'code' => ' $c * @param Collection $d */ function bar(Collection $c, Collection $d): void { foo($c, $d); } /** @template T of object */ interface Collection { /** @param T $item */ public function add(object $item): void; /** @return T */ public function get(): object; } class Cat {} class Dog {} /** * @template T of object * @param Collection $c * @param Collection $d */ function foo(Collection $c, Collection $d): void { $c->add($d->get()); }', 'error_message' => 'InvalidArgument', ], 'preventBoundsMismatchDifferentBoundLevels' => [ 'code' => ' $c */ function bar(Collection $c): void { foo($c, new Cat()); } /** @template T of object */ interface Collection { /** @param T $item */ public function add(object $item): void; } class Cat {} class Dog {} /** * @template T of object * @param Collection $c * @param T $d */ function foo(Collection $c, object $d): void { $c->add($d); }', 'error_message' => 'InvalidArgument', ], 'invalidTemplateArgumentOnDocblockMethod' => [ 'code' => ' $container */ $container = new Container(); $container->set(new B());', 'error_message' => 'InvalidArgument', ], 'refineTemplateTypeOfUnionAccurately' => [ 'code' => 't instanceof One || $this->t instanceof Two) { return $this->t; } throw new \Exception(); } } final class One {} final class Two {} final class Three {}', 'error_message' => 'InvalidReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:40 - The inferred type \'T:A as One|Two\' ', ], ]; } }