Skip to content

Commit 57ed5a4

Browse files
phpstan-botclaude
andcommitted
Use TIterator as RegexIterator constructor param type to enable full generic inference
Change @param from Iterator<TKey, TValue> to TIterator in RegexIterator's constructor stub, matching the pattern already used by IteratorIterator. This allows PHPStan to infer TIterator from the actual argument type (e.g. FilesystemIterator) rather than falling back to the bound Traversable<TKey, TValue>. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4a0950f commit 57ed5a4

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

stubs/iterable.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class RegexIterator extends FilterIterator {
440440
const USE_KEY = 1 ;
441441

442442
/**
443-
* @param Iterator<TKey, TValue> $iterator
443+
* @param TIterator $iterator
444444
* @param self::MATCH|self::GET_MATCH|self::ALL_MATCHES|self::SPLIT|self::REPLACE $mode
445445
*/
446446
public function __construct(Iterator $iterator, string $regex, int $mode = self::MATCH, int $flags = 0, int $preg_flags = 0) {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Bug13325Nsrt;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param \Iterator<int, string> $iterator
12+
*/
13+
public function doFoo(\Iterator $iterator): void
14+
{
15+
$regexIterator = new \RegexIterator($iterator, '/pattern/');
16+
assertType('RegexIterator<int, string, Iterator<int, string>>', $regexIterator);
17+
}
18+
19+
public function doBar(): void
20+
{
21+
$regexIterator = new \RegexIterator(new \FilesystemIterator('foo'), '/pattern/');
22+
assertType('RegexIterator<mixed, mixed, FilesystemIterator>', $regexIterator);
23+
}
24+
25+
}

tests/PHPStan/Rules/Methods/data/bug-13325.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44

55
namespace Bug13325;
66

7+
use FilesystemIterator;
78
use Iterator;
89
use RegexIterator;
9-
use Traversable;
1010

1111
final class Foo
1212
{
13-
/** @return RegexIterator<mixed, mixed, Traversable<mixed, mixed>> */
13+
/** @return RegexIterator<mixed, mixed, Iterator> */
1414
public function __invoke(Iterator $iterator): RegexIterator
1515
{
1616
return new RegexIterator($iterator, 'string');
1717
}
18+
19+
/** @return RegexIterator<mixed, mixed, FilesystemIterator> */
20+
public function createRegionIterator(): RegexIterator
21+
{
22+
return new RegexIterator(
23+
new FilesystemIterator('foo'),
24+
'/bar/',
25+
);
26+
}
1827
}

0 commit comments

Comments
 (0)