Skip to content

Commit 51555c7

Browse files
committed
use newType() as expr
1 parent 636a315 commit 51555c7

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,17 +2664,18 @@ static function (): void {
26642664
$arrayArgNativeType = $scope->getNativeType($arrayArg);
26652665

26662666
$isArrayPop = $functionReflection->getName() === 'array_pop';
2667+
$newType = $isArrayPop ? $arrayArgType->popArray() : $arrayArgType->shiftArray();
26672668
$scope = $scope->invalidateExpression($arrayArg)->assignExpression(
26682669
$arrayArg,
2669-
$isArrayPop ? $arrayArgType->popArray() : $arrayArgType->shiftArray(),
2670+
$newType,
26702671
$isArrayPop ? $arrayArgNativeType->popArray() : $arrayArgNativeType->shiftArray(),
26712672
);
26722673

26732674
$scope = $this->processAssignVar(
26742675
$scope,
26752676
$stmt,
26762677
$arrayArg,
2677-
$arrayArg,
2678+
new TypeExpr($newType),
26782679
static function (Node $node, Scope $scope) use ($nodeCallback): void {
26792680
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
26802681
return;
@@ -2703,7 +2704,7 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27032704
$scope,
27042705
$stmt,
27052706
$arrayArg,
2706-
$arrayArg,
2707+
new TypeExpr($arrayType),
27072708
static function (Node $node, Scope $scope) use ($nodeCallback): void {
27082709
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
27092710
return;
@@ -2729,17 +2730,18 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27292730
&& $functionReflection->getName() === 'shuffle'
27302731
) {
27312732
$arrayArg = $expr->getArgs()[0]->value;
2733+
$newType = $scope->getType($arrayArg)->shuffleArray();
27322734
$scope = $scope->assignExpression(
27332735
$arrayArg,
2734-
$scope->getType($arrayArg)->shuffleArray(),
2736+
$newType,
27352737
$scope->getNativeType($arrayArg)->shuffleArray(),
27362738
);
27372739

27382740
$scope = $this->processAssignVar(
27392741
$scope,
27402742
$stmt,
27412743
$arrayArg,
2742-
$arrayArg,
2744+
new TypeExpr($newType),
27432745
static function (Node $node, Scope $scope) use ($nodeCallback): void {
27442746
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
27452747
return;
@@ -2766,17 +2768,18 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27662768
$lengthType = isset($expr->getArgs()[2]) ? $scope->getType($expr->getArgs()[2]->value) : new NullType();
27672769
$replacementType = isset($expr->getArgs()[3]) ? $scope->getType($expr->getArgs()[3]->value) : new ConstantArrayType([], []);
27682770

2771+
$newType = $arrayArgType->spliceArray($offsetType, $lengthType, $replacementType);
27692772
$scope = $scope->invalidateExpression($arrayArg)->assignExpression(
27702773
$arrayArg,
2771-
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
2774+
$newType,
27722775
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
27732776
);
27742777

27752778
$scope = $this->processAssignVar(
27762779
$scope,
27772780
$stmt,
27782781
$arrayArg,
2779-
$arrayArg,
2782+
new TypeExpr($newType),
27802783
static function (Node $node, Scope $scope) use ($nodeCallback): void {
27812784
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
27822785
return;
@@ -2796,17 +2799,18 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27962799
&& count($expr->getArgs()) >= 1
27972800
) {
27982801
$arrayArg = $expr->getArgs()[0]->value;
2802+
$newType = $this->getArraySortPreserveListFunctionType($scope->getType($arrayArg));
27992803
$scope = $scope->assignExpression(
28002804
$arrayArg,
2801-
$this->getArraySortPreserveListFunctionType($scope->getType($arrayArg)),
2805+
$newType,
28022806
$this->getArraySortPreserveListFunctionType($scope->getNativeType($arrayArg)),
28032807
);
28042808

28052809
$scope = $this->processAssignVar(
28062810
$scope,
28072811
$stmt,
28082812
$arrayArg,
2809-
$arrayArg,
2813+
new TypeExpr($newType),
28102814
static function (Node $node, Scope $scope) use ($nodeCallback): void {
28112815
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
28122816
return;
@@ -2826,17 +2830,18 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
28262830
&& count($expr->getArgs()) >= 1
28272831
) {
28282832
$arrayArg = $expr->getArgs()[0]->value;
2833+
$newType = $this->getArraySortDoNotPreserveListFunctionType($scope->getType($arrayArg));
28292834
$scope = $scope->assignExpression(
28302835
$arrayArg,
2831-
$this->getArraySortDoNotPreserveListFunctionType($scope->getType($arrayArg)),
2836+
$newType,
28322837
$this->getArraySortDoNotPreserveListFunctionType($scope->getNativeType($arrayArg)),
28332838
);
28342839

28352840
$scope = $this->processAssignVar(
28362841
$scope,
28372842
$stmt,
28382843
$arrayArg,
2839-
$arrayArg,
2844+
new TypeExpr($newType),
28402845
static function (Node $node, Scope $scope) use ($nodeCallback): void {
28412846
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
28422847
return;

tests/PHPStan/Analyser/nsrt/shuffle.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function normalArrays1(array $arr): void
1313
/** @var mixed[] $arr */
1414
shuffle($arr);
1515
assertType('list<mixed>', $arr);
16-
assertNativeType('list', $arr);
16+
assertNativeType('list<mixed>', $arr);
1717
assertType('list<int<0, max>>', array_keys($arr));
1818
assertType('list<mixed>', array_values($arr));
1919
}
@@ -23,7 +23,7 @@ public function normalArrays2(array $arr): void
2323
/** @var non-empty-array<string, int> $arr */
2424
shuffle($arr);
2525
assertType('non-empty-list<int>', $arr);
26-
assertNativeType('list', $arr);
26+
assertNativeType('non-empty-list<int>', $arr);
2727
assertType('non-empty-list<int<0, max>>', array_keys($arr));
2828
assertType('non-empty-list<int>', array_values($arr));
2929
}
@@ -67,7 +67,7 @@ public function constantArrays2(array $arr): void
6767
/** @var array{0?: 1, 1?: 2, 2?: 3} $arr */
6868
shuffle($arr);
6969
assertType('list<1|2|3>', $arr);
70-
assertNativeType('list', $arr);
70+
assertNativeType('list<1|2|3>', $arr);
7171
assertType('list<0|1|2>', array_keys($arr));
7272
assertType('list<1|2|3>', array_values($arr));
7373
}
@@ -107,7 +107,7 @@ public function constantArrays6(array $arr): void
107107
/** @var array{foo?: 1, bar: 2, }|array{baz: 3, foobar?: 4} $arr */
108108
shuffle($arr);
109109
assertType('non-empty-list<1|2|3|4>', $arr);
110-
assertNativeType('list', $arr);
110+
assertNativeType('non-empty-list<1|2|3|4>', $arr);
111111
assertType('non-empty-list<0|1>', array_keys($arr));
112112
assertType('non-empty-list<1|2|3|4>', array_values($arr));
113113
}

tests/PHPStan/Analyser/nsrt/sort.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ public function normalArray(array $arr): void
9191
$arr1 = $arr;
9292
sort($arr1);
9393
assertType('list<string>', $arr1);
94-
assertNativeType('list', $arr1);
94+
assertNativeType('list<string>', $arr1);
9595

9696
$arr2 = $arr;
9797
rsort($arr2);
9898
assertType('list<string>', $arr2);
99-
assertNativeType('list', $arr2);
99+
assertNativeType('list<string>', $arr2);
100100

101101
$arr3 = $arr;
102102
usort($arr3, fn(int $a, int $b) => $a <=> $b);
103103
assertType('list<string>', $arr3);
104-
assertNativeType('list', $arr3);
104+
assertNativeType('list<string>', $arr3);
105105
}
106106

107107
public function mixed($arr): void

0 commit comments

Comments
 (0)