2019-06-25 20:13:00 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-06-25 20:13:00 +02:00
|
|
|
namespace Psalm\Tests\Template;
|
|
|
|
|
|
|
|
use Psalm\Tests\TestCase;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2019-06-25 20:13:00 +02:00
|
|
|
|
|
|
|
class FunctionClassStringTemplateTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2019-06-25 20:13:00 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2019-06-25 20:13:00 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'callStaticMethodOnTemplatedClassName' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T
|
2020-08-30 17:44:14 +02:00
|
|
|
* @param class-string<T> $class
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function foo(string $class, array $args) : void {
|
|
|
|
$class::bar($args);
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedMethodCall'],
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'returnTemplatedClassClassName' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
class I {
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param class-string<T> $class
|
|
|
|
* @return T|null
|
|
|
|
*/
|
|
|
|
public function loader(string $class) {
|
|
|
|
return $class::load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-25 20:13:00 +02:00
|
|
|
class Foo {
|
|
|
|
/** @return static */
|
|
|
|
public static function load() {
|
|
|
|
return new static();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FooChild extends Foo{}
|
|
|
|
|
|
|
|
$a = (new I)->loader(FooChild::class);',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'FooChild|null',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'upcastIterableToTraversable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T as iterable
|
2021-07-17 00:38:39 +02:00
|
|
|
* @param class-string<T> $class
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function foo(string $class) : void {
|
|
|
|
$a = new $class();
|
|
|
|
|
|
|
|
foreach ($a as $b) {}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'upcastGenericIterableToGenericTraversable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T as iterable<int>
|
|
|
|
* @param T::class $class
|
|
|
|
*/
|
|
|
|
function foo(string $class) : void {
|
|
|
|
$a = new $class();
|
|
|
|
|
|
|
|
foreach ($a as $b) {}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'understandTemplatedCalculationInOtherFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T as Exception
|
|
|
|
* @param T::class $type
|
|
|
|
* @return T
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-suppress UnsafeInstantiation
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function a(string $type): Exception {
|
|
|
|
return new $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as InvalidArgumentException
|
|
|
|
* @param T::class $type
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function b(string $type): InvalidArgumentException {
|
|
|
|
return a($type);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'objectReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
*
|
|
|
|
* @param class-string<T> $foo
|
|
|
|
*
|
|
|
|
* @return T
|
2020-08-06 16:18:55 +02:00
|
|
|
*
|
|
|
|
* @psalm-suppress MixedMethodCall
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function Foo(string $foo) : object {
|
|
|
|
return new $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo Foo(DateTime::class)->format("c");',
|
|
|
|
],
|
|
|
|
'templatedClassStringParamAsClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-25 20:13:00 +02:00
|
|
|
abstract class C {
|
|
|
|
public function foo() : void{}
|
|
|
|
}
|
|
|
|
|
|
|
|
class E {
|
|
|
|
/**
|
|
|
|
* @template T as C
|
|
|
|
* @param class-string<T> $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> $c_class
|
|
|
|
*/
|
|
|
|
function bar(string $c_class) : void {
|
|
|
|
$c = E::get($c_class);
|
|
|
|
$c->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-30 17:44:14 +02:00
|
|
|
* @psalm-suppress ArgumentTypeCoercion
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function bat(string $c_class) : void {
|
|
|
|
$c = E::get($c_class);
|
|
|
|
$c->foo();
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'templatedClassStringParamAsObject' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-25 20:13:00 +02:00
|
|
|
abstract class C {
|
|
|
|
public function foo() : void{}
|
|
|
|
}
|
|
|
|
|
|
|
|
class E {
|
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @param class-string<T> $c_class
|
|
|
|
*
|
|
|
|
* @psalm-return T
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-suppress MixedMethodCall
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
public static function get(string $c_class) {
|
|
|
|
return new $c_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-30 17:44:14 +02:00
|
|
|
* @psalm-suppress ArgumentTypeCoercion
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function bat(string $c_class) : void {
|
|
|
|
$c = E::get($c_class);
|
|
|
|
$c->bar = "bax";
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'templatedClassStringParamMoreSpecific' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-25 20:13:00 +02:00
|
|
|
abstract class C {
|
|
|
|
public function foo() : void{}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D extends C {
|
|
|
|
public function faa() : void{}
|
|
|
|
}
|
|
|
|
|
|
|
|
class E {
|
|
|
|
/**
|
|
|
|
* @template T as C
|
|
|
|
* @param class-string<T> $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> $d_class
|
|
|
|
*/
|
|
|
|
function moreSpecific(string $d_class) : void {
|
|
|
|
$d = E::get($d_class);
|
|
|
|
$d->foo();
|
|
|
|
$d->faa();
|
|
|
|
}',
|
|
|
|
],
|
2020-02-02 18:25:24 +01:00
|
|
|
'templateFilterArrayWithIntersection' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @template S as object
|
|
|
|
* @param array<T> $a
|
2021-07-17 00:38:39 +02:00
|
|
|
* @param interface-string<S> $type
|
2019-06-25 20:13:00 +02:00
|
|
|
* @return array<T&S>
|
|
|
|
*/
|
|
|
|
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<A> */
|
|
|
|
$x = [];
|
|
|
|
$y = filter($x, B::class);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2019-06-25 20:13:00 +02:00
|
|
|
'$y' => 'array<array-key, A&B>',
|
2019-07-05 22:24:00 +02:00
|
|
|
],
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
2020-02-02 18:25:24 +01:00
|
|
|
'templateFilterWithIntersection' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-02 18:25:24 +01:00
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @template S as object
|
|
|
|
* @param T $item
|
2021-07-17 00:38:39 +02:00
|
|
|
* @param interface-string<S> $type
|
2020-02-02 18:25:24 +01:00
|
|
|
* @return T&S
|
|
|
|
*/
|
2022-01-20 23:47:41 +01:00
|
|
|
function filter($item, string $type) {
|
2020-02-02 18:25:24 +01:00
|
|
|
if (is_a($item, $type)) {
|
|
|
|
return $item;
|
|
|
|
};
|
|
|
|
|
|
|
|
throw new \UnexpectedValueException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
|
|
|
|
/** @var A */
|
|
|
|
$x = null;
|
|
|
|
|
|
|
|
$y = filter($x, B::class);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-02-02 18:25:24 +01:00
|
|
|
'$y' => 'A&B',
|
|
|
|
],
|
|
|
|
],
|
2019-06-25 20:13:00 +02:00
|
|
|
'unionTOrClassStringTPassedClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @psalm-template T of object
|
|
|
|
* @psalm-param T|class-string<T> $someType
|
|
|
|
* @psalm-return T
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-suppress MixedMethodCall
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function getObject($someType) {
|
|
|
|
if (is_object($someType)) {
|
|
|
|
return $someType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $someType();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
function sayHello() : string {
|
|
|
|
return "hi";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
getObject(C::class)->sayHello();',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'unionTOrClassStringTPassedObject' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @psalm-template T of object
|
|
|
|
* @psalm-param T|class-string<T> $someType
|
|
|
|
* @psalm-return T
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-suppress MixedMethodCall
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function getObject($someType) {
|
|
|
|
if (is_object($someType)) {
|
|
|
|
return $someType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $someType();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
function sayHello() : string {
|
|
|
|
return "hi";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
getObject(new C())->sayHello();',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'dontModifyByRefTemplatedArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of object
|
|
|
|
* @param class-string<T> $className
|
|
|
|
* @param array<T> $map
|
|
|
|
* @param-out array<T> $map
|
|
|
|
* @param int $id
|
|
|
|
* @return T
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-suppress MixedMethodCall
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function get(string $className, array &$map, int $id) {
|
|
|
|
if(!array_key_exists($id, $map)) {
|
|
|
|
$map[$id] = new $className();
|
|
|
|
}
|
|
|
|
return $map[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<A> $mapA
|
|
|
|
*/
|
|
|
|
function getA(int $id, array $mapA): A {
|
|
|
|
return get(A::class, $mapA, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<B> $mapB
|
|
|
|
*/
|
|
|
|
function getB(int $id, array $mapB): B {
|
|
|
|
return get(B::class, $mapB, $id);
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'unionClassStringTWithTReturnsObjectWhenCoerced' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
* @param T|class-string<T> $s
|
|
|
|
* @return T
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-suppress MixedMethodCall
|
2019-06-25 20:13:00 +02:00
|
|
|
*/
|
|
|
|
function bar($s) {
|
|
|
|
if (is_object($s)) {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $s();
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(string $s) : object {
|
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */
|
|
|
|
return bar($s);
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
|
|
|
|
'allowTemplatedIntersectionFirst' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
class MockObject
|
|
|
|
{
|
|
|
|
public function checkExpectations() : void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template RequestedType
|
|
|
|
* @psalm-param class-string<RequestedType> $className
|
|
|
|
* @psalm-return RequestedType&MockObject
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
* @psalm-suppress MixedReturnStatement
|
|
|
|
*/
|
2023-06-07 08:12:31 +02:00
|
|
|
function mockHelper(string $className)
|
2019-06-25 20:13:00 +02:00
|
|
|
{
|
|
|
|
eval(\'"there be dragons"\');
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template UnknownType
|
|
|
|
* @psalm-param class-string<UnknownType> $className
|
|
|
|
*/
|
|
|
|
function useMockTemplated(string $className) : void
|
|
|
|
{
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper($className)->checkExpectations();
|
2019-06-25 20:13:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper(A::class)->foo();',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'allowTemplatedIntersectionFirstTemplatedMock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
class MockObject
|
|
|
|
{
|
|
|
|
public function checkExpectations() : void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template RequestedType
|
|
|
|
* @psalm-param class-string<RequestedType> $className
|
|
|
|
* @psalm-return RequestedType&MockObject
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
* @psalm-suppress MixedReturnStatement
|
|
|
|
*/
|
2023-06-07 08:12:31 +02:00
|
|
|
function mockHelper(string $className)
|
2019-06-25 20:13:00 +02:00
|
|
|
{
|
|
|
|
eval(\'"there be dragons"\');
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template UnknownType
|
|
|
|
* @psalm-param class-string<UnknownType> $className
|
|
|
|
*/
|
|
|
|
function useMockTemplated(string $className) : void
|
|
|
|
{
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper($className)->checkExpectations();
|
2019-06-25 20:13:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper(A::class)->foo();',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
|
|
|
'allowTemplatedIntersectionSecond' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
class MockObject
|
|
|
|
{
|
|
|
|
public function checkExpectations() : void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template RequestedType
|
|
|
|
* @psalm-param class-string<RequestedType> $className
|
|
|
|
* @psalm-return MockObject&RequestedType
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
|
|
|
* @psalm-suppress MixedReturnStatement
|
|
|
|
*/
|
2023-06-07 08:12:31 +02:00
|
|
|
function mockHelper(string $className)
|
2019-06-25 20:13:00 +02:00
|
|
|
{
|
|
|
|
eval(\'"there be dragons"\');
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-param class-string $className
|
|
|
|
*/
|
|
|
|
function useMock(string $className) : void {
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper($className)->checkExpectations();
|
2019-06-25 20:13:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-template UnknownType
|
|
|
|
* @psalm-param class-string<UnknownType> $className
|
|
|
|
*/
|
|
|
|
function useMockTemplated(string $className) : void
|
|
|
|
{
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper($className)->checkExpectations();
|
2019-06-25 20:13:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:12:31 +02:00
|
|
|
mockHelper(A::class)->foo();',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
2019-06-25 20:15:58 +02:00
|
|
|
'returnClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:15:58 +02:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T::class $s
|
|
|
|
* @return T::class
|
|
|
|
*/
|
|
|
|
function foo(string $s) : string {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A::class $s
|
|
|
|
*/
|
|
|
|
function bar(string $s) : void {
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
bar(foo(A::class));',
|
|
|
|
],
|
2019-06-27 17:44:35 +02:00
|
|
|
'templateAsUnionClassStringPassingValidClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-27 17:44:35 +02:00
|
|
|
class A {}
|
2020-08-06 16:18:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-27 17:44:35 +02:00
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T1 as A
|
|
|
|
* @template T2 as B
|
|
|
|
* @param class-string<T1>|class-string<T2> $type
|
|
|
|
* @return T1|T2
|
|
|
|
*/
|
|
|
|
function f(string $type) {
|
|
|
|
return new $type();
|
|
|
|
}
|
|
|
|
|
|
|
|
f(A::class);
|
|
|
|
f(B::class);',
|
2019-07-04 22:38:31 +02:00
|
|
|
],
|
2021-10-20 23:14:35 +02:00
|
|
|
'compareToExactClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-04 22:38:31 +02:00
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
*/
|
|
|
|
class Type
|
|
|
|
{
|
|
|
|
/** @var class-string<T> */
|
|
|
|
private $typeName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<T> $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();
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-07-04 22:38:31 +02:00
|
|
|
],
|
2019-07-06 04:57:38 +02:00
|
|
|
'compareGetClassTypeString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-06 04:57:38 +02:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $typeName
|
|
|
|
* @param mixed $value
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function cast($value, string $typeName) {
|
|
|
|
if (is_object($value) && get_class($value) === $typeName) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException();
|
|
|
|
}',
|
|
|
|
],
|
2019-07-25 14:33:27 +02:00
|
|
|
'instanceofTemplatedClassStringOnMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-25 04:13:51 +02:00
|
|
|
interface Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param class-string<T> $fooClass
|
|
|
|
* @param mixed $foo
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function get($fooClass, $foo) {
|
|
|
|
if ($foo instanceof $fooClass) {
|
2019-07-25 14:33:27 +02:00
|
|
|
return $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception();
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'instanceofTemplatedClassStringOnObjectType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-25 14:33:27 +02:00
|
|
|
interface Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param class-string<T> $fooClass
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function get($fooClass, Foo $foo) {
|
|
|
|
if ($foo instanceof $fooClass) {
|
2019-07-25 04:13:51 +02:00
|
|
|
return $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception();
|
|
|
|
}',
|
|
|
|
],
|
2020-02-01 21:02:39 +01:00
|
|
|
'templateFromDifferentClassStrings' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2020-02-01 21:02:39 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of A
|
|
|
|
* @param class-string<T> $a1
|
|
|
|
* @param class-string<T> $a2
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function test(string $a1, string $a2) {
|
|
|
|
if (rand(0, 1)) return new $a1();
|
|
|
|
|
|
|
|
return new $a2();
|
|
|
|
}
|
|
|
|
|
|
|
|
$b_or_c = test(B::class, C::class);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-02-01 21:02:39 +01:00
|
|
|
'$b_or_c' => 'B|C',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2020-02-01 21:02:39 +01:00
|
|
|
],
|
2020-02-14 17:18:47 +01:00
|
|
|
'allowComparisonWithoutCrash' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-14 17:18:47 +01:00
|
|
|
/**
|
|
|
|
* @template T as object
|
|
|
|
*
|
|
|
|
* @param T::class $e
|
|
|
|
* @param T::class $expected
|
|
|
|
*/
|
|
|
|
function bar(string $e, string $expected) : void {
|
|
|
|
if ($e !== $expected) {}
|
|
|
|
}',
|
|
|
|
],
|
2020-03-24 17:15:56 +01:00
|
|
|
'refineByArrayFilterIntersection' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 17:15:56 +01:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param array<Bar> $bars
|
|
|
|
* @psalm-param class-string<T> $class
|
|
|
|
* @return array<T&Bar>
|
|
|
|
*/
|
|
|
|
function getBarsThatAreInstancesOf(array $bars, string $class): array
|
|
|
|
{
|
|
|
|
return \array_filter(
|
|
|
|
$bars,
|
|
|
|
function (Bar $bar) use ($class): bool {
|
|
|
|
return $bar instanceof $class;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
interface Bar {}',
|
2020-03-24 17:15:56 +01:00
|
|
|
],
|
2022-06-08 02:08:55 +02:00
|
|
|
'classStringSatisfiesTemplateWithConstraint' => [
|
2022-07-07 07:21:35 +02:00
|
|
|
'code' => '<?php
|
2022-06-08 02:08:55 +02:00
|
|
|
/** @template T of string */
|
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
/** @param class-string<Foo> $_fooClass */
|
|
|
|
function bar(string $_fooClass): void {}
|
|
|
|
|
|
|
|
bar(Foo::class);
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'classStringWithGenericChildSatisfiesGenericParentWithDifferentConstraint' => [
|
2022-07-07 07:21:35 +02:00
|
|
|
'code' => '<?php
|
2022-06-08 02:08:55 +02:00
|
|
|
/** @template T of string */
|
|
|
|
class Foo {}
|
|
|
|
/**
|
|
|
|
* @template T of non-empty-string
|
|
|
|
* @extends Foo<T>
|
|
|
|
*/
|
|
|
|
class Bar extends Foo {}
|
|
|
|
|
|
|
|
/** @param class-string<Foo> $_fooClass */
|
|
|
|
function bar(string $_fooClass): void {}
|
|
|
|
|
|
|
|
bar(Bar::class);
|
|
|
|
',
|
|
|
|
],
|
2022-10-07 13:18:57 +02:00
|
|
|
'classStringNestedTemplate' => [
|
|
|
|
'code' => '<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as MyMapper
|
|
|
|
*/
|
|
|
|
abstract class MyObject {}
|
|
|
|
/**
|
|
|
|
* @template T as MyObject
|
|
|
|
*/
|
|
|
|
abstract class MyMapper {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends MyObject<AMapper>
|
|
|
|
*/
|
|
|
|
final class AObject extends MyObject {}
|
|
|
|
/**
|
|
|
|
* @extends MyMapper<AObject>
|
|
|
|
*/
|
|
|
|
final class AMapper extends MyMapper {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends MyObject<BMapper>
|
|
|
|
*/
|
|
|
|
final class BObject extends MyObject {}
|
|
|
|
/**
|
|
|
|
* @extends MyMapper<BObject>
|
|
|
|
*/
|
|
|
|
final class BMapper extends MyMapper {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get source, asserting class type
|
|
|
|
*
|
|
|
|
* @template T as MyObject
|
|
|
|
*
|
|
|
|
* @param class-string<T> $class
|
|
|
|
* @param AObject|BObject $source
|
|
|
|
*
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
function getSourceAssertType(string $class, MyObject $source): MyObject {
|
|
|
|
if (!$source instanceof $class) {
|
|
|
|
throw new RuntimeException("Invalid class!");
|
|
|
|
}
|
|
|
|
return $source;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
|
|
|
],
|
2019-06-25 20:13:00 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2019-06-25 20:13:00 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'copyScopedClassInFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
/**
|
|
|
|
* @template Throwable as DOMNode
|
|
|
|
*
|
|
|
|
* @param class-string<Throwable> $foo
|
|
|
|
*/
|
|
|
|
function Foo(string $foo) : string {
|
|
|
|
return $foo;
|
|
|
|
}',
|
|
|
|
'error_message' => 'ReservedWord',
|
|
|
|
],
|
|
|
|
'copyScopedClassInNamespacedFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class Bar {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Bar as DOMNode
|
|
|
|
*
|
|
|
|
* @param class-string<Bar> $foo
|
|
|
|
*/
|
|
|
|
function Foo(string $foo) : string {
|
|
|
|
return $foo;
|
|
|
|
}',
|
|
|
|
'error_message' => 'ReservedWord',
|
|
|
|
],
|
|
|
|
'constrainTemplateTypeWhenClassStringUsed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:13:00 +02:00
|
|
|
class GenericObjectFactory {
|
|
|
|
/**
|
|
|
|
* @psalm-template T
|
|
|
|
* @psalm-param class-string<T> $type
|
|
|
|
* @psalm-return T
|
|
|
|
*/
|
|
|
|
public function getObject(string $type)
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2019-06-25 20:13:00 +02:00
|
|
|
],
|
2019-06-25 20:15:58 +02:00
|
|
|
'forbidLossOfInformationWhenCoercing' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-25 20:15:58 +02:00
|
|
|
/**
|
|
|
|
* @template T as iterable<int>
|
|
|
|
* @param T::class $class
|
|
|
|
*/
|
|
|
|
function foo(string $class) : void {}
|
|
|
|
|
|
|
|
function bar(Traversable $t) : void {
|
|
|
|
foo(get_class($t));
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedArgumentTypeCoercion',
|
|
|
|
],
|
2019-06-27 17:44:35 +02:00
|
|
|
'templateAsUnionClassStringPassingInvalidClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-27 17:44:35 +02:00
|
|
|
class A {}
|
2020-08-06 16:18:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-06-27 17:44:35 +02:00
|
|
|
class B {}
|
2020-08-06 16:18:55 +02:00
|
|
|
|
2019-06-27 17:44:35 +02:00
|
|
|
class C {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T1 as A
|
|
|
|
* @template T2 as B
|
|
|
|
* @param class-string<T1>|class-string<T2> $type
|
|
|
|
* @return T1|T2
|
|
|
|
*/
|
|
|
|
function f(string $type) {
|
|
|
|
return new $type();
|
|
|
|
}
|
|
|
|
|
|
|
|
f(C::class);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2020-08-25 23:25:05 +02:00
|
|
|
'bindToClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-25 23:25:05 +02:00
|
|
|
/**
|
|
|
|
* @template TClass as object
|
|
|
|
*
|
|
|
|
* @param class-string<TClass> $className
|
|
|
|
* @param TClass $realInstance
|
|
|
|
*
|
|
|
|
* @return Closure(TClass) : void
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
*/
|
|
|
|
function createInitializer(string $className, object $realInstance) : Closure {}
|
|
|
|
|
|
|
|
function foo(object $realInstance) : void {
|
|
|
|
$className = get_class($realInstance);
|
|
|
|
/** @psalm-trace $i */
|
|
|
|
$i = createInitializer($className, $realInstance);
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'Closure(object):void',
|
2020-08-25 23:25:05 +02:00
|
|
|
],
|
2021-01-11 06:28:44 +01:00
|
|
|
'preventClassStringInPlaceOfTemplatedClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-11 06:28:44 +01:00
|
|
|
class ImageFile {}
|
|
|
|
class MusicFile {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of object
|
|
|
|
*/
|
|
|
|
interface FileManager {
|
|
|
|
/**
|
|
|
|
* @param class-string<T> $instance
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public function create(string $instance) : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param FileManager<ImageFile> $m */
|
|
|
|
function foo(FileManager $m) : void {
|
|
|
|
$m->create(MusicFile::class);
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2021-01-11 06:28:44 +01:00
|
|
|
],
|
2019-06-25 20:13:00 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|