2022-01-16 21:45:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-17 02:52:07 +01:00
|
|
|
namespace Psalm\Tests\Template;
|
2022-01-16 21:45:58 +01:00
|
|
|
|
2022-02-17 02:52:07 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
2022-01-16 21:45:58 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class KeyOfTemplateTest extends TestCase
|
|
|
|
{
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>,php_version?:string}>
|
|
|
|
*/
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'acceptsArrayKeysFn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return key-of<T>[]
|
|
|
|
*/
|
|
|
|
function getKey($array) {
|
|
|
|
return array_keys($array);
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
|
|
|
'acceptsArrayKeyFirstFn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return key-of<T>|null
|
|
|
|
*/
|
|
|
|
function getKey($array) {
|
|
|
|
return array_key_first($array);
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
|
|
|
'acceptsArrayKeyLastFn' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return key-of<T>|null
|
|
|
|
*/
|
|
|
|
function getKey($array) {
|
|
|
|
return array_key_last($array);
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
2022-01-20 22:47:32 +01:00
|
|
|
'SKIPPED-acceptsIfArrayKeyExistsFn' => [
|
2022-01-20 22:41:33 +01:00
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return key-of<T>|null
|
|
|
|
*/
|
|
|
|
function getKey(string $key, $array) {
|
|
|
|
if (array_key_exists($key, $array)) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
'
|
|
|
|
],
|
2022-01-27 21:59:26 +01:00
|
|
|
'keyOfUnreplacedTemplateParam' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T as array<string, bool>
|
|
|
|
*/
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return key-of<T>
|
|
|
|
*/
|
|
|
|
abstract public function getRandomKey(): string;
|
2022-01-28 00:04:21 +01:00
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
2022-01-31 20:43:28 +01:00
|
|
|
'keyOfNestedTemplates' => [
|
2022-01-28 00:04:21 +01:00
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template TKey of int
|
|
|
|
* @template TArray of array<TKey, bool>
|
|
|
|
* @param TArray $array
|
|
|
|
* @return list<TKey>
|
|
|
|
*/
|
|
|
|
function toListOfKeys(array $array): array {
|
|
|
|
return array_keys($array);
|
|
|
|
}'
|
2022-01-27 21:59:26 +01:00
|
|
|
],
|
2022-01-16 21:45:58 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
|
|
|
*/
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'keyOfTemplateNotIncludesString' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return key-of<T>
|
|
|
|
*/
|
|
|
|
function getKey($array) {
|
2022-01-20 22:41:33 +01:00
|
|
|
return "foo";
|
2022-01-16 21:45:58 +01:00
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
|
|
|
'keyOfTemplateNotIncludesInt' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T of array
|
|
|
|
* @param T $array
|
|
|
|
* @return key-of<T>
|
|
|
|
*/
|
|
|
|
function getKey($array) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnStatement'
|
|
|
|
],
|
2022-01-27 21:59:26 +01:00
|
|
|
'keyOfUnresolvedTemplateParamIsStillChecked' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @template T as array<int, bool>
|
|
|
|
*/
|
|
|
|
abstract class Foo {
|
|
|
|
/**
|
|
|
|
* @return key-of<T>
|
|
|
|
*/
|
|
|
|
abstract public function getRandomKey(): string;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MismatchingDocblockReturnType'
|
|
|
|
],
|
2022-01-16 21:45:58 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|