Skip to content

Commit 7961f7a

Browse files
committed
Allow passing undefined variable into by-ref parameter with PHPDoc-only nullable type
1 parent 7f8f9cc commit 7961f7a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3798,15 +3798,24 @@ private function processArgs(
37983798
$assignByReference = false;
37993799
$parameter = null;
38003800
$parameterType = null;
3801+
$parameterNativeType = null;
38013802
if (isset($parameters) && $parametersAcceptor !== null) {
38023803
if (isset($parameters[$i])) {
38033804
$assignByReference = $parameters[$i]->passedByReference()->createsNewVariable();
38043805
$parameterType = $parameters[$i]->getType();
3806+
3807+
if ($parameters[$i] instanceof ParameterReflectionWithPhpDocs) {
3808+
$parameterNativeType = $parameters[$i]->getNativeType();
3809+
}
38053810
$parameter = $parameters[$i];
38063811
} elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) {
38073812
$lastParameter = $parameters[count($parameters) - 1];
38083813
$assignByReference = $lastParameter->passedByReference()->createsNewVariable();
38093814
$parameterType = $lastParameter->getType();
3815+
3816+
if ($lastParameter instanceof ParameterReflectionWithPhpDocs) {
3817+
$parameterNativeType = $lastParameter->getNativeType();
3818+
}
38103819
$parameter = $lastParameter;
38113820
}
38123821
}
@@ -3822,7 +3831,7 @@ private function processArgs(
38223831
}
38233832
if (
38243833
$isBuiltin
3825-
|| ($parameterType === null || !$parameterType->isNull()->no())
3834+
|| ($parameterNativeType === null || !$parameterNativeType->isNull()->no())
38263835
) {
38273836
$scope = $this->lookForSetAllowedUndefinedExpressions($scope, $arg->value);
38283837
$lookForUnset = true;

tests/PHPStan/Rules/Variables/data/pass-by-reference-into-not-nullable.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,39 @@ public function test()
3434
}
3535

3636
}
37+
38+
class FooPhpDocs
39+
{
40+
41+
/**
42+
* @param mixed $test
43+
*/
44+
public function doFooMixedType(&$test)
45+
{
46+
47+
}
48+
49+
/**
50+
* @param int $test
51+
*/
52+
public function doFooIntType(&$test)
53+
{
54+
55+
}
56+
57+
/**
58+
* @param int|null $test
59+
*/
60+
public function doFooNullableType(&$test)
61+
{
62+
63+
}
64+
65+
public function test()
66+
{
67+
$this->doFooMixedType($two);
68+
$this->doFooIntType($three);
69+
$this->doFooNullableType($four);
70+
}
71+
72+
}

0 commit comments

Comments
 (0)