-
Notifications
You must be signed in to change notification settings - Fork 657
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
Psalm doesn't recognize that an array key can be an int #9295
Comments
I found these snippets: https://psalm.dev/r/5edbef1817<?php
declare(strict_types=1);
/** @var string[] $strings */
$strings = (function():array{
return ['1'];
})();
$arr = [];
foreach ($strings as $string) {
$arr[$string] = 42;
}
foreach ($arr as $key => $value) {
echo htmlentities((string) $key);
}
|
I had the same issue just now. Here is another code snippet that reproduces the issue: <?php declare(strict_types = 1);
class test {
public string $prop = '1';
}
$test = new test();
$array = [$test->prop => 'value'];
foreach ($array as $k => $_) {
echo (string) $k; // even if the key was a string, but it represented an int, PHP will convert it to int
} |
I found these snippets: https://psalm.dev/r/4517dabd6d<?php declare(strict_types = 1);
class test {
public string $prop = '1';
}
$test = new test();
$array = [$test->prop => 'value'];
foreach ($array as $k => $_) {
echo (string) $k; // even if the key was a string, but it represented an int, PHP will convert it to int
}
|
Basic unit test cases: Funny enough for array shapes it already works correctly as the example shows Duplicate of #8680 |
I found these snippets: https://psalm.dev/r/db8d70db4e<?php
$x = ['15' => 'a', 17 => 'b'];
abc($x);
def($x);
ghi($x);
/**
* @param array<numeric-string, string> $arg
* @return void
*/
function abc($arg) {
foreach ($arg as $k => $v) {
/** @psalm-trace $k */; // should be int|numeric-string since float numeric strings stay strings, e.g. '1.2'
}
}
/**
* @param array<15|'17', string> $arg
* @return void
*/
function def($arg) {
foreach ($arg as $k => $v) {
/** @psalm-trace $k */;
}
}
/**
* @param array{15: string, '17': string} $arg
* @return void
*/
function ghi($arg) {
foreach ($arg as $k => $v) {
/** @psalm-trace $k */;
}
}
|
kkmuffme
added a commit
to kkmuffme/psalm
that referenced
this issue
Dec 12, 2023
kkmuffme
added a commit
to kkmuffme/psalm
that referenced
this issue
Dec 12, 2023
kkmuffme
added a commit
to kkmuffme/psalm
that referenced
this issue
Dec 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP maintains numeric strings as integers whenever used as an array key. It's a little quirk of PHP. Psalm however, doesn't recognize the fact that a value which is initially
string
can beint|string
when used as an array key.This example is showing how to do it properly with a string cast:
https://psalm.dev/r/5edbef1817
Psalm says that the cast is redundant but it's not. Without it, PHP would throw an error!
https://3v4l.org/WirJP
Psalm should treat all array keys as
int|string
unless the array is a list.The text was updated successfully, but these errors were encountered: