1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Fix template, conditional array keys

This commit is contained in:
Daniil Gentili 2024-01-18 12:59:56 +01:00
parent 680c8cd39d
commit 23543218dc
2 changed files with 66 additions and 1 deletions

View File

@ -672,8 +672,8 @@ final class TypeParser
// PHP 8 values with whitespace after number are counted as numeric
// and filter_var treats them as such too
if ($atomic_type instanceof TLiteralString
&& trim($atomic_type->value) === $atomic_type->value
&& ($string_to_int = filter_var($atomic_type->value, FILTER_VALIDATE_INT)) !== false
&& trim($atomic_type->value) === $atomic_type->value
) {
$builder = $generic_params[0]->getBuilder();
$builder->removeType($key);
@ -688,7 +688,14 @@ final class TypeParser
|| $atomic_type instanceof TMixed
|| $atomic_type instanceof TNever
|| $atomic_type instanceof TTemplateParam
|| $atomic_type instanceof TTemplateIndexedAccess
|| $atomic_type instanceof TTemplateValueOf
|| $atomic_type instanceof TTemplateKeyOf
|| $atomic_type instanceof TTemplateParamClass
|| $atomic_type instanceof TTypeAlias
|| $atomic_type instanceof TValueOf
|| $atomic_type instanceof TConditional
|| $atomic_type instanceof TKeyOf
|| !$from_docblock
) {
continue;

View File

@ -124,6 +124,64 @@ class ArrayKeysTest extends TestCase
}
}',
],
'variousArrayKeys' => [
'code' => '<?php
/**
* @psalm-type TAlias = 123
*/
class a {}
/**
* @psalm-import-type TAlias from a
* @template TKey as array-key
* @template TValue as array-key
* @template T as array<TKey, TValue>
* @template TT
* @template TBool as bool
*/
class b {
/**
* @var array<TAlias, int>
*/
private array $a = [];
/** @var array<value-of<T>, int> */
private array $c = [];
/** @var array<key-of<T>, int> */
private array $d = [];
/** @var array<TT, int> */
private array $e = [];
/** @var array<key-of<array<int, int>>, int> */
private array $f = [];
/** @var array<value-of<array<int, int>>, int> */
private array $g = [];
/** @var array<TBool is true ? string : int, int> */
private array $h = [];
/**
* @param T $arr
* @param class-string<TT> $b
* @param TBool $c
*/
public function __construct(
array $arr,
string $b,
bool $c
) {}
/**
* @return array<$v is true ? "a" : 123, 123>
*/
public function test(bool $v): array {
return $v ? ["a" => 123] : [123 => 123];
}
}',
],
];
}