1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 10:17:33 +01:00
psalm/tests/Template/KeyOfTemplateTest.php

144 lines
4.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Psalm\Tests\Template;
use Psalm\Tests\TestCase;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class KeyOfTemplateTest extends TestCase
{
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
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);
}
2022-12-18 17:15:15 +01:00
',
],
'acceptsArrayKeyFirstFn' => [
'code' => '<?php
/**
* @template T of array
* @param T $array
* @return key-of<T>|null
*/
function getKey($array) {
return array_key_first($array);
}
2022-12-18 17:15:15 +01:00
',
],
'acceptsArrayKeyLastFn' => [
'code' => '<?php
/**
* @template T of array
* @param T $array
* @return key-of<T>|null
*/
function getKey($array) {
return array_key_last($array);
}
2022-12-18 17:15:15 +01:00
',
],
'SKIPPED-acceptsIfArrayKeyExistsFn' => [
'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-12-18 17:15:15 +01:00
',
],
'keyOfUnreplacedTemplateParam' => [
'code' => '<?php
/**
* @template T as array<string, bool>
*/
abstract class Foo {
/**
* @return key-of<T>
*/
abstract public function getRandomKey(): string;
}
',
],
'keyOfNestedTemplates' => [
'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-12-18 17:15:15 +01:00
}',
],
];
}
public function providerInvalidCodeParse(): iterable
{
return [
'keyOfTemplateNotIncludesString' => [
'code' => '<?php
/**
* @template T of array
* @param T $array
* @return key-of<T>
*/
function getKey($array) {
return "foo";
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
'keyOfTemplateNotIncludesInt' => [
'code' => '<?php
/**
* @template T of array
* @param T $array
* @return key-of<T>
*/
function getKey($array) {
return 0;
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'InvalidReturnStatement',
],
'keyOfUnresolvedTemplateParamIsStillChecked' => [
'code' => '<?php
/**
* @template T as array<int, bool>
*/
abstract class Foo {
/**
* @return key-of<T>
*/
abstract public function getRandomKey(): string;
}
',
2022-12-18 17:15:15 +01:00
'error_message' => 'MismatchingDocblockReturnType',
],
];
}
}