2021-01-31 17:32:24 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
class PropertyTypeInvarianceTest extends TestCase
|
2021-01-31 17:32:24 +01:00
|
|
|
{
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
|
|
*/
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
2021-02-07 07:45:48 +01:00
|
|
|
'validcode' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass
|
|
|
|
{
|
|
|
|
/** @var null|string */
|
|
|
|
protected $mightExist;
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
protected ?string $mightExistNative = null;
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
/** @var string */
|
|
|
|
protected $doesExist = "";
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
protected string $doesExistNative = "";
|
|
|
|
}
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
class ChildClass extends ParentClass
|
|
|
|
{
|
|
|
|
/** @var null|string */
|
|
|
|
protected $mightExist = "";
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
protected ?string $mightExistNative = null;
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
/** @var string */
|
|
|
|
protected $doesExist = "";
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
protected string $doesExistNative = "";
|
2021-03-16 18:43:30 +01:00
|
|
|
}',
|
2021-02-07 07:45:48 +01:00
|
|
|
],
|
|
|
|
'allowTemplatedInvariance' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as string|null
|
|
|
|
*/
|
|
|
|
abstract class A {
|
|
|
|
/** @var T */
|
|
|
|
public $foo;
|
|
|
|
}
|
2021-01-31 17:32:24 +01:00
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
/**
|
|
|
|
* @extends A<string>
|
|
|
|
*/
|
|
|
|
class AChild extends A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "foo";
|
2021-03-16 18:43:30 +01:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'allowTemplatedInvarianceWithListTemplate' => [
|
|
|
|
'<?php
|
|
|
|
abstract class Item {}
|
|
|
|
class Foo extends Item {}
|
|
|
|
|
|
|
|
/** @template TItem of Item */
|
|
|
|
abstract class ItemCollection
|
|
|
|
{
|
|
|
|
/** @var list<TItem> */
|
|
|
|
protected $items = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @extends ItemCollection<Foo> */
|
|
|
|
class FooCollection extends ItemCollection
|
|
|
|
{
|
|
|
|
/** @var list<Foo> */
|
|
|
|
protected $items = [];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'allowTemplatedInvarianceWithClassTemplate' => [
|
|
|
|
'<?php
|
|
|
|
abstract class Item {}
|
|
|
|
class Foo extends Item {}
|
|
|
|
|
|
|
|
/** @template T */
|
|
|
|
class Collection {}
|
|
|
|
|
|
|
|
/** @template TItem of Item */
|
|
|
|
abstract class ItemCollection
|
|
|
|
{
|
|
|
|
/** @var Collection<TItem>|null */
|
|
|
|
protected $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @extends ItemCollection<Foo> */
|
|
|
|
class FooCollection extends ItemCollection
|
|
|
|
{
|
|
|
|
/** @var Collection<Foo>|null */
|
|
|
|
protected $items;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'allowTemplatedInvarianceWithClassString' => [
|
|
|
|
'<?php
|
|
|
|
abstract class Item {}
|
|
|
|
class Foo extends Item {}
|
|
|
|
|
|
|
|
/** @template T of Item */
|
|
|
|
abstract class ItemType
|
|
|
|
{
|
|
|
|
/** @var class-string<T>|null */
|
|
|
|
protected $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @extends ItemType<Foo> */
|
|
|
|
class FooTypes extends ItemType
|
|
|
|
{
|
|
|
|
/** @var class-string<Foo>|null */
|
|
|
|
protected $type;
|
|
|
|
}',
|
2021-02-07 07:45:48 +01:00
|
|
|
],
|
2021-01-31 17:32:24 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
|
|
|
*/
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
2021-02-07 06:52:29 +01:00
|
|
|
'variantDocblockProperties' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass
|
|
|
|
{
|
|
|
|
/** @var null|string */
|
|
|
|
protected $mightExist;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChildClass extends ParentClass
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
protected $mightExist = "";
|
|
|
|
}',
|
|
|
|
'error_message' => 'NonInvariantDocblockPropertyType',
|
|
|
|
],
|
2021-01-31 17:32:24 +01:00
|
|
|
'variantProperties' => [
|
2021-02-07 06:52:29 +01:00
|
|
|
'<?php
|
|
|
|
class ParentClass
|
|
|
|
{
|
|
|
|
protected ?string $mightExist = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChildClass extends ParentClass
|
|
|
|
{
|
|
|
|
protected string $mightExist = "";
|
|
|
|
}',
|
2021-01-31 17:32:24 +01:00
|
|
|
'error_message' => 'NonInvariantPropertyType',
|
2021-02-07 06:52:29 +01:00
|
|
|
],
|
2021-03-16 18:43:30 +01:00
|
|
|
// TODO support property invariance checks with templates
|
|
|
|
// 'disallowInvalidTemplatedInvariance' => [
|
|
|
|
// '<?php
|
|
|
|
// /**
|
|
|
|
// * @template T as string|null
|
|
|
|
// */
|
|
|
|
// abstract class A {
|
|
|
|
// /** @var T */
|
|
|
|
// public $foo;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @extends A<string>
|
|
|
|
// */
|
|
|
|
// class AChild extends A {
|
|
|
|
// /** @var int */
|
|
|
|
// public $foo = 0;
|
|
|
|
// }',
|
|
|
|
// 'error_message' => 'NonInvariantPropertyType',
|
|
|
|
// ],
|
2021-01-31 17:32:24 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|