Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.10.x' into 1.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 14, 2024
2 parents 5c3a711 + 7f8f9cc commit b356af1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4242,6 +4242,7 @@ private function processArgs(
foreach ($args as $i => $arg) {
$assignByReference = false;
$parameter = null;
$parameterType = null;
if (isset($parameters) && $parametersAcceptor !== null) {
if (isset($parameters[$i])) {
$assignByReference = $parameters[$i]->passedByReference()->createsNewVariable();
Expand All @@ -4255,9 +4256,22 @@ private function processArgs(
}
}

$lookForUnset = false;
if ($assignByReference) {
if ($arg->value instanceof Variable) {
$scope = $this->lookForSetAllowedUndefinedExpressions($scope, $arg->value);
$isBuiltin = false;
if ($calleeReflection instanceof FunctionReflection && $calleeReflection->isBuiltin()) {
$isBuiltin = true;
} elseif ($calleeReflection instanceof ExtendedMethodReflection && $calleeReflection->getDeclaringClass()->isBuiltin()) {
$isBuiltin = true;
}
if (
$isBuiltin
|| ($parameterType === null || !$parameterType->isNull()->no())
) {
$scope = $this->lookForSetAllowedUndefinedExpressions($scope, $arg->value);
$lookForUnset = true;
}
}
}

Expand Down Expand Up @@ -4365,10 +4379,9 @@ private function processArgs(
}
}
}
if ($assignByReference) {
if ($arg->value instanceof Variable) {
$scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $arg->value);
}

if ($assignByReference && $lookForUnset) {
$scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $arg->value);
}

if ($calleeReflection !== null) {
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1042,4 +1042,22 @@ public function testBug10418(): void
$this->analyse([__DIR__ . '/data/bug-10418.php'], []);
}

public function testPassByReferenceIntoNotNullable(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/pass-by-reference-into-not-nullable.php'], [
[
'Undefined variable: $three',
32,
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php // lint >= 8.0

namespace PassByReferenceIntoNotNullable;

class Foo
{

public function doFooNoType(&$test)
{

}

public function doFooMixedType(mixed &$test)
{

}

public function doFooIntType(int &$test)
{

}

public function doFooNullableType(?int &$test)
{

}

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

}

0 comments on commit b356af1

Please sign in to comment.