From d3ad5f732903279c5b4b9c744547e8f0ec7476f9 Mon Sep 17 00:00:00 2001 From: Lincoln Maskey <1082557+ljmaskey@users.noreply.github.com> Date: Fri, 23 Feb 2024 18:14:52 -0400 Subject: [PATCH] Do type checks on pass by reference parameters for non-builtin functions and methods. Initially, all pass by reference parameters were skipped for type checking. This change continues to skip those checks for builtin PHP methods, but does check parameters for userland methods. --- src/Rules/FunctionCallParametersCheck.php | 2 +- .../CallToFunctionParametersRuleTest.php | 17 +++++++++++++++++ .../PHPStan/Rules/Functions/data/bug-10626.php | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-10626.php diff --git a/src/Rules/FunctionCallParametersCheck.php b/src/Rules/FunctionCallParametersCheck.php index 4d4a29c713..9f73d06d6b 100644 --- a/src/Rules/FunctionCallParametersCheck.php +++ b/src/Rules/FunctionCallParametersCheck.php @@ -278,7 +278,7 @@ public function check( if ($this->checkArgumentTypes) { $parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType()); - if (!$parameter->passedByReference()->createsNewVariable()) { + if (!$parameter->passedByReference()->createsNewVariable() || !$isBuiltin) { $accepts = $this->ruleLevelHelper->acceptsWithReason($parameterType, $argumentValueType, $scope->isDeclareStrictTypes()); if (!$accepts->result) { diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index e00f619105..3c78353b04 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -297,6 +297,10 @@ public function testPassingNonVariableToParameterPassedByReference(): void 'Parameter #1 $array of function reset expects array|object, null given.', 39, ], + [ + 'Parameter #1 $s of function PassedByReference\bar expects string, int given.', + 48, + ], ]); } @@ -1623,4 +1627,17 @@ public function testDiscussion10454(): void ]); } + public function testBug10626(): void + { + $this->analyse([__DIR__ . '/data/bug-10626.php'], [ + [ + 'Parameter #1 $value of function PassedByReference\intByValue expects int, string given.', + 16, + ], + [ + 'Parameter #1 $value of function PassedByReference\intByReference expects int, string given.', + 17, + ], + ]); + } } diff --git a/tests/PHPStan/Rules/Functions/data/bug-10626.php b/tests/PHPStan/Rules/Functions/data/bug-10626.php new file mode 100644 index 0000000000..aa9659b280 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-10626.php @@ -0,0 +1,17 @@ +