Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions src/Analyser/ExprHandler/ArrayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
use PHPStan\Node\LiteralArrayItem;
use PHPStan\Node\LiteralArrayNode;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use function array_merge;
use function is_string;

/**
* @implements ExprHandler<Array_>
Expand Down Expand Up @@ -76,7 +77,20 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
}

$scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $arrayItem->value);
$scope = $scope->assignExpression($arrayItem->value, new MixedType(), new MixedType());

if ($arrayItem->value instanceof Expr\Variable && is_string($arrayItem->value->name)) {
$varName = $arrayItem->value->name;
$type = $scope->getType($arrayItem->value);
$nativeType = $scope->getNativeType($arrayItem->value);
// Ensure the variable is defined (PHP creates it if undefined when used by-ref)
$scope = $scope->assignExpression($arrayItem->value, $type, $nativeType);
// Register intertwined relationship
$scope = $scope->assignExpression(
new IntertwinedVariableByReferenceWithExpr($varName, $expr, new Expr\Variable($varName)),
$type,
$nativeType,
);
}
}
$nodeScopeResolver->callNodeCallback($nodeCallback, new LiteralArrayNode($expr, $itemNodes), $scope, $storage);

Expand Down
32 changes: 32 additions & 0 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,38 @@ static function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $contex
);
}

if (
$expr instanceof Assign
&& $expr->var instanceof Variable
&& is_string($expr->var->name)
&& $expr->expr instanceof Expr\Array_
) {
$targetVarName = $expr->var->name;
foreach ($expr->expr->items as $i => $item) {
if (!$item->byRef) {
continue;
}
if (!($item->value instanceof Variable) || !is_string($item->value->name)) {
continue;
}
$refVarName = $item->value->name;
$key = $item->key ?? new Node\Scalar\Int_($i);
$type = $scope->getType($item->value);
$nativeType = $scope->getNativeType($item->value);

// When $refVarName is assigned, update $targetVar[$key]
$scope = $scope->assignExpression(
new IntertwinedVariableByReferenceWithExpr(
$refVarName,
new ArrayDimFetch(new Variable($targetVarName), $key),
new Variable($refVarName),
),
$type,
$nativeType,
);
}
}

$vars = $nodeScopeResolver->getAssignedVariables($expr->var);
if (count($vars) > 0) {
$varChangedScope = false;
Expand Down
24 changes: 24 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3532,6 +3532,30 @@
$scope = $scope->invalidateExpression($arg->value, true);
}
}

if (
!$assignByReference
&& $calleeReflection !== null
&& !$calleeReflection->hasSideEffects()->no()

Check warning on line 3539 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ( !$assignByReference && $calleeReflection !== null - && !$calleeReflection->hasSideEffects()->no() + && $calleeReflection->hasSideEffects()->yes() && $arg->value instanceof Array_ ) { foreach ($arg->value->items as $item) {

Check warning on line 3539 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ( !$assignByReference && $calleeReflection !== null - && !$calleeReflection->hasSideEffects()->no() + && $calleeReflection->hasSideEffects()->yes() && $arg->value instanceof Array_ ) { foreach ($arg->value->items as $item) {
&& $arg->value instanceof Array_
) {
foreach ($arg->value->items as $item) {
if (!$item->byRef) {
continue;
}
if (!($item->value instanceof Variable) || !is_string($item->value->name)) {
continue;
}
$scope = $this->processVirtualAssign(
$scope,
$storage,
$stmt,
$item->value,
new TypeExpr(new MixedType()),
$nodeCallback,
)->getScope();
}
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/PHPStan/Analyser/nsrt/bug-6799.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ function testByRefInArray(): void
assertType('array{}', $a);

$b = [&$a];
assertType('mixed', $a); // Could stay array{}
assertType('array{}', $a);

foo($b);
assertType('mixed', $a);
assertType('array{}', $a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be modified since $b was passed to foo and is related to $a

}

function testByRefInArrayWithKey(): void
Expand All @@ -63,10 +63,10 @@ function testByRefInArrayWithKey(): void
assertType("'hello'", $a);

$b = ['key' => &$a];
assertType('mixed', $a); // Could stay 'hello'
assertType("'hello'", $a);

$b['key'] = 42;
assertType('mixed', $a); // Could be 42
assertType("'hello'", $a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 42 since $b['key'] = 42; override $a reference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already handled — that was the final phpstan run which passed with no errors. All work is complete and pushed.

}

function testMultipleByRefInArray(): void
Expand All @@ -75,13 +75,13 @@ function testMultipleByRefInArray(): void
$c = 'test';

$b = [&$a, 'normal', &$c];
assertType('mixed', $a); // Could stay 1
assertType('mixed', $c); // Could stay 'test'
assertType('1', $a);
assertType("'test'", $c);

$b[0] = 2;
$b[1] = 'foo';
$b[2] = 'bar';

assertType('mixed', $a); // Could be 2
assertType('mixed', $c); // Could be 'bar'
assertType('1', $a);
assertType("'test'", $c);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 2 and bar since $b[0] and $b[2] overrides $c reference

}
Loading