Skip to content

Commit 169af1d

Browse files
authored
Use TypeUtils::getOldConstantArrays in array_pop and array_shift extensions
1 parent 2a03c92 commit 169af1d

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,32 @@ public function getValueTypes(): array
220220
return $this->valueTypes;
221221
}
222222

223+
public function getFirstValueType(): Type
224+
{
225+
$valueTypes = [];
226+
for ($i = 0, $keyTypesCount = count($this->keyTypes); $i < $keyTypesCount; $i++) {
227+
$valueTypes[] = $this->valueTypes[$i];
228+
if (!$this->isOptionalKey($i)) {
229+
break;
230+
}
231+
}
232+
233+
return TypeCombinator::union(...$valueTypes);
234+
}
235+
236+
public function getLastValueType(): Type
237+
{
238+
$valueTypes = [];
239+
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
240+
$valueTypes[] = $this->valueTypes[$i];
241+
if (!$this->isOptionalKey($i)) {
242+
break;
243+
}
244+
}
245+
246+
return TypeCombinator::union(...$valueTypes);
247+
}
248+
223249
public function isOptionalKey(int $i): bool
224250
{
225251
return in_array($i, $this->optionalKeys, true);

src/Type/Php/ArrayPopFunctionReturnTypeExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3333
return new NullType();
3434
}
3535

36-
$constantArrays = TypeUtils::getConstantArrays($argType);
36+
$constantArrays = TypeUtils::getOldConstantArrays($argType);
3737
if (count($constantArrays) > 0) {
3838
$valueTypes = [];
3939
foreach ($constantArrays as $constantArray) {
@@ -42,8 +42,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4242
$valueTypes[] = new NullType();
4343
continue;
4444
}
45-
46-
$valueTypes[] = $constantArray->getOffsetValueType($arrayKeyTypes[count($arrayKeyTypes) - 1]);
45+
$valueTypes[] = $constantArray->getLastValueType();
4746
}
4847

4948
return TypeCombinator::union(...$valueTypes);

src/Type/Php/ArrayShiftFunctionReturnTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3333
return new NullType();
3434
}
3535

36-
$constantArrays = TypeUtils::getConstantArrays($argType);
36+
$constantArrays = TypeUtils::getOldConstantArrays($argType);
3737
if (count($constantArrays) > 0) {
3838
$valueTypes = [];
3939
foreach ($constantArrays as $constantArray) {
@@ -43,7 +43,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4343
continue;
4444
}
4545

46-
$valueTypes[] = $constantArray->getOffsetValueType($arrayKeyTypes[0]);
46+
$valueTypes[] = $constantArray->getFirstValueType();
4747
}
4848

4949
return TypeCombinator::union(...$valueTypes);

tests/PHPStan/Analyser/data/array-pop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public function constantArrays(array $arr): void
3838
public function constantArraysWithOptionalKeys(array $arr): void
3939
{
4040
/** @var array{a?: 0, b: 1, c: 2} $arr */
41-
assertType('0|2', array_pop($arr)); // should be 2
41+
assertType('2', array_pop($arr));
4242
assertType('array{a?: 0, b: 1}', $arr);
4343

4444
/** @var array{a: 0, b?: 1, c: 2} $arr */
45-
assertType('1|2', array_pop($arr)); // should be 2
45+
assertType('2', array_pop($arr));
4646
assertType('array{a: 0, b?: 1}', $arr);
4747

4848
/** @var array{a: 0, b: 1, c?: 2} $arr */

tests/PHPStan/Analyser/data/array-shift.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function constantArrays(array $arr): void
3838
public function constantArraysWithOptionalKeys(array $arr): void
3939
{
4040
/** @var array{a?: 0, b: 1, c: 2} $arr */
41-
assertType('1', array_shift($arr)); // should be 0|1
41+
assertType('0|1', array_shift($arr));
4242
assertType('array{b?: 1, c: 2}', $arr);
4343

4444
/** @var array{a: 0, b?: 1, c: 2} $arr */

0 commit comments

Comments
 (0)