From ae8cc5a8cb16ce3f41e93190d00f3715a451bd83 Mon Sep 17 00:00:00 2001 From: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:05:11 +0000 Subject: [PATCH 1/2] Reject keys-superset merge of constant arrays whose shared integer keys are in a different order unless both are non-lists - `ConstantArrayType::legacyIsKeysSupersetOf()` now returns false when the integer keys shared by both arrays appear in a different relative order, unless both arrays are definitely not lists. `mergeWith()` keeps `$this`'s key order and infers list-ness from it, so `[1 => 0, 0 => 0] | [0 => 1, 1 => 0]` used to become the list `array{0|1, 0}` and `array_is_list()` was reported as always true. - String keys are not checked: their position does not affect list-ness, and checking them stopped lossless optional-key collapsing (TypeCombinatorTest, bug-13637). - Ported the same check to the native mirror in turbo-ext/src/ConstantArrayType.cpp. - Also checked: identical values in a different order, a superset with an extra key, if/else scope merging and offset-by-offset building. All of them now keep both shapes. --- src/Type/Constant/ConstantArrayType.php | 11 +++++++++++ tests/PHPStan/Analyser/nsrt/bug-15679.php | 16 ++++++++++++++++ turbo-ext/src/ConstantArrayType.cpp | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-15679.php 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..5b5d18423d8 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15679.php @@ -0,0 +1,16 @@ + 0, 0 => 0] : [0 => 1, 1 => 0]; + assertType('array{1, 0}|array{1: 0, 0: 0}', $x); + assertType('bool', array_is_list($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)); +} 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; From a3cdde2aee5da5a7c42ddefbbfc01c4ad29bee0a Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Thu, 19 Nov 2026 10:17:05 +0000 Subject: [PATCH 2/2] Cover both merge orders, equal values, same order and array_is_list() narrowing in bug-15679 test Co-Authored-By: Claude Opus 5.5 --- tests/PHPStan/Analyser/nsrt/bug-15679.php | 61 +++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/PHPStan/Analyser/nsrt/bug-15679.php b/tests/PHPStan/Analyser/nsrt/bug-15679.php index 5b5d18423d8..b4d5e266844 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-15679.php +++ b/tests/PHPStan/Analyser/nsrt/bug-15679.php @@ -9,8 +9,69 @@ function f(bool $b): void $x = $b ? [1 => 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)); +}