2019-12-27 18:49:28 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests\Template;
|
|
|
|
|
|
|
|
use Psalm\Tests\TestCase;
|
|
|
|
use Psalm\Tests\Traits;
|
|
|
|
|
|
|
|
class ClassStringMapTest extends TestCase
|
|
|
|
{
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2019-12-27 18:49:28 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'basicClassStringMap' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2019-12-27 18:49:28 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'assignInvalidClass' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|