@@ -275,6 +275,15 @@ public function forgetKeyOrder(): self
275275 return new self ($ this ->keyTypes , $ this ->valueTypes , $ this ->nextAutoIndexes , $ this ->optionalKeys , $ this ->isList , $ this ->unsealedDeclaration ?? $ this ->unsealed , false );
276276 }
277277
278+ private function knowKeyOrder (): self
279+ {
280+ if ($ this ->keyOrderKnown ) {
281+ return $ this ;
282+ }
283+
284+ return new self ($ this ->keyTypes , $ this ->valueTypes , $ this ->nextAutoIndexes , $ this ->optionalKeys , $ this ->isList , $ this ->unsealedDeclaration ?? $ this ->unsealed , true );
285+ }
286+
278287 /**
279288 * Whether both shapes know their key order and list the keys they share in the same order.
280289 * Two such shapes that list them in different orders never hold the same array.
@@ -581,8 +590,8 @@ public function getAllArrays(): array
581590 }
582591
583592 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
584- if (! $ this ->keyOrderKnown ) {
585- $ builder ->forgetKeyOrder ();
593+ if ($ this ->keyOrderKnown ) {
594+ $ builder ->makeKeyOrderKnown ();
586595 }
587596 $ builder ->disableArrayDegradation ();
588597 foreach ($ keys as $ i ) {
@@ -1982,8 +1991,8 @@ public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
19821991 $ length = $ finiteType ->getValue ();
19831992
19841993 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
1985- if (! $ this ->keyOrderKnown ) {
1986- $ builder ->forgetKeyOrder ();
1994+ if ($ this ->keyOrderKnown ) {
1995+ $ builder ->makeKeyOrderKnown ();
19871996 }
19881997
19891998 $ keyTypesCount = count ($ this ->keyTypes );
@@ -2004,8 +2013,8 @@ public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
20042013 public function fillKeysArray (Type $ valueType ): Type
20052014 {
20062015 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2007- if (! $ this ->keyOrderKnown ) {
2008- $ builder ->forgetKeyOrder ();
2016+ if ($ this ->keyOrderKnown ) {
2017+ $ builder ->makeKeyOrderKnown ();
20092018 }
20102019
20112020 foreach ($ this ->valueTypes as $ i => $ keyType ) {
@@ -2040,8 +2049,8 @@ public function fillKeysArray(Type $valueType): Type
20402049 public function flipArray (): Type
20412050 {
20422051 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2043- if (! $ this ->keyOrderKnown ) {
2044- $ builder ->forgetKeyOrder ();
2052+ if ($ this ->keyOrderKnown ) {
2053+ $ builder ->makeKeyOrderKnown ();
20452054 }
20462055
20472056 foreach ($ this ->keyTypes as $ i => $ keyType ) {
@@ -2077,8 +2086,8 @@ public function flipArray(): Type
20772086 public function intersectKeyArray (Type $ otherArraysType ): Type
20782087 {
20792088 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2080- if (! $ this ->keyOrderKnown ) {
2081- $ builder ->forgetKeyOrder ();
2089+ if ($ this ->keyOrderKnown ) {
2090+ $ builder ->makeKeyOrderKnown ();
20822091 }
20832092
20842093 foreach ($ this ->keyTypes as $ i => $ keyType ) {
@@ -2123,8 +2132,8 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type
21232132 }
21242133
21252134 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2126- if (! $ this ->keyOrderKnown ) {
2127- $ builder ->forgetKeyOrder ();
2135+ if ($ this ->keyOrderKnown ) {
2136+ $ builder ->makeKeyOrderKnown ();
21282137 }
21292138
21302139 for ($ i = count ($ this ->keyTypes ) - 1 ; $ i >= 0 ; $ i --) {
@@ -2304,8 +2313,8 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
23042313 }
23052314
23062315 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2307- if (! $ this ->keyOrderKnown ) {
2308- $ builder ->forgetKeyOrder ();
2316+ if ($ this ->keyOrderKnown ) {
2317+ $ builder ->makeKeyOrderKnown ();
23092318 }
23102319
23112320 $ nonOptionalElementsCount = 0 ;
@@ -2406,8 +2415,8 @@ public function spliceArray(Type $offsetType, Type $lengthType, Type $replacemen
24062415 $ optionalKeysBeforeReplacement = 0 ;
24072416
24082417 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2409- if (! $ this ->keyOrderKnown ) {
2410- $ builder ->forgetKeyOrder ();
2418+ if ($ this ->keyOrderKnown ) {
2419+ $ builder ->makeKeyOrderKnown ();
24112420 }
24122421 for ($ i = 0 ;; $ i ++) {
24132422 $ isOptional = $ this ->isOptionalKey ($ i );
@@ -2541,8 +2550,8 @@ public function truncateListToSize(Type $sizeType): Type
25412550 }
25422551
25432552 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2544- if (! $ this ->keyOrderKnown ) {
2545- $ builder ->forgetKeyOrder ();
2553+ if ($ this ->keyOrderKnown ) {
2554+ $ builder ->makeKeyOrderKnown ();
25462555 }
25472556 foreach ($ builderData as [$ offsetType , $ valueType , $ optional ]) {
25482557 $ builder ->setOffsetValueType ($ offsetType , $ valueType , $ optional );
@@ -2812,8 +2821,8 @@ private function removeLastElements(int $length): self
28122821 private function removeFirstElements (int $ length , bool $ reindex = true ): Type
28132822 {
28142823 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
2815- if (! $ this ->keyOrderKnown ) {
2816- $ builder ->forgetKeyOrder ();
2824+ if ($ this ->keyOrderKnown ) {
2825+ $ builder ->makeKeyOrderKnown ();
28172826 }
28182827
28192828 $ optionalKeysIgnored = 0 ;
@@ -3037,7 +3046,10 @@ public function inRenumberingOrder(): self
30373046 $ optionalKeys [] = count ($ keyTypes ) - 1 ;
30383047 }
30393048
3040- return $ this ->recreate ($ keyTypes , $ valueTypes , $ this ->nextAutoIndexes , $ optionalKeys , $ this ->isList , $ this ->unsealed );
3049+ $ renumbered = $ this ->recreate ($ keyTypes , $ valueTypes , $ this ->nextAutoIndexes , $ optionalKeys , $ this ->isList , $ this ->unsealed );
3050+
3051+ // with only integer keys, each position holds any of the values, so the positions are all the order there is
3052+ return count ($ integerValueTypes ) === count ($ this ->keyTypes ) ? $ renumbered ->knowKeyOrder () : $ renumbered ;
30413053 }
30423054
30433055 private function listsIntegerKey (): bool
@@ -3823,41 +3835,62 @@ public function mergeWith(self $otherArray): self
38233835 */
38243836 public function inMergedKeyOrderOf (self $ a , self $ b ): self
38253837 {
3826- if (!$ a ->sharesKeyOrderWith ($ b )) {
3838+ $ mergedKeyTypes = $ a ->getKeyTypesInMergedOrderWith ($ b );
3839+ if ($ mergedKeyTypes === null ) {
38273840 return $ this ->forgetKeyOrder ();
38283841 }
38293842
3830- $ aKeyIndexMap = $ a ->getKeyIndexMap ();
3831- $ bKeyIndexMap = $ b ->getKeyIndexMap ();
38323843 $ positions = [];
3844+ foreach ($ mergedKeyTypes as $ position => $ keyType ) {
3845+ $ positions [$ keyType ->getValue ()] = $ position ;
3846+ }
3847+
3848+ $ indices = [];
3849+ foreach ($ this ->keyTypes as $ k => $ keyType ) {
3850+ $ indices [$ positions [$ keyType ->getValue ()]] = $ k ;
3851+ }
3852+ ksort ($ indices );
3853+
3854+ return $ this ->reorderKeys (array_values ($ indices ));
3855+ }
3856+
3857+ /**
3858+ * The keys of both shapes in an order both of them keep: a key only one of them lists stays between the keys
3859+ * it is listed between there. Null when the shapes do not list the keys they share in one known order.
3860+ *
3861+ * @return list<ConstantIntegerType|ConstantStringType>|null
3862+ */
3863+ public function getKeyTypesInMergedOrderWith (self $ other ): ?array
3864+ {
3865+ if (!$ this ->sharesKeyOrderWith ($ other )) {
3866+ return null ;
3867+ }
3868+
3869+ $ keyIndexMap = $ this ->getKeyIndexMap ();
3870+ $ otherKeyIndexMap = $ other ->getKeyIndexMap ();
3871+ $ keyTypes = [];
38333872 $ i = 0 ;
38343873 $ j = 0 ;
3835- $ aCount = count ($ a ->keyTypes );
3836- $ bCount = count ($ b ->keyTypes );
3837- while ($ i < $ aCount || $ j < $ bCount ) {
3838- if ($ i < $ aCount && !array_key_exists ($ a ->keyTypes [$ i ]->getValue (), $ bKeyIndexMap )) {
3839- $ positions [ $ a ->keyTypes [$ i ]-> getValue ()] = count ( $ positions ) ;
3874+ $ count = count ($ this ->keyTypes );
3875+ $ otherCount = count ($ other ->keyTypes );
3876+ while ($ i < $ count || $ j < $ otherCount ) {
3877+ if ($ i < $ count && !array_key_exists ($ this ->keyTypes [$ i ]->getValue (), $ otherKeyIndexMap )) {
3878+ $ keyTypes [] = $ this ->keyTypes [$ i ];
38403879 $ i ++;
38413880 continue ;
38423881 }
3843- if ($ j < $ bCount && !array_key_exists ($ b ->keyTypes [$ j ]->getValue (), $ aKeyIndexMap )) {
3844- $ positions [ $ b ->keyTypes [$ j ]-> getValue ()] = count ( $ positions ) ;
3882+ if ($ j < $ otherCount && !array_key_exists ($ other ->keyTypes [$ j ]->getValue (), $ keyIndexMap )) {
3883+ $ keyTypes [] = $ other ->keyTypes [$ j ];
38453884 $ j ++;
38463885 continue ;
38473886 }
38483887 // the next key of both is the same shared key
3849- $ positions [ $ a ->keyTypes [$ i ]-> getValue ()] = count ( $ positions ) ;
3888+ $ keyTypes [] = $ this ->keyTypes [$ i ];
38503889 $ i ++;
38513890 $ j ++;
38523891 }
38533892
3854- $ indices = [];
3855- foreach ($ this ->keyTypes as $ k => $ keyType ) {
3856- $ indices [$ positions [$ keyType ->getValue ()]] = $ k ;
3857- }
3858- ksort ($ indices );
3859-
3860- return $ this ->reorderKeys (array_values ($ indices ));
3893+ return $ keyTypes ;
38613894 }
38623895
38633896 private function legacyMergeWith (self $ otherArray ): self
@@ -4089,8 +4122,8 @@ public function makeList(): Type
40894122
40904123 if (count ($ keptPositions ) < count ($ this ->keyTypes )) {
40914124 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
4092- if (! $ this ->keyOrderKnown ) {
4093- $ builder ->forgetKeyOrder ();
4125+ if ($ this ->keyOrderKnown ) {
4126+ $ builder ->makeKeyOrderKnown ();
40944127 }
40954128 foreach ($ keptPositions as $ position ) {
40964129 $ builder ->setOffsetValueType (
@@ -4173,8 +4206,8 @@ public function makeAllArrayKeysOptional(): Type
41734206 public function changeKeyCaseArray (?int $ case ): Type
41744207 {
41754208 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
4176- if (! $ this ->keyOrderKnown ) {
4177- $ builder ->forgetKeyOrder ();
4209+ if ($ this ->keyOrderKnown ) {
4210+ $ builder ->makeKeyOrderKnown ();
41784211 }
41794212 foreach ($ this ->keyTypes as $ i => $ keyType ) {
41804213 if ($ keyType instanceof ConstantStringType) {
@@ -4200,8 +4233,8 @@ public function filterArrayRemovingFalsey(): Type
42004233 {
42014234 $ falseyTypes = StaticTypeFactory ::falsey ();
42024235 $ builder = ConstantArrayTypeBuilder ::createEmpty ();
4203- if (! $ this ->keyOrderKnown ) {
4204- $ builder ->forgetKeyOrder ();
4236+ if ($ this ->keyOrderKnown ) {
4237+ $ builder ->makeKeyOrderKnown ();
42054238 }
42064239 foreach ($ this ->keyTypes as $ i => $ keyType ) {
42074240 $ value = $ this ->valueTypes [$ i ];
@@ -4424,8 +4457,8 @@ public function getFiniteTypes(): array
44244457 }
44254458
44264459 $ emptyPartial = ConstantArrayTypeBuilder ::createEmpty ();
4427- if (! $ this ->keyOrderKnown ) {
4428- $ emptyPartial ->forgetKeyOrder ();
4460+ if ($ this ->keyOrderKnown ) {
4461+ $ emptyPartial ->makeKeyOrderKnown ();
44294462 }
44304463
44314464 /** @var list<ConstantArrayTypeBuilder> $partials */
0 commit comments