1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/PropertiesOfTest.php

345 lines
12 KiB
PHP
Raw Normal View History

2022-01-09 21:17:42 +01:00
<?php
namespace Psalm\Tests;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class PropertiesOfTest extends TestCase
{
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
/**
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
2022-01-09 21:17:42 +01:00
*/
public function providerValidCodeParse(): iterable
{
return [
'publicPropertiesOf' => [
'code' => '<?php
class A {
/** @var bool */
public $foo = false;
/** @var string */
private $bar = "";
/** @var int */
protected $adams = 42;
}
2022-01-09 21:17:42 +01:00
/** @return public-properties-of<A> */
function returnPropertyOfA() {
return ["foo" => true];
}
2022-01-09 21:17:42 +01:00
',
],
'protectedPropertiesOf' => [
'code' => '<?php
class A {
/** @var bool */
public $foo = false;
/** @var string */
private $bar = "";
/** @var int */
protected $adams = 42;
}
2022-01-09 21:17:42 +01:00
/** @return protected-properties-of<A> */
function returnPropertyOfA() {
return ["adams" => 42];
}
2022-01-09 21:17:42 +01:00
',
],
'privatePropertiesOf' => [
'code' => '<?php
class A {
/** @var bool */
public $foo = false;
/** @var string */
private $bar = "";
/** @var int */
protected $adams = 42;
}
2022-01-09 21:17:42 +01:00
/** @return private-properties-of<A> */
function returnPropertyOfA() {
return ["bar" => "foo"];
}
2022-01-09 21:17:42 +01:00
',
],
'allPropertiesOf' => [
'code' => '<?php
class A {
/** @var bool */
public $foo = false;
/** @var string */
private $bar = "";
/** @var int */
protected $adams = 42;
}
2022-01-09 21:17:42 +01:00
/** @return properties-of<A> */
function returnPropertyOfA(int $visibility) {
return [
"foo" => true,
"bar" => "foo",
"adams" => 1
];
2022-01-09 21:17:42 +01:00
}
',
],
'allPropertiesOfContainsNoStatic' => [
'code' => '<?php
class A {
/** @var bool */
public static $imStatic = true;
/** @var bool */
public $foo = false;
/** @var string */
private $bar = "";
/** @var int */
protected $adams = 42;
}
/** @return properties-of<A> */
function returnPropertyOfA(int $visibility) {
return [
"foo" => true,
"bar" => "foo",
"adams" => 1
];
}
',
],
2022-01-09 21:17:42 +01:00
'usePropertiesOfSelfAsArrayKey' => [
'code' => '<?php
class A {
/** @var int */
public $a = 1;
/** @var int */
public $b = 2;
2022-01-09 21:17:42 +01:00
/** @return properties-of<self> */
public function asArray() {
return [
"a" => $this->a,
"b" => $this->b
];
}
2022-01-09 21:17:42 +01:00
}
',
2022-01-09 21:17:42 +01:00
],
'usePropertiesOfStaticAsArrayKey' => [
'code' => '<?php
class A {
/** @var int */
public $a = 1;
/** @var int */
public $b = 2;
2022-01-09 21:17:42 +01:00
/** @return properties-of<static> */
public function asArray() {
return [
"a" => $this->a,
"b" => $this->b
];
}
2022-01-09 21:17:42 +01:00
}
class B extends A {
/** @var int */
public $c = 3;
2022-01-09 21:17:42 +01:00
public function asArray() {
return [
"a" => $this->a,
"b" => $this->b,
"c" => $this->c,
];
}
2022-01-09 21:17:42 +01:00
}
',
],
'propertiesOfMultipleInheritanceStaticAsArrayKey' => [
'code' => '<?php
class A {
/** @var int */
public $a = 1;
/** @var int */
public $b = 2;
2022-01-09 21:17:42 +01:00
/** @return properties-of<static> */
public function asArray() {
return [
"a" => $this->a,
"b" => $this->b
];
}
2022-01-09 21:17:42 +01:00
}
class B extends A {
/** @var int */
public $c = 3;
}
2022-01-09 21:17:42 +01:00
class C extends B {
/** @var int */
public $d = 4;
2022-01-09 21:17:42 +01:00
public function asArray() {
return [
"a" => $this->a,
"b" => $this->b,
"c" => $this->c,
"d" => $this->d,
];
}
2022-01-09 21:17:42 +01:00
}
',
],
];
}
/**
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
2022-01-09 21:17:42 +01:00
*/
public function providerInvalidCodeParse(): iterable
{
return [
'onlyOneTemplateParam' => [
'code' => '<?php
class A {}
class B {}
2022-01-09 21:17:42 +01:00
/** @var properties-of<A, B> */
$test = "foobar";
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidDocblock',
],
'propertiesOfPicksNoStatic' => [
'code' => '<?php
class A {
/** @var mixed */
public static $foo;
}
/** @return properties-of<A> */
function returnPropertyOfA() {
return ["foo" => true];
}
',
'error_message' => 'InvalidReturnStatement'
],
2022-01-09 21:17:42 +01:00
'publicPropertiesOfPicksNoPrivate' => [
'code' => '<?php
class A {
/** @var mixed */
public $foo;
/** @var mixed */
private $bar;
/** @var mixed */
protected $adams;
}
2022-01-09 21:17:42 +01:00
/** @return public-properties-of<A> */
function returnPropertyOfA() {
return ["bar" => true];
}
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidReturnStatement'
],
'publicPropertiesOfPicksNoProtected' => [
'code' => '<?php
class A {
/** @var mixed */
public $foo;
/** @var mixed */
private $bar;
/** @var mixed */
protected $adams;
}
2022-01-09 21:17:42 +01:00
/** @return public-properties-of<A> */
function returnPropertyOfA() {
return ["adams" => true];
}
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidReturnStatement'
],
'protectedPropertiesOfPicksNoPublic' => [
'code' => '<?php
class A {
/** @var mixed */
public $foo;
/** @var mixed */
private $bar;
/** @var mixed */
protected $adams;
}
2022-01-09 21:17:42 +01:00
/** @return protected-properties-of<A> */
function returnPropertyOfA() {
return ["foo" => true];
}
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidReturnStatement'
],
'protectedPropertiesOfPicksNoPrivate' => [
'code' => '<?php
class A {
/** @var mixed */
public $foo;
/** @var mixed */
private $bar;
/** @var mixed */
protected $adams;
}
2022-01-09 21:17:42 +01:00
/** @return protected-properties-of<A> */
function returnPropertyOfA() {
return ["bar" => true];
}
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidReturnStatement'
],
'privatePropertiesOfPicksNoPublic' => [
'code' => '<?php
class A {
/** @var mixed */
public $foo;
/** @var mixed */
private $bar;
/** @var mixed */
protected $adams;
}
2022-01-09 21:17:42 +01:00
/** @return private-properties-of<A> */
function returnPropertyOfA() {
return ["foo" => true];
}
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidReturnStatement'
],
'privatePropertiesOfPicksNoProtected' => [
'code' => '<?php
class A {
/** @var mixed */
public $foo;
/** @var mixed */
private $bar;
/** @var mixed */
protected $adams;
}
2022-01-09 21:17:42 +01:00
/** @return private-properties-of<A> */
function returnPropertyOfA() {
return ["adams" => true];
}
2022-01-09 21:17:42 +01:00
',
'error_message' => 'InvalidReturnStatement'
],
];
}
}