2019-12-27 18:49:28 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 14:16:41 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
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
|
|
|
|
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);
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-05-15 01:44:03 +02:00
|
|
|
],
|
2023-02-26 17:13:16 +01:00
|
|
|
'assignClassStringMapInConstruct' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class A {
|
|
|
|
/** @var class-string-map<T,T> */
|
|
|
|
private array $map;
|
|
|
|
/** @param class-string-map<T,T> $map */
|
|
|
|
public function __construct(array $map) {
|
|
|
|
$this->map = $map;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'assignClassStringMapInMethod' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class A {
|
|
|
|
/** @var class-string-map<T,T> */
|
|
|
|
private array $map = [];
|
|
|
|
/** @param class-string-map<T,T> $map */
|
|
|
|
public function set(array $map): void {
|
|
|
|
$this->map = $map;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2023-02-26 15:07:32 +01:00
|
|
|
'simpleSetter' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Container {
|
|
|
|
/** @var class-string-map<T, T> */
|
|
|
|
public array $map = [];
|
|
|
|
/**
|
|
|
|
* @template U of object
|
|
|
|
* @param class-string<U> $key
|
|
|
|
* @param U $obj
|
|
|
|
*/
|
|
|
|
public function set(string $key, object $obj): void {
|
|
|
|
$this->map[$key] = $obj;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
,
|
|
|
|
],
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2019-12-27 18:49:28 +01:00
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2019-12-27 18:49:28 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|