Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 26, 2022
1 parent 5aac6a2 commit 4dc74e9
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/running_psalm/issues/InvalidTraversableImplementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ implemented by implementing either `IteratorAggregate` or `Iterator`
```php
<?php

/**
* @implements Traversable<mixed, mixed>
*/
final class C implements Traversable {} // will cause fatal error
```
6 changes: 6 additions & 0 deletions tests/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ function getArray() : array { return []; }
$c = isset($array["foo"]) ? $array["foo"] : null;
/**
* @psalm-suppress MissingTemplateParam
*/
class C implements ArrayAccess
{
/**
Expand Down Expand Up @@ -875,6 +878,9 @@ class A {
public function foo() : void {}
}
/**
* @psalm-suppress MissingTemplateParam
*/
class C implements ArrayAccess
{
/**
Expand Down
6 changes: 6 additions & 0 deletions tests/ArrayAssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ function getThings(): array {
],
'implementsArrayAccess' => [
'<?php
/**
* @implements \ArrayAccess<array-key, mixed>
*/
class A implements \ArrayAccess {
/**
* @param string|int $offset
Expand Down Expand Up @@ -734,6 +737,9 @@ public function offsetGet($offset) {
],
'implementsArrayAccessInheritingDocblock' => [
'<?php
/**
* @implements \ArrayAccess<string, mixed>
*/
class A implements \ArrayAccess
{
/**
Expand Down
6 changes: 6 additions & 0 deletions tests/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ class Baz {}
use IteratorAggregate;
use ReturnTypeWillChange;
/**
* @psalm-suppress MissingTemplateParam
*/
final class EmptyCollection implements IteratorAggregate
{
#[ReturnTypeWillChange]
Expand All @@ -229,6 +232,9 @@ public function getIterator()
use IteratorAggregate;
use ReturnTypeWillChange;
/**
* @psalm-suppress MissingTemplateParam
*/
final class EmptyCollection implements IteratorAggregate
{
#[ReturnTypeWillChange]
Expand Down
8 changes: 7 additions & 1 deletion tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ function generator(): Generator {
],
'iteratorToArrayWithGetIterator' => [
'<?php
/**
* @implements IteratorAggregate<int, string, Traversable<int, string>>
*/
class C implements IteratorAggregate {
/**
* @return Traversable<int,string>
Expand All @@ -525,9 +528,12 @@ public function getIterator() {
],
'iteratorToArrayWithGetIteratorReturningList' => [
'<?php
/**
* @implements IteratorAggregate<int, string, Traversable<int, string>>
*/
class C implements IteratorAggregate {
/**
* @return Traversable<int,string>
* @return Traversable<int, string>
*/
public function getIterator() {
yield 1 => "1";
Expand Down
9 changes: 9 additions & 0 deletions tests/Loop/ForeachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@ function foo(array $arr): void {
],
'iteratorAggregateIteration' => [
'<?php
/**
* @psalm-suppress MissingTemplateParam
*/
class C implements IteratorAggregate
{
public function getIterator(): Iterator
Expand Down Expand Up @@ -790,6 +793,9 @@ class Item {
public $prop = "var";
}
/**
* @implements IteratorAggregate<array-key, Item, ArrayIterator<array-key, Item>>
*/
class Collection implements IteratorAggregate {
/**
* @var Item[]
Expand Down Expand Up @@ -878,6 +884,9 @@ function Foo(int $a, int $b, int ...$ints) : array {
'<?php
class Value {}
/**
* @psalm-suppress MissingTemplateParam
*/
class ValueCollection implements \Countable, \IteratorAggregate {
/**
* @var Value[]
Expand Down
1 change: 1 addition & 0 deletions tests/MagicMethodAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class A {
public function find() {}
}
/** @psalm-suppress MissingTemplateParam */
class B extends A {}
class Obj {}
Expand Down
3 changes: 3 additions & 0 deletions tests/MethodSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ public function boo(A $class): void {}
],
'allowMixedExtensionOfIteratorAggregate' => [
'<?php
/**
* @psalm-suppress MissingTemplateParam
*/
class C implements IteratorAggregate {
public function getIterator(): Iterator {
return new ArrayIterator([]);
Expand Down
3 changes: 3 additions & 0 deletions tests/MixinAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function foo(string $dir) : void {
],
'wrapCustomIterator' => [
'<?php
/**
* @implements Iterator<1, 2>
*/
class Subject implements Iterator {
/**
* the index method exists
Expand Down
3 changes: 3 additions & 0 deletions tests/Php71Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ function iterator(iterable $iter): void
],
'traversableObject' => [
'<?php
/**
* @implements Iterator<0, mixed>
*/
class IteratorObj implements Iterator {
function rewind(): void {}
/** @return mixed */
Expand Down
27 changes: 25 additions & 2 deletions tests/Template/ClassTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,24 +531,37 @@ function(Collection $elt): bool { return true; }
/**
* @template TKey
* @template TValue
*
* @extends \IteratorAggregate<TKey, TValue, \Traversable<TKey,TValue>>
*/
interface ICollection extends \IteratorAggregate {
/** @return \Traversable<TKey,TValue> */
public function getIterator();
}
/**
* @template TKey as array-key
* @template TValue
*
* @implements ICollection<TKey, TValue>
*/
class Collection implements ICollection {
/** @var array */
/** @var array<TKey, TValue> */
private $data;
/**
* @param array<TKey, TValue> $data
*/
public function __construct(array $data) {
$this->data = $data;
}
/**
* @return \Traversable<TKey, TValue>
*/
public function getIterator(): \Traversable {
return new \ArrayIterator($this->data);
}
}
/** @var ICollection<string, int> */
$c = new Collection(["a" => 1]);
foreach ($c as $k => $v) { atan($v); strlen($k); }',
Expand Down Expand Up @@ -862,6 +875,7 @@ public function getValue()
}
/**
* @psalm-suppress MissingTemplateParam
* @template TKey
* @template TValue
*/
Expand Down Expand Up @@ -926,6 +940,8 @@ public function getValue()
/**
* @template TKey
* @template Tv
*
* @psalm-suppress MissingTemplateParam
*/
class KeyValueContainer extends ValueContainer
{
Expand Down Expand Up @@ -985,6 +1001,9 @@ public function getID()
}
}
/**
* @psalm-suppress MissingTemplateParam
*/
class AppUser extends User {}
$au = new AppUser(-1);
Expand Down Expand Up @@ -1732,6 +1751,7 @@ interface IParent {
function foo();
}
/** @psalm-suppress MissingTemplateParam */
interface IChild extends IParent {}
class C {}
Expand Down Expand Up @@ -1766,6 +1786,8 @@ class C {}
* @psalm-param class-string<T> $className
*
* @psalm-return T&I<T>
*
* @psalm-suppress MissingTemplateParam
*/
function makeConcrete(string $className) : object
{
Expand Down Expand Up @@ -1926,6 +1948,7 @@ class Foo {
/** @psalm-param T $val */
public function set($val) : void {
$this->value = $val;
/** @psalm-suppress MissingTemplateParam */
new class extends Foo {};
}
Expand Down
2 changes: 1 addition & 1 deletion tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ public function unserialize($_serialized) : void {}
],
'useIteratorMethodsWhenCallingForeach' => [
'<?php
/** @psalm-suppress UnimplementedInterfaceMethod */
/** @psalm-suppress UnimplementedInterfaceMethod, MissingTemplateParam */
class IterableResult implements \Iterator {
public function current() {
return null;
Expand Down
1 change: 1 addition & 0 deletions tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,7 @@ function test(Exception $e): callable {
'<?php
/**
* @psalm-immutable
* @psalm-suppress MissingTemplateParam
*/
class A implements IteratorAggregate
{
Expand Down

0 comments on commit 4dc74e9

Please sign in to comment.