Skip to content

Commit a9851db

Browse files
schlndhondrejmirtes
authored andcommitted
fix iterator_to_array, iterator_count for PHP 8.2
1 parent 42f6c2e commit a9851db

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

resources/functionMap_php82delta.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
return [
2323
'new' => [
24+
'iterator_count' => ['0|positive-int', 'iterator'=>'iterable'],
25+
'iterator_to_array' => ['array', 'iterator'=>'iterable', 'use_keys='=>'bool'],
2426
'str_split' => ['list<string>', 'str'=>'string', 'split_length='=>'positive-int'],
2527
],
2628
'old' => [

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,4 +1512,41 @@ public function testNamedParametersForMultiVariantFunctions(): void
15121512
$this->analyse([__DIR__ . '/data/call-to-function-named-params-multivariant.php'], []);
15131513
}
15141514

1515+
public function testBug9793(): void
1516+
{
1517+
$errors = [];
1518+
1519+
if (PHP_VERSION_ID < 80200) {
1520+
$errors = [
1521+
[
1522+
'Parameter #1 $iterator of function iterator_to_array expects Traversable, array<stdClass> given.',
1523+
13,
1524+
],
1525+
[
1526+
'Parameter #1 $iterator of function iterator_to_array expects Traversable, array<stdClass>|Iterator<mixed, stdClass> given.',
1527+
14,
1528+
],
1529+
[
1530+
'Parameter #1 $iterator of function iterator_count expects Traversable, array<stdClass> given.',
1531+
15,
1532+
],
1533+
[
1534+
'Parameter #1 $iterator of function iterator_count expects Traversable, array<stdClass>|Iterator<mixed, stdClass> given.',
1535+
16,
1536+
],
1537+
];
1538+
}
1539+
1540+
$errors[] = [
1541+
'Parameter #1 $iterator of function iterator_apply expects Traversable, array<stdClass> given.',
1542+
17,
1543+
];
1544+
$errors[] = [
1545+
'Parameter #1 $iterator of function iterator_apply expects Traversable, array<stdClass>|Iterator<mixed, stdClass> given.',
1546+
18,
1547+
];
1548+
1549+
$this->analyse([__DIR__ . '/data/bug-9793.php'], $errors);
1550+
}
1551+
15151552
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug9793;
6+
7+
/**
8+
* @param array<\stdClass> $arr
9+
* @param \Iterator<\stdClass>|array<\stdClass> $itOrArr
10+
*/
11+
function foo(array $arr, $itOrArr): void
12+
{
13+
\iterator_to_array($arr);
14+
\iterator_to_array($itOrArr);
15+
echo \iterator_count($arr);
16+
echo \iterator_count($itOrArr);
17+
\iterator_apply($arr, fn ($x) => $x);
18+
\iterator_apply($itOrArr, fn ($x) => $x);
19+
}

0 commit comments

Comments
 (0)