1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add support for Iterator<X> type (#2285)

* Add support for Iterator<X> type

* Fix tests
This commit is contained in:
Jáchym Toušek 2019-10-30 19:00:27 +01:00 committed by Matthew Brown
parent dc0dd08a67
commit f2c82fa212
4 changed files with 17 additions and 9 deletions

View File

@ -1106,12 +1106,22 @@ extending all its template params.
```php
/**
* @template-implements IteratorAggregate<int>
* @template-implements ArrayAccess<int>
*/
class SomeIterator implements IteratorAggregate
class SomeIterator implements ArrayAccess
{
public function getIterator() {
yield 5;
public function offsetSet($offset, $value) {
}
public function offsetExists($offset) {
return false;
}
public function offsetUnset($offset) {
}
public function offsetGet($offset) {
return null;
}
}
```

View File

@ -251,7 +251,7 @@ abstract class Type
&& count($generic_params) === 1
) {
array_unshift($generic_params, new Union([new TArrayKey]));
} elseif (($generic_type_value === 'iterable' || $generic_type_value === 'Traversable')
} elseif (in_array($generic_type_value, ['iterable', 'Traversable', 'Iterator', 'IteratorAggregate'], true)
&& count($generic_params) === 1
) {
array_unshift($generic_params, new Union([new TMixed]));

View File

@ -973,8 +973,6 @@ class ForeachTest extends \Psalm\Tests\TestCase
'<?php
/**
* @param Iterator<string> $arr
* @psalm-suppress MissingTemplateParam
* @psalm-suppress MixedAssignment
*/
function foo(Iterator $arr) : void {
foreach ($arr as $a) {}

View File

@ -186,7 +186,7 @@ class TypeParseTest extends TestCase
*/
public function testInteratorAndTraversable()
{
$this->assertSame('Iterator<int>&Traversable', (string) Type::parseString('Iterator<int>&Traversable'));
$this->assertSame('Iterator<mixed, int>&Traversable', (string) Type::parseString('Iterator<int>&Traversable'));
}
/**
@ -195,7 +195,7 @@ class TypeParseTest extends TestCase
public function testTraversableAndIteratorOrNull()
{
$this->assertSame(
'Traversable&Iterator<int>|null',
'Traversable&Iterator<mixed, int>|null',
(string) Type::parseString('Traversable&Iterator<int>|null')
);
}