klesun / deep-assoc-completion

A phpstorm plugin for associative array key typing and completion
Other
267 stars 17 forks source link

foreach to an iterator wich returns an array #191

Open SamueleD78 opened 2 years ago

SamueleD78 commented 2 years ago

I don't know if it's me, but i have a class which implements iterator, and on the current() override i return an array with the subkeys definitions.

When i use the object inside a foreach, the value elements are not been recognized. If i explicitly call the ->current() method, it is.

class A implements \Iterator
{
    private int $position;
    private array $keys;
    private array $values;

    public function __construct(array $matches)
    {
        $this->position = 0;
        $this->keys = array_keys($matches);
        $this->values = array_values($matches);
    }

    /**
     * @return array [
     *  'rule' => new RoutingRule
     * ]
     */
    public function current(): array
    {
        return $this->values[$this->position];
    }

    public function next(): void
    {
        ++$this->position;
    }

    public function key(): string
    {
        return $this->keys[$this->position];
    }

    public function valid(): bool
    {
        return isset($this->values[$this->position]);
    }

    public function rewind(): void
    {
        $this->position = 0;
    }
}

$test = new A(["a" => new RoutingRule()]);
$test->current()[WORKS];
foreach ($test as $value) {
  $value[DOESNTWORKS]
}
klesun commented 2 years ago

Hi. Thanks for the use case. I think the plugin did not have support for the custom \Iterator classes. Would need to implement it. I won't have the time myself anytime soon sadly, the only hope is on an MR ;c