Skip to content

Commit 542d903

Browse files
committed
Fix: False positive for invalid Generator return type
1 parent b53f4bf commit 542d903

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,13 @@ private function checkInvalidGenerator(
347347
): bool {
348348

349349
$hasGenerator = false;
350+
$hasIterator = false;
350351
while (!$hasGenerator && $returnTypes) {
351352
$returnType = explode('&', rtrim(ltrim(array_shift($returnTypes), '('), ')'));
352353
$hasGenerator = in_array('Generator', $returnType, true)
353-
|| in_array('\Generator', $returnType, true)
354+
|| in_array('\Generator', $returnType, true);
355+
$hasIterator = $hasIterator
356+
|| $hasGenerator
354357
|| in_array('Traversable', $returnType, true)
355358
|| in_array('\Traversable', $returnType, true)
356359
|| in_array('Iterator', $returnType, true)
@@ -383,7 +386,7 @@ private function checkInvalidGenerator(
383386
return true;
384387
}
385388

386-
if (!$hasGenerator && ($yieldCount > 0)) {
389+
if (!$hasIterator && ($yieldCount > 0)) {
387390
$file->addError(
388391
'Return type does not contain "Generator" but yield found in the function body',
389392
$position,

tests/fixtures/return-type-declaration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
<?php
22
// @phpcsSniff Inpsyde.CodeQuality.ReturnTypeDeclaration
33

4+
use Brain\Assets\Enqueue\Enqueue;
45
use Psr\Container\ContainerInterface as PsrContainer;
56

7+
class FooIterator implements IteratorAggregate
8+
{
9+
private array $collection = [];
10+
11+
/**
12+
* @return Iterator<int, Foo>
13+
*/
14+
public function getIterator(): \Iterator
15+
{
16+
return new ArrayIterator($this->collection);
17+
}
18+
}
19+
620
function returnMixed(): mixed
721
{
822
return null;

0 commit comments

Comments
 (0)