2021-02-26 22:05:46 +01:00
|
|
|
<?php
|
|
|
|
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;
|
2021-02-26 22:05:46 +01:00
|
|
|
|
2021-03-28 15:52:23 +02:00
|
|
|
class NestedTemplateTest extends TestCase
|
2021-02-26 22:05:46 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2021-02-26 22:05:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
|
|
*/
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'nestedTemplateExtends' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
interface IBaseViewData {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TViewData
|
|
|
|
*/
|
|
|
|
class BaseModel {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TViewData as IBaseViewData
|
|
|
|
* @template TModel as BaseModel<TViewData>
|
|
|
|
*/
|
|
|
|
abstract class BaseRepository {}
|
|
|
|
|
|
|
|
class StudentViewData implements IBaseViewData {}
|
|
|
|
class TeacherViewData implements IBaseViewData {}
|
|
|
|
|
|
|
|
/** @extends BaseModel<StudentViewData> */
|
|
|
|
class StudentModel extends BaseModel {}
|
|
|
|
|
|
|
|
/** @extends BaseModel<TeacherViewData> */
|
|
|
|
class TeacherModel extends BaseModel {}
|
|
|
|
|
|
|
|
/** @extends BaseRepository<StudentViewData, StudentModel> */
|
|
|
|
class StudentRepository extends BaseRepository {}
|
|
|
|
|
|
|
|
/** @extends BaseRepository<TeacherViewData, TeacherModel> */
|
|
|
|
class TeacherRepository extends BaseRepository {}'
|
|
|
|
],
|
2021-02-27 06:22:31 +01:00
|
|
|
'unwrapIndirectGenericTemplated' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TInner
|
|
|
|
*/
|
|
|
|
interface Wrapper {
|
|
|
|
/** @return TInner */
|
|
|
|
public function unwrap() : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TInner2
|
|
|
|
* @template TWrapper2 of Wrapper<TInner2>
|
|
|
|
* @param TWrapper2 $wrapper
|
|
|
|
* @return TInner2
|
|
|
|
*/
|
|
|
|
function indirectUnwrap(Wrapper $wrapper) : object {
|
|
|
|
return unwrapGeneric($wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TInner1
|
|
|
|
* @template TWrapper1 of Wrapper<TInner1>
|
|
|
|
* @param TWrapper1 $wrapper
|
|
|
|
* @return TInner1
|
|
|
|
*/
|
|
|
|
function unwrapGeneric(Wrapper $wrapper) {
|
|
|
|
return $wrapper->unwrap();
|
|
|
|
}'
|
|
|
|
],
|
2021-02-27 07:00:05 +01:00
|
|
|
'unwrapFromTemplatedClassString' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TInner
|
|
|
|
*/
|
|
|
|
interface Wrapper {
|
|
|
|
/** @return TInner */
|
|
|
|
public function unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-17 00:38:39 +02:00
|
|
|
* @implements Wrapper<string>
|
2021-02-27 07:00:05 +01:00
|
|
|
*/
|
2021-07-17 00:38:39 +02:00
|
|
|
class StringWrapper implements Wrapper {
|
|
|
|
public function unwrap() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 07:00:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TInner
|
|
|
|
* @template TWrapper of Wrapper<TInner>
|
|
|
|
*
|
|
|
|
* @param class-string<TWrapper> $class
|
|
|
|
* @return TInner
|
|
|
|
*/
|
|
|
|
function load(string $class) {
|
|
|
|
$package = new $class();
|
|
|
|
return $package->unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = load(StringWrapper::class);'
|
|
|
|
],
|
2021-03-28 15:52:23 +02:00
|
|
|
'unwrapNestedTemplateWithReset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
* @template TArray of non-empty-array<TValue>
|
|
|
|
* @param TArray $arr
|
|
|
|
* @return TValue
|
|
|
|
*/
|
|
|
|
function toList(array $arr): array {
|
|
|
|
return reset($arr);
|
|
|
|
}'
|
|
|
|
],
|
2021-02-26 22:05:46 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-20 02:44:44 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
|
2021-02-26 22:05:46 +01:00
|
|
|
*/
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'nestedTemplateExtendsInvalid' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
interface IBaseViewData {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TViewData
|
|
|
|
*/
|
|
|
|
class BaseModel {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TViewData as IBaseViewData
|
|
|
|
* @template TModel as BaseModel<TViewData>
|
|
|
|
*/
|
|
|
|
abstract class BaseRepository {}
|
|
|
|
|
|
|
|
class StudentViewData implements IBaseViewData {}
|
|
|
|
class TeacherViewData implements IBaseViewData {}
|
|
|
|
|
|
|
|
/** @extends BaseModel<StudentViewData> */
|
|
|
|
class StudentModel extends BaseModel {}
|
|
|
|
|
|
|
|
/** @extends BaseModel<TeacherViewData> */
|
|
|
|
class TeacherModel extends BaseModel {}
|
|
|
|
|
|
|
|
/** @extends BaseRepository<StudentViewData, TeacherModel> */
|
|
|
|
class StudentRepository extends BaseRepository {}',
|
|
|
|
'error_message' => 'InvalidTemplateParam'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|