1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 01:09:38 +01:00
psalm/tests/PropertyTypeInvarianceTest.php
Matt Brown 4d76f7545c Allow property type invariance on templated properties
This is a bit of a hack – the comparison should be similar to the ones done in MethodComparator, but this avoids false-positives for now
2021-02-07 01:45:48 -05:00

101 lines
3.0 KiB
PHP

<?php
namespace Psalm\Tests;
class PropertyTypeInvarianceTest extends TestCase
{
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
public function providerValidCodeParse(): iterable
{
return [
'validcode' => [
'<?php
class ParentClass
{
/** @var null|string */
protected $mightExist;
protected ?string $mightExistNative = null;
/** @var string */
protected $doesExist = "";
protected string $doesExistNative = "";
}
class ChildClass extends ParentClass
{
/** @var null|string */
protected $mightExist = "";
protected ?string $mightExistNative = null;
/** @var string */
protected $doesExist = "";
protected string $doesExistNative = "";
}'
],
'allowTemplatedInvariance' => [
'<?php
/**
* @template T as string|null
*/
abstract class A {
/** @var T */
public $foo;
}
/**
* @extends A<string>
*/
class AChild extends A {
/** @var string */
public $foo = "foo";
}'
],
];
}
/**
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
public function providerInvalidCodeParse(): iterable
{
return [
'variantDocblockProperties' => [
'<?php
class ParentClass
{
/** @var null|string */
protected $mightExist;
}
class ChildClass extends ParentClass
{
/** @var string */
protected $mightExist = "";
}',
'error_message' => 'NonInvariantDocblockPropertyType',
],
'variantProperties' => [
'<?php
class ParentClass
{
protected ?string $mightExist = null;
}
class ChildClass extends ParentClass
{
protected string $mightExist = "";
}',
'error_message' => 'NonInvariantPropertyType',
],
];
}
}