2019-12-27 18:49:28 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-12-27 18:49:28 +01: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-12-27 18:49:28 +01:00
|
|
|
|
|
|
|
class ClassStringMapTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
2019-12-27 18:49:28 +01:00
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:array<string>}>
|
2019-12-27 18:49:28 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2019-12-27 18:49:28 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'basicClassStringMap' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-27 18:49:28 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-12-27 18:49:28 +01:00
|
|
|
class Foo {}
|
2020-08-06 16:18:55 +02:00
|
|
|
|
2019-12-27 18:49:28 +01:00
|
|
|
class A {
|
|
|
|
/** @var class-string-map<T as Foo, T> */
|
|
|
|
public static array $map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T as Foo
|
|
|
|
* @param class-string<T> $class
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
public function get(string $class) : Foo {
|
|
|
|
if (isset(self::$map[$class])) {
|
|
|
|
return self::$map[$class];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$map[$class] = new $class();
|
|
|
|
return self::$map[$class];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'basicClassStringMapDifferentTemplateName' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-27 18:49:28 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
2020-08-06 16:18:55 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2019-12-27 18:49:28 +01:00
|
|
|
class Foo {}
|
2020-08-06 16:18:55 +02:00
|
|
|
|
2019-12-27 18:49:28 +01:00
|
|
|
class A {
|
|
|
|
/** @var class-string-map<T as Foo, T> */
|
|
|
|
public static array $map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template U as Foo
|
|
|
|
* @param class-string<U> $class
|
|
|
|
* @return U
|
|
|
|
*/
|
|
|
|
public function get(string $class) : Foo {
|
|
|
|
if (isset(self::$map[$class])) {
|
|
|
|
return self::$map[$class];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$map[$class] = new $class();
|
|
|
|
return self::$map[$class];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2021-05-15 01:44:03 +02:00
|
|
|
'noCrashWithSplatMap' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-15 01:44:03 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
/** @param array<array-key, mixed> $args */
|
|
|
|
function takesVariadic(...$args): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param class-string-map<A, A> $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
takesVariadic(...$arr);
|
|
|
|
}'
|
|
|
|
],
|
2019-12-27 18:49:28 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:array<string>,php_version?:string}>
|
2019-12-27 18:49:28 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2019-12-27 18:49:28 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'assignInvalidClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-27 18:49:28 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var class-string-map<T, T> */
|
|
|
|
public static array $map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $class
|
|
|
|
*/
|
|
|
|
public function get(string $class) : void {
|
|
|
|
self::$map[$class] = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue'
|
|
|
|
],
|
|
|
|
'assignInvalidClassDifferentTemplateName' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-27 18:49:28 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var class-string-map<T, T> */
|
|
|
|
public static array $map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template U
|
|
|
|
* @param class-string<U> $class
|
|
|
|
*/
|
|
|
|
public function get(string $class) : void {
|
|
|
|
self::$map[$class] = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|