Skip to content

Commit c90cc0b

Browse files
committed
refactor
1 parent 607b822 commit c90cc0b

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use function count;
2727
use function in_array;
2828
use function is_int;
29-
use function is_string;
3029

3130
#[AutowiredService]
3231
final class ArrayMergeFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -100,26 +99,25 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
10099
}
101100

102101
if ($keyTypes === []) {
103-
foreach ($offsetTypes as [&$generalize, $offsetType]) {
104-
$generalize = true;
102+
foreach ($offsetTypes as $key => [$generalize, $offsetValueType]) {
103+
$offsetTypes[$key][0] = true;
105104
}
106-
unset($generalize);
107105
}
108106

109-
if ($newArrayBuilder === null) {
110-
foreach (TypeUtils::getAccessoryTypes($argType) as $accessoryType) {
111-
if (
112-
!($accessoryType instanceof HasOffsetType)
113-
&& !($accessoryType instanceof HasOffsetValueType)
114-
) {
115-
continue;
116-
}
117-
118-
$offsetType = $accessoryType->getOffsetType();
119-
$offsetValueType = $argType->getOffsetValueType($offsetType);
120-
$offsetTypes[$offsetType->getValue()] = [false, $offsetValueType];
107+
foreach (TypeUtils::getAccessoryTypes($argType) as $accessoryType) {
108+
if (
109+
!($accessoryType instanceof HasOffsetType)
110+
&& !($accessoryType instanceof HasOffsetValueType)
111+
) {
112+
continue;
121113
}
122114

115+
$offsetType = $accessoryType->getOffsetType();
116+
$offsetValueType = $argType->getOffsetValueType($offsetType);
117+
$offsetTypes[$offsetType->getValue()] = [false, $offsetValueType];
118+
}
119+
120+
if ($newArrayBuilder === null) {
123121
continue;
124122
}
125123

@@ -182,7 +180,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
182180
}
183181
$keyType = new ConstantStringType($key);
184182

185-
if (!$generalize && is_string($key)) {
183+
if (!$generalize) {
186184
// the last string-keyed offset will overwrite previous values
187185
$hasOffsetType = new HasOffsetValueType(
188186
$keyType,

tests/PHPStan/Analyser/nsrt/array-merge-const-non-const.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,29 @@ function doOptKeys(array $array, array $arr2): void {
3232
$array['abc'] = 'def';
3333
}
3434
assertType("array", array_merge($arr2, $array));
35+
assertType("array", array_merge($array, $arr2));
3536
}
3637

3738
/**
3839
* @param array{a?: 1, b: 2} $array
3940
*/
4041
function doOptShapeKeys(array $array, array $arr2): void {
4142
assertType("non-empty-array&hasOffsetValue('b', 2)", array_merge($arr2, $array));
43+
assertType("non-empty-array&hasOffset('b')", array_merge($array, $arr2));
4244
}
4345

4446
function hasOffsetKeys(array $array, array $arr2): void {
4547
if (array_key_exists('b', $array)) {
4648
assertType("non-empty-array&hasOffsetValue('b', mixed)", array_merge($arr2, $array));
49+
assertType("non-empty-array&hasOffset('b')", array_merge($array, $arr2));
4750
}
4851
}
4952

5053
function hasOffsetValueKeys(array $array, array $arr2): void {
5154
$array['b'] = 123;
5255

5356
assertType("non-empty-array&hasOffsetValue('b', 123)", array_merge($arr2, $array));
57+
assertType("non-empty-array&hasOffset('b')", array_merge($array, $arr2));
5458
}
5559

5660
/**

0 commit comments

Comments
 (0)