Skip to content

Commit 36d7142

Browse files
kukulichondrejmirtes
authored andcommitted
StrictFunctionCallsRule: more functions with $strict parameter
1 parent da3f532 commit 36d7142

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/Rules/StrictCalls/StrictFunctionCallsRule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class StrictFunctionCallsRule implements \PHPStan\Rules\Rule
99
private $functionArguments = [
1010
'in_array' => 2,
1111
'array_search' => 2,
12+
'base64_decode' => 1,
13+
'array_keys' => 2,
1214
];
1315

1416
/** @var \PHPStan\Broker\Broker */
@@ -44,6 +46,10 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop
4446
return [];
4547
}
4648

49+
if ($functionName === 'array_keys' && !array_key_exists(1, $node->args)) {
50+
return [];
51+
}
52+
4753
$argumentPosition = $this->functionArguments[$functionName];
4854
$message = sprintf('Call to function %s() requires parameter #%d to be true.', $functionName, $argumentPosition + 1);
4955
if (!array_key_exists($argumentPosition, $node->args)) {

tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ public function testRule(): void
3939
'Call to function array_search() requires parameter #3 to be true.',
4040
13,
4141
],
42+
[
43+
'Call to function base64_decode() requires parameter #2 to be true.',
44+
15,
45+
],
46+
[
47+
'Call to function base64_decode() requires parameter #2 to be true.',
48+
17,
49+
],
50+
[
51+
'Call to function base64_decode() requires parameter #2 to be true.',
52+
18,
53+
],
54+
[
55+
'Call to function array_keys() requires parameter #3 to be true.',
56+
20,
57+
],
58+
[
59+
'Call to function array_keys() requires parameter #3 to be true.',
60+
22,
61+
],
62+
[
63+
'Call to function array_keys() requires parameter #3 to be true.',
64+
23,
65+
],
4266
]);
4367
}
4468

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,16 @@
1212
array_search(1, [1, 2, 3], false);
1313
Array_Search(1, [1, 2, 3]);
1414

15+
base64_decode('abcd');
16+
base64_decode('abcd', true);
17+
base64_decode('abcd', false);
18+
Base64_Decode('abcd');
19+
20+
array_keys([1, 2, 3], 1);
21+
array_keys([1, 2, 3], 1, true);
22+
array_keys([1, 2, 3], 1, false);
23+
Array_Keys([1, 2, 3], 1);
24+
array_keys([1, 2, 3]);
25+
1526
$dynamicCall = 'foo';
1627
$dynamicCall();

0 commit comments

Comments
 (0)