1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-06 05:29:00 +01:00
psalm/tests/KeyOfArrayTest.php

225 lines
7.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Psalm\Tests;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class KeyOfArrayTest extends TestCase
{
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
public function providerValidCodeParse(): iterable
{
return [
'keyOfListClassConstant' => [
'code' => '<?php
class A {
const FOO = [
"bar"
];
/** @return key-of<A::FOO> */
public function getKey() {
return 0;
}
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfAssociativeArrayClassConstant' => [
'code' => '<?php
class A {
const FOO = [
"bar" => 42
];
/** @return key-of<A::FOO> */
public function getKey() {
return "bar";
}
}
2022-12-18 17:15:15 +01:00
',
],
'allKeysOfAssociativeArrayPossible' => [
'code' => '<?php
class A {
const FOO = [
"bar" => 42,
"adams" => 43,
];
/** @return key-of<A::FOO> */
public function getKey(bool $adams) {
if ($adams) {
return "adams";
}
return "bar";
}
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfAsArray' => [
'code' => '<?php
class A {
/** @var array */
const FOO = [
"bar" => 42,
"adams" => 43,
];
/** @return key-of<self::FOO>[] */
public function getKey() {
return array_keys(self::FOO);
}
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfArrayLiteral' => [
'code' => '<?php
/**
* @return key-of<array<int, string>>
*/
function getKey() {
return 32;
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfUnionArrayLiteral' => [
'code' => '<?php
/**
* @return key-of<array<int, string>|array<float, string>>
*/
function getKey(bool $asFloat) {
if ($asFloat) {
return 42.0;
}
return 42;
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfUnionListAndKeyedArray' => [
'code' => '<?php
/**
* @return key-of<list<int>|array{a: int, b: int}>
*/
function getKey(bool $asInt) {
if ($asInt) {
return 42;
}
return "a";
}
',
],
'keyOfListArrayLiteral' => [
'code' => '<?php
/**
* @return key-of<list<string>>
*/
function getKey() {
return 42;
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfStringArrayConformsToString' => [
'code' => '<?php
/**
* @return string
*/
function getKey2() {
/** @var key-of<array<string, string>>[] */
$keys2 = ["foo"];
return $keys2[0];
}
2022-12-18 17:15:15 +01:00
',
],
'keyOfExpandsPropertiesOf' => [
'code' => '<?php
class A {
/** @var bool */
public $foo = false;
/** @var string */
private $bar = "";
/** @var int */
protected $adams = 42;
}
/** @return list<key-of<properties-of<A>>> */
function returnPropertyOfA() {
return ["foo", "bar", "adams"];
}
',
],
];
}
public function providerInvalidCodeParse(): iterable
{
return [
'onlyDefinedKeysOfAssociativeArray' => [
'code' => '<?php
class A {
const FOO = [
"bar" => 42
];
/** @return key-of<A::FOO> */
public function getKey() {
return "adams";
}
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
'keyOfArrayLiteral' => [
'code' => '<?php
class A {
/**
* @return key-of<array<int, string>>
*/
public function getKey() {
return "foo";
}
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
'onlyIntAllowedForKeyOfList' => [
'code' => '<?php
class A {
/**
* @return key-of<list<string>>
*/
public function getKey() {
return "42";
}
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
'noStringAllowedInKeyOfIntFloatArray' => [
'code' => '<?php
/**
* @return key-of<array<int, string>|array<float, string>>
*/
function getKey(bool $asFloat) {
if ($asFloat) {
return 42.0;
}
return "42";
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
'noLiteralCAllowedInKeyOfUnionListAndKeyedArray' => [
'code' => '<?php
/**
* @return key-of<list<int>|array{a: int, b: int}>
*/
function getKey() {
return "c";
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
];
}
}