Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3401,11 +3401,18 @@ public function processArgs(
$this->storeBeforeScope($storage, $arg->value, $scopeToPass);
} else {
$exprType = $scope->getType($arg->value);
$enterExpressionAssignForByRef = $assignByReference && $arg->value instanceof ArrayDimFetch && $arg->value->dim === null;
if ($enterExpressionAssignForByRef) {
$scopeToPass = $scopeToPass->enterExpressionAssign($arg->value);
}
$exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context->enterDeep());
$throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints());
$isAlwaysTerminating = $isAlwaysTerminating || $exprResult->isAlwaysTerminating();
$scope = $exprResult->getScope();
if ($enterExpressionAssignForByRef) {
$scope = $scope->exitExpressionAssign($arg->value);
}
$hasYield = $hasYield || $exprResult->hasYield();

if ($exprType->isCallable()->yes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ protected function getRule(): Rule
return new OffsetAccessWithoutDimForReadingRule();
}

public function testBug5290(): void
{
$this->analyse([__DIR__ . '/data/bug-5290.php'], []);
}

public function testOffsetAccessWithoutDimForReading(): void
{
$this->analyse(
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-5290.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug5290;

function set(?bool &$value): void {
$value = true;
}

$array = [];
set($array[]);

var_dump($array);

// Also test with closures and anonymous functions
(function (&$ref) {})($array[]);

class Foo {
public function bar(?bool &$value): void {
$value = true;
}
}

$foo = new Foo();
$foo->bar($array[]);

// Nested array dim fetch with by-ref parameter
set($array[1][]);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$firstElement = &$array[];
(function ($ref) {})($array[]);
//(function (&$ref) {})($array[]); // Should work but doesn't
(function (&$ref) {})($array[]);

// Technically works but makes no sense
$array[] += 20;
Expand Down
Loading