-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IteratorAggregate - read key and value type from generics if getItera…
…tor() doesn't have PHPDoc type
- Loading branch information
1 parent
f69bd3e
commit c75b0ea
Showing
5 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Bug4415; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
/** | ||
* @implements \IteratorAggregate<int, string> | ||
*/ | ||
class Foo implements \IteratorAggregate | ||
{ | ||
|
||
public function getIterator(): \Iterator | ||
{ | ||
|
||
} | ||
|
||
} | ||
|
||
function (Foo $foo): void { | ||
foreach ($foo as $k => $v) { | ||
assertType('int', $k); | ||
assertType('string', $v); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Bug4415Rule; | ||
|
||
/** | ||
* @template T | ||
* @extends \IteratorAggregate<T> | ||
*/ | ||
interface CollectionInterface extends \IteratorAggregate | ||
{ | ||
/** | ||
* @param T $item | ||
*/ | ||
public function has($item): bool; | ||
|
||
/** | ||
* @return self<T> | ||
*/ | ||
public function sort(): self; | ||
} | ||
|
||
/** | ||
* @template T | ||
* @extends CollectionInterface<T> | ||
*/ | ||
interface MutableCollectionInterface extends CollectionInterface | ||
{ | ||
/** | ||
* @param T $item | ||
* @phpstan-return self<T> | ||
*/ | ||
public function add($item): self; | ||
} | ||
|
||
/** | ||
* @extends CollectionInterface<Category> | ||
*/ | ||
interface CategoryCollectionInterface extends CollectionInterface | ||
{ | ||
public function has($item): bool; | ||
|
||
/** | ||
* @phpstan-return \Iterator<Category> | ||
*/ | ||
public function getIterator(): \Iterator; | ||
} | ||
|
||
/** | ||
* @extends MutableCollectionInterface<Category> | ||
*/ | ||
interface MutableCategoryCollectionInterface extends CategoryCollectionInterface, MutableCollectionInterface | ||
{ | ||
} | ||
|
||
class CategoryCollection implements MutableCategoryCollectionInterface | ||
{ | ||
/** @var array<Category> */ | ||
private array $categories = []; | ||
|
||
public function add($item): self | ||
{ | ||
$this->categories[$item->getName()] = $item; | ||
return $this; | ||
} | ||
|
||
public function has($item): bool | ||
{ | ||
return isset($this->categories[$item->getName()]); | ||
} | ||
|
||
public function sort(): self | ||
{ | ||
return $this; | ||
} | ||
|
||
public function getIterator(): \Iterator | ||
{ | ||
return new \ArrayIterator($this->categories); | ||
} | ||
} | ||
|
||
class Category { | ||
public function getName(): string | ||
{ | ||
return ''; | ||
} | ||
} |