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

Psalm doesn't recognize that an array key can be an int #9295

Open
kamil-tekiela opened this issue Feb 14, 2023 · 5 comments
Open

Psalm doesn't recognize that an array key can be an int #9295

kamil-tekiela opened this issue Feb 14, 2023 · 5 comments

Comments

@kamil-tekiela
Copy link
Contributor

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 be int|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.

@psalm-github-bot
Copy link

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);
}
Psalm output (using commit f73fe36):

ERROR: RedundantCastGivenDocblockType - 14:23 - Redundant cast to string given docblock-provided type

INFO: UnusedForeachValue - 13:26 - $value is never referenced or the value is not used

@dragosprotung
Copy link

I had the same issue just now. Here is another code snippet that reproduces the issue:
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
}

@psalm-github-bot
Copy link

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
}
Psalm output (using commit f73fe36):

ERROR: RedundantCast - 11:7 - Redundant cast to string

@kkmuffme
Copy link
Contributor

kkmuffme commented Dec 7, 2023

Basic unit test cases:
https://psalm.dev/r/db8d70db4e

Funny enough for array shapes it already works correctly as the example shows
=> only for fallback_params it has the same issue.

Duplicate of #8680

Copy link

psalm-github-bot bot commented Dec 7, 2023

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 */;   
    }
}
Psalm output (using commit 0e43c44):

ERROR: InvalidArgument - 4:5 - Argument 1 of abc expects array<numeric-string, string>, but array{15: 'a', 17: 'b'} provided

ERROR: InvalidArgument - 5:5 - Argument 1 of def expects array<'17'|15, string>, but array{15: 'a', 17: 'b'} provided

INFO: Trace - 14:29 - $k: numeric-string

INFO: Trace - 24:29 - $k: '17'|15

INFO: Trace - 34:29 - $k: 15|17

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants