Skip to content

Commit 5c70842

Browse files
Use pre-args scope for value types in array_push/array_unshift (#5579)
1 parent c6fb24e commit 5c70842

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/Analyser/ExprHandler/FuncCallHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
265265
}
266266
}
267267

268+
$scopeBeforeArgs = $scope;
268269
$argsResult = $nodeScopeResolver->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallbackForArgs, $context);
269270
$scope = $argsResult->getScope();
270271
$hasYield = $argsResult->hasYield();
@@ -395,8 +396,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
395396
$stmt,
396397
$arrayArg,
397398
new NativeTypeExpr(
398-
$this->getArrayFunctionAppendingType($functionReflection, $scope, $normalizedExpr),
399-
$this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $normalizedExpr),
399+
$this->getArrayFunctionAppendingType($functionReflection, $scopeBeforeArgs, $normalizedExpr),
400+
$this->getArrayFunctionAppendingType($functionReflection, $scopeBeforeArgs->doNotTreatPhpDocTypesAsCertain(), $normalizedExpr),
400401
),
401402
$nodeCallback,
402403
)->getScope();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Bug13510;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
final class Foo
8+
{
9+
10+
/** @param non-empty-list<int> $arr */
11+
public function test(array $arr): void
12+
{
13+
array_unshift($arr, array_pop($arr));
14+
assertType('non-empty-list<int>', $arr);
15+
}
16+
17+
/** @param non-empty-list<int> $arr */
18+
public function testTwoLines(array $arr): void
19+
{
20+
$popped = array_pop($arr);
21+
array_unshift($arr, $popped);
22+
assertType('non-empty-list<int>', $arr);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)