Skip to content

Commit

Permalink
Allow passing undefined variable into by-ref parameter with PHPDoc-on…
Browse files Browse the repository at this point in the history
…ly nullable type
  • Loading branch information
ondrejmirtes committed Apr 14, 2024
1 parent 7f8f9cc commit 7961f7a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -3798,15 +3798,24 @@ private function processArgs(
$assignByReference = false;
$parameter = null;
$parameterType = null;
$parameterNativeType = null;
if (isset($parameters) && $parametersAcceptor !== null) {
if (isset($parameters[$i])) {
$assignByReference = $parameters[$i]->passedByReference()->createsNewVariable();
$parameterType = $parameters[$i]->getType();

if ($parameters[$i] instanceof ParameterReflectionWithPhpDocs) {
$parameterNativeType = $parameters[$i]->getNativeType();
}
$parameter = $parameters[$i];
} elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) {
$lastParameter = $parameters[count($parameters) - 1];
$assignByReference = $lastParameter->passedByReference()->createsNewVariable();
$parameterType = $lastParameter->getType();

if ($lastParameter instanceof ParameterReflectionWithPhpDocs) {
$parameterNativeType = $lastParameter->getNativeType();
}
$parameter = $lastParameter;
}
}
Expand All @@ -3822,7 +3831,7 @@ private function processArgs(
}
if (
$isBuiltin
|| ($parameterType === null || !$parameterType->isNull()->no())
|| ($parameterNativeType === null || !$parameterNativeType->isNull()->no())
) {
$scope = $this->lookForSetAllowedUndefinedExpressions($scope, $arg->value);
$lookForUnset = true;
Expand Down
Expand Up @@ -34,3 +34,39 @@ public function test()
}

}

class FooPhpDocs
{

/**
* @param mixed $test
*/
public function doFooMixedType(&$test)
{

}

/**
* @param int $test
*/
public function doFooIntType(&$test)
{

}

/**
* @param int|null $test
*/
public function doFooNullableType(&$test)
{

}

public function test()
{
$this->doFooMixedType($two);
$this->doFooIntType($three);
$this->doFooNullableType($four);
}

}

0 comments on commit 7961f7a

Please sign in to comment.