Skip to content

StrictFunctionCallsRule improvements #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2018
Merged
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
2 changes: 1 addition & 1 deletion src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ public function processNode(Node $node, Scope $scope): array
''
)->getType();

if ($type instanceof ErrorType || !$type->canCallMethods() || !$type->hasMethod($name)) {
if ($type instanceof ErrorType || !$type->canCallMethods()->yes() || !$type->hasMethod($name)) {
return [];
}

11 changes: 8 additions & 3 deletions src/Rules/StrictCalls/StrictFunctionCallsRule.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ class StrictFunctionCallsRule implements \PHPStan\Rules\Rule
private $functionArguments = [
'in_array' => 2,
'array_search' => 2,
'base64_decode' => 1,
'array_keys' => 2,
];

/** @var \PHPStan\Broker\Broker */
@@ -44,15 +46,18 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop
return [];
}

if ($functionName === 'array_keys' && !array_key_exists(1, $node->args)) {
return [];
}

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

$argumentType = $scope->getType($node->args[$argumentPosition]->value);
if (!$argumentType instanceof \PHPStan\Type\TrueBooleanType) {
return [$message];
return [sprintf('Call to function %s() requires parameter #%d to be true.', $functionName, $argumentPosition + 1)];
}

return [];
32 changes: 28 additions & 4 deletions tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php
Original file line number Diff line number Diff line change
@@ -16,29 +16,53 @@ public function testRule(): void
{
$this->analyse([__DIR__ . '/data/strict-calls.php'], [
[
'Call to function in_array() requires parameter #3 to be true.',
'Call to function in_array() requires parameter #3 to be set.',
5,
],
[
'Call to function in_array() requires parameter #3 to be true.',
7,
],
[
'Call to function in_array() requires parameter #3 to be true.',
'Call to function in_array() requires parameter #3 to be set.',
8,
],
[
'Call to function array_search() requires parameter #3 to be true.',
'Call to function array_search() requires parameter #3 to be set.',
10,
],
[
'Call to function array_search() requires parameter #3 to be true.',
12,
],
[
'Call to function array_search() requires parameter #3 to be true.',
'Call to function array_search() requires parameter #3 to be set.',
13,
],
[
'Call to function base64_decode() requires parameter #2 to be set.',
15,
],
[
'Call to function base64_decode() requires parameter #2 to be true.',
17,
],
[
'Call to function base64_decode() requires parameter #2 to be set.',
18,
],
[
'Call to function array_keys() requires parameter #3 to be set.',
20,
],
[
'Call to function array_keys() requires parameter #3 to be true.',
22,
],
[
'Call to function array_keys() requires parameter #3 to be set.',
23,
],
]);
}

11 changes: 11 additions & 0 deletions tests/Rules/StrictCalls/data/strict-calls.php
Original file line number Diff line number Diff line change
@@ -12,5 +12,16 @@
array_search(1, [1, 2, 3], false);
Array_Search(1, [1, 2, 3]);

base64_decode('abcd');
base64_decode('abcd', true);
base64_decode('abcd', false);
Base64_Decode('abcd');

array_keys([1, 2, 3], 1);
array_keys([1, 2, 3], 1, true);
array_keys([1, 2, 3], 1, false);
Array_Keys([1, 2, 3], 1);
array_keys([1, 2, 3]);

$dynamicCall = 'foo';
$dynamicCall();