Skip to content

Commit

Permalink
Do type checks on pass by reference parameters for non-builtin functi…
Browse files Browse the repository at this point in the history
…ons 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.
  • Loading branch information
ljmaskey committed Feb 23, 2024
1 parent 0d9e8ea commit d3ad5f7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

Expand Down Expand Up @@ -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,
],
]);
}
}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10626.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace PassedByReference;

function intByValue(int $value): void
{

}

function intByReference(int &$value): void
{

}

$notAnInt = 'not-an-int';
intByValue($notAnInt);
intByReference($notAnInt);

0 comments on commit d3ad5f7

Please sign in to comment.