-
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
Literal string key is being casted to integer #8680
Comments
I found these snippets: https://psalm.dev/r/791f571f06<?php
class a {
private const REDIRECTS = [
'a' => [
'from' => '79268724911',
'to' => '74950235931',
],
'b' => [
'from' => '79313044964',
'to' => '78124169167',
],
];
private const SIP_FORMAT = 'sip:%s@voip.test.com:9090';
/** @return array<string, string> */
public function test(): array {
$redirects = [];
foreach (self::REDIRECTS as $redirect) {
$redirects[$redirect['from']] = sprintf(self::SIP_FORMAT, $redirect['to']);
}
return $redirects;
}
}
|
Yeah, this is closely related to #4553 Basically, there's no way of guaranteeing a Unfortunately, this is the kind of issue where almost every move you'll make will break some use case. If I had to change something in PHP, this is one of the first thing I would change and I'm not the only one :p |
@orklah the literal array key type part looks wrong though: Shouldn't we remove those literal strings in this case? |
Holy cow that's the weirdest thing I've ever seen, and I thought I knew all of PHP's weird quirks :P Anyway I think this can be easily fixed by checking if the string is numeric, and in this case specifying only integers, as suggested by @weirdan. |
I believe it was made on purpose to allow users to use the string again like this: https://psalm.dev/r/ecf0649a4d. This could possibly be removed but you'll have to handle the access side of things too and redo the implicit casting But this will unfortunately work only with literals. For real life arrays with unknown string offset, you can never really be sure you won't end up with numeric-strings in there that will mess up your strictly typed array :p |
I found these snippets: https://psalm.dev/r/ecf0649a4d<?php
$a = '1111';
$b = [$a => 'b'];
$c = $b[$a];
/** @psalm-trace $c */;
|
https://psalm.dev/r/791f571f06
The text was updated successfully, but these errors were encountered: