,error_levels?:string[]}> */ public function providerValidCodeParse() { return [ 'callStaticMethodOnTemplatedClassName' => [ ' [], 'error_levels' => ['MixedMethodCall'], ], 'returnTemplatedClassClassName' => [ ' $class * @return T|null */ public function loader(string $class) { return $class::load(); } } class Foo { /** @return static */ public static function load() { return new static(); } } class FooChild extends Foo{} $a = (new I)->loader(FooChild::class);', 'assertions' => [ '$a' => 'FooChild|null', ], ], 'upcastIterableToTraversable' => [ ' [], 'error_levels' => ['MixedAssignment'], ], 'upcastGenericIterableToGenericTraversable' => [ ' * @param T::class $class */ function foo(string $class) : void { $a = new $class(); foreach ($a as $b) {} }', 'assertions' => [], 'error_levels' => [], ], 'understandTemplatedCalculationInOtherFunction' => [ ' [ ' $foo * * @return T */ function Foo(string $foo) : object { return new $foo; } echo Foo(DateTime::class)->format("c");', ], 'templatedClassStringParamAsClass' => [ ' $c_class * * @return C * @psalm-return T */ public static function get(string $c_class) : C { $c = new $c_class; $c->foo(); return $c; } } /** * @param class-string $c_class */ function bar(string $c_class) : void { $c = E::get($c_class); $c->foo(); } /** * @psalm-suppress TypeCoercion */ function bat(string $c_class) : void { $c = E::get($c_class); $c->foo(); }', ], 'templatedClassStringParamAsObject' => [ ' $c_class * * @psalm-return T */ public static function get(string $c_class) { return new $c_class; } } /** * @psalm-suppress TypeCoercion */ function bat(string $c_class) : void { $c = E::get($c_class); $c->bar = "bax"; }', ], 'templatedClassStringParamMoreSpecific' => [ ' $c_class * * @return C * @psalm-return T */ public static function get(string $c_class) : C { $c = new $c_class; $c->foo(); return $c; } } /** * @param class-string $d_class */ function moreSpecific(string $d_class) : void { $d = E::get($d_class); $d->foo(); $d->faa(); }', ], 'templateArrayIntersection' => [ ' $a * @param class-string $type * @return array */ function filter(array $a, string $type): array { $result = []; foreach ($a as $item) { if (is_a($item, $type)) { $result[] = $item; } } return $result; } interface A {} interface B {} /** @var array */ $x = []; $y = filter($x, B::class);', [ '$y' => 'array', ], ], 'unionTOrClassStringTPassedClassString' => [ ' $someType * @psalm-return T */ function getObject($someType) { if (is_object($someType)) { return $someType; } return new $someType(); } class C { function sayHello() : string { return "hi"; } } getObject(C::class)->sayHello();', ], 'unionTOrClassStringTPassedObject' => [ ' $someType * @psalm-return T */ function getObject($someType) { if (is_object($someType)) { return $someType; } return new $someType(); } class C { function sayHello() : string { return "hi"; } } getObject(new C())->sayHello();', ], 'dontModifyByRefTemplatedArray' => [ ' $className * @param array $map * @param-out array $map * @param int $id * @return T */ function get(string $className, array &$map, int $id) { if(!array_key_exists($id, $map)) { $map[$id] = new $className(); } return $map[$id]; } /** * @param array $mapA */ function getA(int $id, array $mapA): A { return get(A::class, $mapA, $id); } /** * @param array $mapB */ function getB(int $id, array $mapB): B { return get(B::class, $mapB, $id); }', ], 'unionClassStringTWithTReturnsObjectWhenCoerced' => [ ' $s * @return T */ function bar($s) { if (is_object($s)) { return $s; } return new $s(); } function foo(string $s) : object { /** @psalm-suppress ArgumentTypeCoercion */ return bar($s); }', ], 'allowTemplatedIntersectionFirst' => [ ' $className * @psalm-return RequestedType&MockObject * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ function mock(string $className) { eval(\'"there be dragons"\'); return $instance; } class A { public function foo() : void {} } /** * @psalm-template UnknownType * @psalm-param class-string $className */ function useMockTemplated(string $className) : void { mock($className)->checkExpectations(); } mock(A::class)->foo();', ], 'allowTemplatedIntersectionFirstTemplatedMock' => [ ' $className * @psalm-return RequestedType&MockObject * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ function mock(string $className) { eval(\'"there be dragons"\'); return $instance; } class A { public function foo() : void {} } /** * @psalm-template UnknownType * @psalm-param class-string $className */ function useMockTemplated(string $className) : void { mock($className)->checkExpectations(); } mock(A::class)->foo();', ], 'allowTemplatedIntersectionSecond' => [ ' $className * @psalm-return MockObject&RequestedType * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ function mock(string $className) { eval(\'"there be dragons"\'); return $instance; } class A { public function foo() : void {} } /** * @psalm-param class-string $className */ function useMock(string $className) : void { mock($className)->checkExpectations(); } /** * @psalm-template UnknownType * @psalm-param class-string $className */ function useMockTemplated(string $className) : void { mock($className)->checkExpectations(); } mock(A::class)->foo();', ], 'returnClassString' => [ ' [ '|class-string $type * @return T1|T2 */ function f(string $type) { return new $type(); } f(A::class); f(B::class);', ], 'SKIPPED-compareToExactClassString' => [ ' */ private $typeName; /** * @param class-string $typeName */ public function __construct(string $typeName) { $this->typeName = $typeName; } /** * @param mixed $value * @return T */ public function cast($value) { if (is_object($value) && get_class($value) === $this->typeName) { return $value; } throw new RuntimeException(); } }', ], 'compareGetClassTypeString' => [ ' $typeName * @param mixed $value * @return T */ function cast($value, string $typeName) { if (is_object($value) && get_class($value) === $typeName) { return $value; } throw new RuntimeException(); }', ], 'instanceofTemplatedClassStringOnMixed' => [ ' $fooClass * @param mixed $foo * @return T */ function get($fooClass, $foo) { if ($foo instanceof $fooClass) { return $foo; } throw new \Exception(); }', ], 'instanceofTemplatedClassStringOnObjectType' => [ ' $fooClass * @return T */ function get($fooClass, Foo $foo) { if ($foo instanceof $fooClass) { return $foo; } throw new \Exception(); }', ], ]; } /** * @return iterable */ public function providerInvalidCodeParse() { return [ 'copyScopedClassInFunction' => [ ' $foo */ function Foo(string $foo) : string { return $foo; }', 'error_message' => 'ReservedWord', ], 'copyScopedClassInNamespacedFunction' => [ ' $foo */ function Foo(string $foo) : string { return $foo; }', 'error_message' => 'ReservedWord', ], 'constrainTemplateTypeWhenClassStringUsed' => [ ' $type * @psalm-return T */ public function getObject(string $type) { return 3; } }', 'error_message' => 'InvalidReturnStatement', ], 'forbidLossOfInformationWhenCoercing' => [ ' * @param T::class $class */ function foo(string $class) : void {} function bar(Traversable $t) : void { foo(get_class($t)); }', 'error_message' => 'MixedArgumentTypeCoercion', ], 'templateAsUnionClassStringPassingInvalidClass' => [ '|class-string $type * @return T1|T2 */ function f(string $type) { return new $type(); } f(C::class);', 'error_message' => 'InvalidArgument', ], ]; } }