Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef63534

Browse files
committed
Strict iterator_to_array, do not overwite duplicate keys
1 parent 2091311 commit ef63534

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `&&` and `||`.
1010
* Functions `in_array` and `array_search` must be called with third parameter `$strict` set to `true` to search values with matching types only.
11+
* Function `iterator_to_array` must be called with second parameter `$use_keys` set to `false` to not overwite duplicate iterator keys in the result.
1112
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
1213
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
1314
* Statically declared methods are called statically.

‎src/Rules/StrictCalls/StrictFunctionCallsRule.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
class StrictFunctionCallsRule implements \PHPStan\Rules\Rule
66
{
77

8-
/** @var int[] */
8+
/** @var array<string, array> */
99
private $functionArguments = [
10-
'in_array' => 2,
11-
'array_search' => 2,
10+
'in_array' => [2, true],
11+
'array_search' => [2, true],
12+
'iterator_to_array' => [1, false],
1213
];
1314

1415
/** @var \PHPStan\Broker\Broker */
@@ -44,8 +45,8 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop
4445
return [];
4546
}
4647

47-
$argumentPosition = $this->functionArguments[$functionName];
48-
$message = sprintf('Call to function %s() requires parameter #%d to be true.', $functionName, $argumentPosition + 1);
48+
$argumentPosition = $this->functionArguments[$functionName][0];
49+
$message = sprintf('Call to function %s() requires parameter #%d to be %s.', $functionName, $argumentPosition + 1, $this->functionArguments[$functionName][1] ? 'true' : 'false');
4950
if (!array_key_exists($argumentPosition, $node->args)) {
5051
return [$message];
5152
}

‎tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function testRule(): void
3939
'Call to function array_search() requires parameter #3 to be true.',
4040
13,
4141
],
42+
[
43+
'Call to function iterator_to_array() requires parameter #2 to be false.',
44+
18,
45+
],
46+
[
47+
'Call to function iterator_to_array() requires parameter #2 to be false.',
48+
20,
49+
],
4250
]);
4351
}
4452

‎tests/Rules/StrictCalls/data/strict-calls.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414

1515
$dynamicCall = 'foo';
1616
$dynamicCall();
17+
18+
iterator_to_array(new \ArrayIterator([]), false);
19+
iterator_to_array(new \ArrayIterator([]), true);
20+
iterator_to_array(new \ArrayIterator([]));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /