-
Notifications
You must be signed in to change notification settings - Fork 650
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
Invalid return type for template where param and return are the same array #5994
Comments
I found these snippets: https://psalm.dev/r/c75c90c4ec<?php
/**
* @template T of array<int, string>
* @psalm-param T $a
* @psalm-return T
*/
function takesAnInt(array $a) {
for ($i=random_int(0,10); $i<10; $i++){
$a[$i] = md5((string) $i);
}
return $a;
}
|
If we unset the first key instead then Psalm has no issues with modifying it https://psalm.dev/r/d6609968af However if we modify an internal item (even if we explicitly check it exists first) the same issue is thrown. |
I found these snippets: https://psalm.dev/r/d6609968af<?php
/**
* @template T of array<int, string>
* @psalm-param T $a
* @psalm-return T
*/
function takesAnInt(array $a) {
unset($a[random_int(0,10)]);
return $a;
}
https://psalm.dev/r/c75c90c4ec<?php
/**
* @template T of array<int, string>
* @psalm-param T $a
* @psalm-return T
*/
function takesAnInt(array $a) {
for ($i=random_int(0,10); $i<10; $i++){
$a[$i] = md5((string) $i);
}
return $a;
}
|
class-string-map https://psalm.dev/docs/annotating_code/type_syntax/utility_types/#class-string-mapt-of-foo-t does exactly this for class instances. |
I found these snippets: https://psalm.dev/r/c39fbb3124<?php
/**
* @template T of int
* @psalm-param array<T, string> $a
* @psalm-return array<T, string>
*/
function takesAnInt($a) {
foreach ( $a as $key => $value ) {
$a[ $key ] = 'hello' . $value;
}
return $a;
}
$x = takesAnInt( array( 10 => 'bar', 15 => 'world' ) );
/** @psalm-trace $x */;
|
* remove useless replace return type provider code that returned incorrect type for array replacements and was worse duplicate of existing stubs * require preg patterns to be non-empty-strings as otherwise it will throw a PHP notice * improve return type of array replacements to be more correct (not fully correct due to vimeo#5994 (comment))
https://psalm.dev/r/c75c90c4ec
The issue here is that we're wanting the docblock to declare that the function must return the input param. Psalm instead requires us to permit the function to return either the input value, or an alternative array that shares a similar type description.
This means that the common goal of "a function that takes an array, modifies it and returns it" can't be represented without also permitting the function to take an array, discard it and return something that looks like it.
The text was updated successfully, but these errors were encountered: