Skip to content
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

Issue 10626: Do type checks on pass by reference parameters for non-builtin functions and methods. #2939

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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/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);
Loading