Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* `array_search` (3rd parameter)
* `array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)
* `base64_decode` (2nd parameter)
* Function `iterator_to_array` must be called with second parameter `$use_keys` set to `false` to not overwite duplicate iterator keys in the result.
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
* Statically declared methods are called statically.
Expand Down
19 changes: 10 additions & 9 deletions src/Rules/StrictCalls/StrictFunctionCallsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
class StrictFunctionCallsRule implements \PHPStan\Rules\Rule
{

/** @var int[] */
/** @var array<string, array> */
private $functionArguments = [
'in_array' => 2,
'array_search' => 2,
'base64_decode' => 1,
'array_keys' => 2,
'in_array' => [2, true],
'array_search' => [2, true],
'base64_decode' => [1, true],
'array_keys' => [2, true],
'iterator_to_array' => [1, false],
];

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

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

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

return [];
Expand Down
8 changes: 8 additions & 0 deletions tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function testRule(): void
'Call to function array_keys() requires parameter #3 to be true.',
31,
],
[
'Call to function iterator_to_array() requires parameter #2 to be false.',
34,
],
[
'Call to function iterator_to_array() requires parameter #2 to be set.',
35,
],
]);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/StrictCalls/data/strict-calls.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
/** @var bool $bool */
$bool = doFoo();
array_keys([1, 2, 3], 1, $bool);

iterator_to_array(new \ArrayIterator([]), false);
iterator_to_array(new \ArrayIterator([]), true);
iterator_to_array(new \ArrayIterator([]));