From 1027243a0bb1df3cfd1d37f84ada3627560f2f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Wed, 7 Feb 2018 15:02:06 +0100 Subject: [PATCH 1/3] StrictFunctionCallsRule: more functions with $strict parameter --- .../StrictCalls/StrictFunctionCallsRule.php | 6 +++++ .../StrictFunctionCallsRuleTest.php | 24 +++++++++++++++++++ tests/Rules/StrictCalls/data/strict-calls.php | 11 +++++++++ 3 files changed, 41 insertions(+) diff --git a/src/Rules/StrictCalls/StrictFunctionCallsRule.php b/src/Rules/StrictCalls/StrictFunctionCallsRule.php index 984eb834..6b11d9b7 100644 --- a/src/Rules/StrictCalls/StrictFunctionCallsRule.php +++ b/src/Rules/StrictCalls/StrictFunctionCallsRule.php @@ -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,6 +46,10 @@ 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)) { diff --git a/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php b/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php index 66f5462b..06a7a11f 100644 --- a/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php +++ b/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php @@ -39,6 +39,30 @@ public function testRule(): void 'Call to function array_search() requires parameter #3 to be true.', 13, ], + [ + 'Call to function base64_decode() requires parameter #2 to be true.', + 15, + ], + [ + 'Call to function base64_decode() requires parameter #2 to be true.', + 17, + ], + [ + 'Call to function base64_decode() requires parameter #2 to be true.', + 18, + ], + [ + 'Call to function array_keys() requires parameter #3 to be true.', + 20, + ], + [ + 'Call to function array_keys() requires parameter #3 to be true.', + 22, + ], + [ + 'Call to function array_keys() requires parameter #3 to be true.', + 23, + ], ]); } diff --git a/tests/Rules/StrictCalls/data/strict-calls.php b/tests/Rules/StrictCalls/data/strict-calls.php index 039346a6..8af7099d 100644 --- a/tests/Rules/StrictCalls/data/strict-calls.php +++ b/tests/Rules/StrictCalls/data/strict-calls.php @@ -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(); From 9dcbc65cc348a11254c5f00bba1a8baa973efd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Wed, 7 Feb 2018 15:05:23 +0100 Subject: [PATCH 2/3] StrictFunctionCallsRule: distinguish between "parameter is not true" and "parameter is missing" --- .../StrictCalls/StrictFunctionCallsRule.php | 5 ++--- .../StrictCalls/StrictFunctionCallsRuleTest.php | 16 ++++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Rules/StrictCalls/StrictFunctionCallsRule.php b/src/Rules/StrictCalls/StrictFunctionCallsRule.php index 6b11d9b7..384a7759 100644 --- a/src/Rules/StrictCalls/StrictFunctionCallsRule.php +++ b/src/Rules/StrictCalls/StrictFunctionCallsRule.php @@ -51,14 +51,13 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop } $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 []; diff --git a/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php b/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php index 06a7a11f..d51b3471 100644 --- a/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php +++ b/tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php @@ -16,7 +16,7 @@ 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, ], [ @@ -24,11 +24,11 @@ public function testRule(): void 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, ], [ @@ -36,11 +36,11 @@ public function testRule(): void 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 true.', + 'Call to function base64_decode() requires parameter #2 to be set.', 15, ], [ @@ -48,11 +48,11 @@ public function testRule(): void 17, ], [ - 'Call to function base64_decode() requires parameter #2 to be true.', + 'Call to function base64_decode() requires parameter #2 to be set.', 18, ], [ - 'Call to function array_keys() requires parameter #3 to be true.', + 'Call to function array_keys() requires parameter #3 to be set.', 20, ], [ @@ -60,7 +60,7 @@ public function testRule(): void 22, ], [ - 'Call to function array_keys() requires parameter #3 to be true.', + 'Call to function array_keys() requires parameter #3 to be set.', 23, ], ]); From c8b86d7d9d1c1cfb37503b444737b41d85b8a4a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Wed, 7 Feb 2018 15:07:56 +0100 Subject: [PATCH 3/3] DynamicCallOnStaticMethodsRule: fixed PHPStan --- src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php b/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php index 306411ac..48fa9bc9 100644 --- a/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php +++ b/src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php @@ -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 []; }