-
Notifications
You must be signed in to change notification settings - Fork 665
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
array_slice()
ing templated array parameter infers array<array-key, mixed>
#6160
Labels
Comments
I found these snippets: https://psalm.dev/r/e5c56d6c48<?php
/**
* @template T as string[]
* @param T $param
* @return string[]
*/
function foo(array $param): array
{
if (empty($param)) {
return [];
}
return array_slice($param, 1);
}
https://psalm.dev/r/e933bf53a9<?php
/**
* @param string[] $param
* @return string[]
*/
function foo(array $param): array
{
if (empty($param)) {
return [];
}
return array_slice($param, 1);
}
https://psalm.dev/r/42bab64fa6<?php
/**
* @template T as string[]
* @param T $param
* @return string[]
*/
function foo(array $param): array
{
if (empty($param)) {
return [];
}
return $param;
}
|
That template on the method is a bit useless though, as it does not constrain neither the output nor other parameters. |
@weirdan Yeah, i know, it's just a minimal case. Here's the production-ish code: https://psalm.dev/r/d16bbba166 p.s. basically reinventing array to linked list conversion here 😄 |
I found these snippets: https://psalm.dev/r/d16bbba166<?php
class Field
{
public function __construct(public string $value, public ?Field $subField) {}
}
/**
* @psalm-template T of list<string>
* @psalm-param T $fieldChunks
* @psalm-return (T is non-empty-list ? Field : null)
*/
function fieldFromChunks(array $fieldChunks): ?Field
{
if ($fieldChunks === []) {
return null;
}
return new Field($fieldChunks[0], fieldFromChunks(array_slice($fieldChunks, 1)));
}
$nonNullField = fieldFromChunks([ 'a' ]);
/** @psalm-trace $nonNullField */
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://psalm.dev/r/e5c56d6c48
Versions
without
@template
: https://psalm.dev/r/e933bf53a9and without
array_slice
: https://psalm.dev/r/42bab64fa6behave as expected
The text was updated successfully, but these errors were encountered: