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

Prevent argument unpacking iterable with non-array-key key #5454

Closed
AndrolGenhald opened this issue Mar 22, 2021 · 1 comment · Fixed by #5455
Closed

Prevent argument unpacking iterable with non-array-key key #5454

AndrolGenhald opened this issue Mar 22, 2021 · 1 comment · Fixed by #5455

Comments

@AndrolGenhald
Copy link
Collaborator

Iterables have scalar keys, this means when unpacking an iterable when calling a function psalm should show an issue if the key isn't an int or a string.
https://psalm.dev/r/c6f65aebef
https://3v4l.org/Y8Vbu

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/c6f65aebef
<?php

/**
 * @template TValue
 * @implements Iterator<float, TValue>
 */
class FloatKeyIterator implements Iterator
{
    /** @var int */
    private $position = 0;
    /** @var list<array{float, TValue}> */
    private $data = [];
    
    /** @param list<array{float, TValue}> $data */
    public function __construct($data)
    {
        $this->data = $data;
    }
    
    /** @return TValue */
    public function current()
    {
        return $this->data[$this->position][1];
    }
    
    /** @return float */
    public function key()
    {
        return $this->data[$this->position][0];
    }
    
    public function next(): void
    {
        ++$this->position;
    }
    
    public function rewind(): void
    {
        $this->position = 0;
    }
    
    public function valid(): bool
    {
        return isset($this->data[$this->position]);
    }
}

/** @psalm-suppress UnusedParam */
function foo(string ...$args): void {}

/** @var FloatKeyIterator<string> */
$test = new FloatKeyIterator([[3.14, 'pi'], [2.72, 'e']]);
foo(...$test);
Psalm output (using commit 9f3f420):

No issues!

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

Successfully merging a pull request may close this issue.

1 participant