2021-01-31 17:32:24 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2021-01-31 17:32:24 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2021-02-07 07:45:48 +01:00
|
|
|
class PropertyTypeInvarianceTest extends TestCase
|
2021-01-31 17:32:24 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2021-01-31 17:32:24 +01:00
|
|
|
|
|
|
|
/**
|
2022-11-05 22:34:42 +01:00
|
|
|
*
|
2021-01-31 17:32:24 +01:00
|
|
|
*/
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
2021-02-07 07:45:48 +01:00
|
|
|
'validcode' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-07 07:45:48 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-07 07:45:48 +01:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-16 18:43:30 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-16 18:43:30 +01:00
|
|
|
abstract class Item {}
|
|
|
|
class Foo extends Item {}
|
2021-03-20 02:44:44 +01:00
|
|
|
|
2021-03-16 18:43:30 +01:00
|
|
|
/** @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;
|
|
|
|
}',
|
|
|
|
],
|
2021-03-20 02:55:50 +01:00
|
|
|
'allowTemplatedInvarianceWithClassStringTemplate' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-16 18:43:30 +01:00
|
|
|
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-03-20 02:55:50 +01:00
|
|
|
'templatedInvarianceGrandchild' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-20 02:55:50 +01:00
|
|
|
abstract class Item {}
|
|
|
|
class Foo extends Item {}
|
|
|
|
class Bar extends Foo {}
|
|
|
|
|
|
|
|
/** @template T of Item */
|
|
|
|
abstract class ItemCollection
|
|
|
|
{
|
|
|
|
/** @var list<T> */
|
|
|
|
protected $items = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of Foo
|
|
|
|
* @extends ItemCollection<T>
|
|
|
|
*/
|
|
|
|
class FooCollection extends ItemCollection
|
|
|
|
{
|
|
|
|
/** @var list<T> */
|
|
|
|
protected $items = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @extends FooCollection<Bar> */
|
|
|
|
class BarCollection extends FooCollection
|
|
|
|
{
|
|
|
|
/** @var list<Bar> */
|
|
|
|
protected $items = [];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'allowTemplateCovariant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-20 02:55:50 +01:00
|
|
|
class Foo {}
|
|
|
|
class Bar extends Foo {}
|
|
|
|
class Baz extends Bar {}
|
|
|
|
|
|
|
|
/** @template-covariant T */
|
|
|
|
class Pair
|
|
|
|
{
|
|
|
|
/** @var T|null */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
/** @var T|null */
|
|
|
|
public $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @extends Pair<Foo> */
|
|
|
|
class FooPair extends Pair
|
|
|
|
{
|
|
|
|
/** @var Bar|null */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
/** @var Baz|null */
|
|
|
|
public $b;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'allowTemplateCovariantManyTemplates' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-20 02:55:50 +01:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Ta
|
|
|
|
* @template Tb
|
|
|
|
* @template-covariant Tc
|
|
|
|
* @template Td
|
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
/** @var Ta|null */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
/** @var Tb|null */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
/** @var Tc|null */
|
|
|
|
public $c;
|
|
|
|
|
|
|
|
/** @var Td|null */
|
|
|
|
public $d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Ta
|
|
|
|
* @template Tb
|
|
|
|
* @template-covariant Tc
|
|
|
|
* @template Td
|
|
|
|
* @extends Foo<Ta, Tb, Tc, Td>
|
|
|
|
*/
|
|
|
|
class Bar extends Foo {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Ta
|
|
|
|
* @template Tb
|
|
|
|
* @template-covariant Tc
|
|
|
|
* @template Td
|
|
|
|
* @extends Bar<A, B, A, C>
|
|
|
|
*/
|
|
|
|
class Baz extends Bar {
|
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
/** @var B|null */
|
|
|
|
public $b;
|
|
|
|
|
|
|
|
/** @var C|null */
|
|
|
|
public $c;
|
|
|
|
|
|
|
|
/** @var C|null */
|
|
|
|
public $d;
|
|
|
|
}',
|
|
|
|
],
|
2021-01-31 17:32:24 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
2021-02-07 06:52:29 +01:00
|
|
|
'variantDocblockProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-07 06:52:29 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-07 06:52:29 +01:00
|
|
|
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-20 02:55:50 +01:00
|
|
|
'variantTemplatedProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-20 02:55:50 +01:00
|
|
|
/**
|
|
|
|
* @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' => 'NonInvariantDocblockPropertyType',
|
|
|
|
],
|
|
|
|
'variantTemplatedGrandchild' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-20 02:55:50 +01:00
|
|
|
abstract class Item {}
|
|
|
|
class Foo extends Item {}
|
|
|
|
class Bar extends Foo {}
|
|
|
|
|
|
|
|
/** @template T of Item */
|
|
|
|
abstract class ItemCollection
|
|
|
|
{
|
|
|
|
/** @var list<T> */
|
|
|
|
protected $items = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T of Foo
|
|
|
|
* @extends ItemCollection<T>
|
|
|
|
*/
|
|
|
|
class FooCollection extends ItemCollection
|
|
|
|
{
|
|
|
|
/** @var list<T> */
|
|
|
|
protected $items = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @extends FooCollection<Bar> */
|
|
|
|
class BarCollection extends FooCollection
|
|
|
|
{
|
|
|
|
/** @var list<Item> */ // Should be list<Bar>
|
|
|
|
protected $items = [];
|
|
|
|
}',
|
|
|
|
'error_message' => 'NonInvariantDocblockPropertyType',
|
|
|
|
],
|
|
|
|
'variantPropertiesWithTemplateNotSpecified' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-20 02:55:50 +01:00
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
/** @template T */
|
|
|
|
class Pair
|
|
|
|
{
|
|
|
|
/** @var T|null */
|
|
|
|
protected $a;
|
|
|
|
|
|
|
|
/** @var T|null */
|
|
|
|
protected $b;
|
|
|
|
}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
2022-11-05 22:34:42 +01:00
|
|
|
* @psalm-suppress MissingTemplateParam
|
2022-01-26 18:46:02 +01:00
|
|
|
*/
|
2021-03-20 02:55:50 +01:00
|
|
|
class FooPair extends Pair
|
|
|
|
{
|
|
|
|
/** @var Foo|null */ // Template defaults to mixed, this is invariant
|
|
|
|
protected $a;
|
|
|
|
|
|
|
|
/** @var Foo|null */
|
|
|
|
protected $b;
|
|
|
|
}',
|
|
|
|
'error_message' => 'NonInvariantDocblockPropertyType',
|
|
|
|
],
|
2021-01-31 17:32:24 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|