Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix template, conditional array keys #10568

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ private static function getTypeFromGenericTree(
// 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);
Expand All @@ -688,7 +688,14 @@ private static function getTypeFromGenericTree(
|| $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;
Expand Down
59 changes: 59 additions & 0 deletions tests/ArrayKeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,65 @@ public function test(): array {
}
}',
],
'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 TOrig as a|b
* @template TT as class-string<TOrig>
*
* @template TBool as bool
*/
class b {
/**
* @var array<TAlias, int>
*/
private array $a = [123 => 123];

/** @var array<value-of<T>, int> */
public array $c = [];

/** @var array<key-of<T>, int> */
public array $d = [];

/** @var array<TT, int> */
public array $e = [];

/** @var array<key-of<array<int, string>>, int> */
private array $f = [123 => 123];

/** @var array<value-of<array<int, string>>, int> */
private array $g = ["test" => 123];

/** @var array<TBool is true ? string : int, int> */
private array $h = [123 => 123];

/**
* @return array<$v is true ? "a" : 123, 123>
*/
public function test(bool $v): array {
return $v ? ["a" => 123] : [123 => 123];
}
}

/** @var b<"testKey", "testValue", array<"testKey", "testValue">, b, class-string<b>, true> */
$b = new b;
$b->d["testKey"] = 123;

// TODO
//$b->c["testValue"] = 123;
//$b->e["b"] = 123;
',
],
];
}

Expand Down