diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 66a30ed9d28..7e7edd294b2 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -3159,6 +3159,10 @@ private function legacyIsKeysSupersetOf(self $otherArray): bool $keyIndexMap = $this->getKeyIndexMap(); $otherKeyValues = []; + // the merged array has $this's key order, so a different order of the + // shared integer keys would make the merged list-ness wrong + $checkKeyOrder = !$this->isList->no() || !$otherArray->isList->no(); + $previousIndex = -1; foreach ($otherArray->keyTypes as $j => $keyType) { $keyValue = $keyType->getValue(); @@ -3167,6 +3171,13 @@ private function legacyIsKeysSupersetOf(self $otherArray): bool return false; } + if ($checkKeyOrder && is_int($keyValue)) { + if ($i < $previousIndex) { + return false; + } + $previousIndex = $i; + } + $otherKeyValues[$keyValue] = true; $valueType = $this->valueTypes[$i]; diff --git a/tests/PHPStan/Analyser/nsrt/bug-15679.php b/tests/PHPStan/Analyser/nsrt/bug-15679.php new file mode 100644 index 00000000000..b4d5e266844 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15679.php @@ -0,0 +1,77 @@ + 0, 0 => 0] : [0 => 1, 1 => 0]; + assertType('array{1, 0}|array{1: 0, 0: 0}', $x); + assertType('bool', array_is_list($x)); + if (array_is_list($x)) { + assertType('array{1, 0}', $x); + } else { + assertType('array{1: 0, 0: 0}', $x); + } + + $y = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a']; + assertType("array{'c', 'a'}|array{1: 'a', 0: 'b'}", $y); + assertType('bool', array_is_list($y)); +} + +function listFirst(bool $b): void +{ + $x = $b ? [0 => 1, 1 => 0] : [1 => 0, 0 => 0]; + assertType('array{1, 0}|array{1: 0, 0: 0}', $x); + assertType('bool', array_is_list($x)); + if (array_is_list($x)) { + assertType('array{1, 0}', $x); + } else { + assertType('array{1: 0, 0: 0}', $x); + } +} + +function sameValues(bool $b): void +{ + $x = $b ? [1 => 0, 0 => 0] : [0 => 0, 1 => 0]; + assertType('array{0, 0}|array{1: 0, 0: 0}', $x); + assertType('bool', array_is_list($x)); + + $y = $b ? [0 => 0, 1 => 0] : [1 => 0, 0 => 0]; + assertType('array{0, 0}|array{1: 0, 0: 0}', $y); + assertType('bool', array_is_list($y)); +} + +function differingAtTwoKeys(bool $b): void +{ + $x = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'd']; + assertType("array{'c', 'd'}|array{1: 'a', 0: 'b'}", $x); + assertType('bool', array_is_list($x)); +} + +function sameOrder(bool $b): void +{ + $x = $b ? [0 => 0, 1 => 0] : [0 => 1, 1 => 0]; + assertType('array{0|1, 0}', $x); + assertType('true', array_is_list($x)); + + $y = $b ? [1 => 0, 0 => 0] : [1 => 0, 0 => 1]; + assertType('array{1: 0, 0: 0|1}', $y); + assertType('false', array_is_list($y)); +} + +function ifElse(bool $b): void +{ + if ($b) { + $x = []; + $x[1] = 0; + $x[0] = 0; + } else { + $x = []; + $x[0] = 1; + $x[1] = 0; + } + assertType('array{1, 0}|array{1: 0, 0: 0}', $x); + assertType('bool', array_is_list($x)); +} diff --git a/turbo-ext/src/ConstantArrayType.cpp b/turbo-ext/src/ConstantArrayType.cpp index bf65de7da2a..024681f1dba 100644 --- a/turbo-ext/src/ConstantArrayType.cpp +++ b/turbo-ext/src/ConstantArrayType.cpp @@ -4974,6 +4974,16 @@ class ConstantArrayType zv::Val keyIndexMap = getKeyIndexMap(); if (UNEXPECTED(keyIndexMap.isUndef())) return false; zv::Arr otherKeyValues = zv::Arr::create((uint32_t) otherKeyTypesCount); + /* the merged array has $this's key order, so a different order of the + * shared integer keys would make the merged list-ness wrong */ + zval *l = isListSlot(); + zval *otherL = l != NULL ? slotOf(other, slots::isList, "isList") : NULL; + if (UNEXPECTED(otherL == NULL)) return false; + zend_long isListValue = trinaryOf(l); + zend_long otherIsListValue = trinaryOf(otherL); + if (UNEXPECTED(isListValue < 0 || otherIsListValue < 0)) return false; + bool checkKeyOrder = isListValue != PT_TRI_NO || otherIsListValue != PT_TRI_NO; + zend_long previousIndex = -1; for (zv::ArrayEntry entry : zv::ArrRef(otherK)) { zend_long j = (zend_long) entry.indexKey(); @@ -4985,6 +4995,14 @@ class ConstantArrayType return true; } + if (checkKeyOrder && Z_TYPE_P(keyValueZv.raw()) == IS_LONG) { + if (Z_LVAL_P(iZv) < previousIndex) { + out = false; + return true; + } + previousIndex = Z_LVAL_P(iZv); + } + zval trueZv; ZVAL_TRUE(&trueZv); if (UNEXPECTED(!mapSet(otherKeyValues.table(), keyValueZv.raw(), &trueZv))) return false;