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
21 changes: 17 additions & 4 deletions src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,22 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$arrayArgType = $scope->getType($arrayArg);
$arrayArgNativeType = $scope->getNativeType($arrayArg);

$offsetType = $scope->getType($normalizedExpr->getArgs()[1]->value);
$lengthType = isset($normalizedExpr->getArgs()[2]) ? $scope->getType($normalizedExpr->getArgs()[2]->value) : new NullType();
$replacementType = isset($normalizedExpr->getArgs()[3]) ? $scope->getType($normalizedExpr->getArgs()[3]->value) : new ConstantArrayType([], []);
$offsetType = $scopeBeforeArgs->getType($normalizedExpr->getArgs()[1]->value);

if (isset($normalizedExpr->getArgs()[2])) {
$lengthType = $scopeBeforeArgs->getType($normalizedExpr->getArgs()[2]->value);
} else {
$lengthType = new NullType();
}

if (isset($normalizedExpr->getArgs()[3])) {
$replacementArg = $normalizedExpr->getArgs()[3]->value;
$replacementType = $scopeBeforeArgs->getType($replacementArg);
$replacementNativeType = $scopeBeforeArgs->getNativeType($replacementArg);
} else {
$replacementType = new ConstantArrayType([], []);
$replacementNativeType = new ConstantArrayType([], []);
}

$scope = $nodeScopeResolver->processVirtualAssign(
$scope,
Expand All @@ -446,7 +459,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$arrayArg,
new NativeTypeExpr(
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementNativeType),
),
$nodeCallback,
)->getScope();
Expand Down
51 changes: 51 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13510.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,56 @@ public function testTwoLines(array $arr): void
array_unshift($arr, $popped);
assertType('non-empty-list<int>', $arr);
}
}

class Bar
{
/** @var array<int> */
public array $arr = [];

public function test(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
assertType('non-empty-array<int>', $this->arr);
array_unshift($this->arr, array_pop($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArrayPush(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_push($this->arr, array_pop($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArrayUnshiftWithArrayShift(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_unshift($this->arr, array_shift($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArrayPushWithArrayShift(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_push($this->arr, array_shift($this->arr));
assertType('non-empty-array<int>', $this->arr);
}

public function testArraySplice(): void
{
if (count($this->arr) === 0) {
throw new \Exception();
}
array_splice($this->arr, 0, 0, [array_pop($this->arr)]);
assertType('non-empty-array<(int<0, max>|string), int>', $this->arr);
}
}
Loading