Skip to content

Fix IntersectionType::isIterableOnce #4106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
use function array_filter;
use function array_intersect_key;
use function array_map;
use function array_shift;
Expand Down Expand Up @@ -599,7 +600,10 @@ public function isIterable(): TrinaryLogic

public function isIterableAtLeastOnce(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isIterableAtLeastOnce());
return $this->intersectResults(
static fn (Type $type): TrinaryLogic => $type->isIterableAtLeastOnce(),
static fn (Type $type): bool => !$type->isIterable()->no(),
);
}

public function getArraySize(): Type
Expand Down Expand Up @@ -1243,10 +1247,22 @@ public function getFiniteTypes(): array

/**
* @param callable(Type $type): TrinaryLogic $getResult
* @param (callable(Type $type): bool)|null $filter
*/
private function intersectResults(callable $getResult): TrinaryLogic
{
return TrinaryLogic::lazyMaxMin($this->types, $getResult);
private function intersectResults(
callable $getResult,
?callable $filter = null,
): TrinaryLogic
{
$types = $this->types;
if ($filter !== null) {
$types = array_filter($types, $filter);
}
if (count($types) === 0) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::lazyMaxMin($types, $getResult);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Arrays/DeadForeachRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public function testBug8292(): void
$this->analyse([__DIR__ . '/data/bug-8292.php'], []);
}

public function testBug13248(): void
{
$this->analyse([__DIR__ . '/data/bug-13248.php'], []);
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-13248.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bug13248;

use ArrayIterator;
use IteratorAggregate;
use Traversable;

class X
{
}

/**
* @implements IteratorAggregate<int, string>
*/
class Y extends X implements IteratorAggregate
{
/**
* @return ArrayIterator<int<0, 2>, 'a'|'b'|'c'>
*/
public function getIterator(): Traversable
{
return new ArrayIterator(['a', 'b', 'c']);
}
}

/**
* @return X&Traversable<int, string>
*/
function y(): X
{
return new Y();
}

foreach (y() as $item) { // hm?
echo $item . PHP_EOL;
}
Loading