diff --git a/src/PhpStan/FunctionStrictModeRule.php b/src/PhpStan/FunctionStrictModeRule.php index 40c5fd7..d0f7f8c 100644 --- a/src/PhpStan/FunctionStrictModeRule.php +++ b/src/PhpStan/FunctionStrictModeRule.php @@ -15,7 +15,6 @@ use PhpParser\Node; use PHPStan\Analyser\Scope; -use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\Type; @@ -48,14 +47,30 @@ public function processNode(Node $node, Scope $scope): array return []; } - if (3 === \count($node->args)) { - if (!$node->args[2] instanceof Node\Arg) { - return []; - } + if (!$node->args[0] instanceof Node\Arg || !$node->args[1] instanceof Node\Arg) { + return []; + } + + $isThreeArgs = 3 === \count($node->args); + $needleType = $scope->getType($node->args[0]->value); + $isNeedleScalarType = $this->isSafeToCompareNonStrict($needleType); + + if ($isThreeArgs && !$node->args[2] instanceof Node\Arg) { + return []; + } + if (!$isThreeArgs && $isNeedleScalarType) { + return [ + RuleErrorBuilder::message('The function in_array must be used in strict mode.') + ->identifier('functionCall.strictMode') + ->build(), + ]; + } + + if ($isThreeArgs && $node->args[2] instanceof Node\Arg) { $modeType = $scope->getType($node->args[2]->value); - if ($modeType->isFalse()->yes()) { + if ($isNeedleScalarType && $modeType->isFalse()->yes()) { return [ RuleErrorBuilder::message('The function in_array must be used in strict mode.') ->identifier('functionCall.strictMode') @@ -63,39 +78,16 @@ public function processNode(Node $node, Scope $scope): array ]; } - return []; - } - - return $this->analyzeArgs($node, $scope); - } - - /** - * Analyze function arguments types - * - * @param Node\Expr\FuncCall $node - * @param Scope $scope - * - * @return list - */ - private function analyzeArgs(Node\Expr\FuncCall $node, Scope $scope): array - { - if (!$node->args[0] instanceof Node\Arg || !$node->args[1] instanceof Node\Arg) { - return []; - } - - $needleType = $scope->getType($node->args[0]->value); - $isNeedleScalarType = $this->isSafeToCompareNonStrict($needleType); - - if (!$isNeedleScalarType) { - return []; + if (!$isNeedleScalarType && $modeType->isTrue()->yes()) { + return [ + RuleErrorBuilder::message('You don\'t need to use strict mode when comparison objects.') + ->identifier('functionCall.strictMode') + ->build(), + ]; + } } - return [ - RuleErrorBuilder::message('The function in_array must be used in strict mode.') - ->identifier('functionCall.strictMode') - ->build(), - ]; - + return []; } private function isSafeToCompareNonStrict(Type $argType): bool diff --git a/tests/PhpStan/FunctionStrictModeRuleTest.php b/tests/PhpStan/FunctionStrictModeRuleTest.php index 3fe2ef7..721eec1 100644 --- a/tests/PhpStan/FunctionStrictModeRuleTest.php +++ b/tests/PhpStan/FunctionStrictModeRuleTest.php @@ -31,11 +31,14 @@ public function shouldSuccessProcess(): void $this->analyse( [__DIR__.'/Resources/function-strict-mode.php'], [ - ['The function in_array must be used in strict mode.', 16], - ['The function in_array must be used in strict mode.', 17], - ['The function in_array must be used in strict mode.', 18], - ['The function in_array must be used in strict mode.', 21], - ['The function in_array must be used in strict mode.', 23], + ['The function in_array must be used in strict mode.', 26], + ['The function in_array must be used in strict mode.', 27], + ['The function in_array must be used in strict mode.', 28], + ['The function in_array must be used in strict mode.', 31], + ['The function in_array must be used in strict mode.', 33], + ['The function in_array must be used in strict mode.', 35], + ['You don\'t need to use strict mode when comparison objects.', 36], + ['You don\'t need to use strict mode when comparison objects.', 56], ], ); } diff --git a/tests/PhpStan/Resources/function-strict-mode.php b/tests/PhpStan/Resources/function-strict-mode.php index ebd750d..2f0d92d 100644 --- a/tests/PhpStan/Resources/function-strict-mode.php +++ b/tests/PhpStan/Resources/function-strict-mode.php @@ -1,5 +1,15 @@ foo, [SomeEnum::Foo, SomeEnum::Bar], true); + } +}