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

array_slice()ing templated array parameter infers array<array-key, mixed> #6160

Closed
someniatko opened this issue Jul 23, 2021 · 4 comments · Fixed by #6170
Closed

array_slice()ing templated array parameter infers array<array-key, mixed> #6160

someniatko opened this issue Jul 23, 2021 · 4 comments · Fixed by #6170

Comments

@someniatko
Copy link
Contributor

https://psalm.dev/r/e5c56d6c48

Versions
without @template: https://psalm.dev/r/e933bf53a9
and without array_slice: https://psalm.dev/r/42bab64fa6

behave as expected

@psalm-github-bot
Copy link

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

INFO: MixedReturnTypeCoercion - 14:12 - The type 'array<array-key, mixed>' is more general than the declared return type 'array<array-key, string>' for foo

INFO: MixedReturnTypeCoercion - 6:12 - The declared return type 'array<array-key, string>' for foo is more specific than the inferred return type 'array<array-key, mixed>'
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);
}
Psalm output (using commit 198fdf8):

No issues!
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;
}
Psalm output (using commit 198fdf8):

No issues!

@weirdan
Copy link
Collaborator

weirdan commented Jul 23, 2021

That template on the method is a bit useless though, as it does not constrain neither the output nor other parameters.

@someniatko
Copy link
Contributor Author

someniatko commented Jul 23, 2021

@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 😄

@psalm-github-bot
Copy link

psalm-github-bot bot commented Jul 23, 2021

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 */
Psalm output (using commit 198fdf8):

INFO: PossiblyUndefinedIntArrayOffset - 19:22 - Possibly undefined array offset '0' is risky given expected type 'int'. Consider using isset beforehand.

INFO: MixedArgumentTypeCoercion - 19:55 - Argument 1 of fieldFromChunks expects list<string>, parent type array<array-key, mixed> provided

INFO: Trace - 23:34 - $nonNullField: Field

INFO: UnusedVariable - 22:1 - $nonNullField is never referenced or the value is not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants