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 ce5460e

Browse files
committed
Strict iterator_to_array, do not overwite duplicate keys
1 parent e5140e0 commit ce5460e

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* `array_search` (3rd parameter)
1313
* `array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)
1414
* `base64_decode` (2nd parameter)
15+
* Function `iterator_to_array` must be called with second parameter `$use_keys` set to `false` to not overwite duplicate iterator keys in the result.
1516
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
1617
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
1718
* Statically declared methods are called statically.

‎src/Rules/StrictCalls/StrictFunctionCallsRule.php‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
class StrictFunctionCallsRule implements \PHPStan\Rules\Rule
88
{
99

10-
/** @var int[] */
10+
/** @var array<string, array> */
1111
private $functionArguments = [
12-
'in_array' => 2,
13-
'array_search' => 2,
14-
'base64_decode' => 1,
15-
'array_keys' => 2,
12+
'in_array' => [2, true],
13+
'array_search' => [2, true],
14+
'base64_decode' => [1, true],
15+
'array_keys' => [2, true],
16+
'iterator_to_array' => [1, false],
1617
];
1718

1819
/** @var \PHPStan\Broker\Broker */
@@ -52,15 +53,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop
5253
return [];
5354
}
5455

55-
$argumentPosition = $this->functionArguments[$functionName];
56+
$argumentPosition = $this->functionArguments[$functionName][0];
5657
if (!array_key_exists($argumentPosition, $node->args)) {
5758
return [sprintf('Call to function %s() requires parameter #%d to be set.', $functionName, $argumentPosition + 1)];
5859
}
5960

6061
$argumentType = $scope->getType($node->args[$argumentPosition]->value);
61-
$trueType = new ConstantBooleanType(true);
62-
if (!$trueType->isSuperTypeOf($argumentType)->yes()) {
63-
return [sprintf('Call to function %s() requires parameter #%d to be true.', $functionName, $argumentPosition + 1)];
62+
$strictType = new ConstantBooleanType((bool) $this->functionArguments[$functionName][1]);
63+
if (!$strictType->isSuperTypeOf($argumentType)->yes()) {
64+
return [sprintf('Call to function %s() requires parameter #%d to be %s.', $functionName, $argumentPosition + 1, (bool) $this->functionArguments[$functionName][1] ? 'true' : 'false')];
6465
}
6566

6667
return [];

‎tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function testRule(): void
6767
'Call to function array_keys() requires parameter #3 to be true.',
6868
31,
6969
],
70+
[
71+
'Call to function iterator_to_array() requires parameter #2 to be false.',
72+
34,
73+
],
74+
[
75+
'Call to function iterator_to_array() requires parameter #2 to be set.',
76+
35,
77+
],
7078
]);
7179
}
7280

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@
2929
/** @var bool $bool */
3030
$bool = doFoo();
3131
array_keys([1, 2, 3], 1, $bool);
32+
33+
iterator_to_array(new \ArrayIterator([]), false);
34+
iterator_to_array(new \ArrayIterator([]), true);
35+
iterator_to_array(new \ArrayIterator([]));

0 commit comments

Comments
(0)

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