@@ -945,7 +945,7 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
945
945
}
946
946
947
947
if ($ keyTypesCount + $ offset <= 0 ) {
948
- // A negative offset cannot reach left outside the array
948
+ // A negative offset cannot reach left outside the array twice
949
949
$ offset = 0 ;
950
950
}
951
951
@@ -1006,6 +1006,106 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
1006
1006
return $ builder ->getArray ();
1007
1007
}
1008
1008
1009
+ public function spliceArray (Type $ offsetType , Type $ lengthType , Type $ replacementType ): Type
1010
+ {
1011
+ $ keyTypesCount = count ($ this ->keyTypes );
1012
+
1013
+ $ offset = $ offsetType instanceof ConstantIntegerType ? $ offsetType ->getValue () : null ;
1014
+
1015
+ if ($ lengthType instanceof ConstantIntegerType) {
1016
+ $ length = $ lengthType ->getValue ();
1017
+ } elseif ($ lengthType ->isNull ()->yes ()) {
1018
+ $ length = $ keyTypesCount ;
1019
+ } else {
1020
+ $ length = null ;
1021
+ }
1022
+
1023
+ if (
1024
+ $ offset === null || $ length === null
1025
+ || (count ($ this ->optionalKeys ) > 0 && ($ offset < 0 || $ length < 0 )) // Negative offsets or lengths with optional keys are not supported yet
1026
+ ) {
1027
+ return $ this ->degradeToGeneralArray ()
1028
+ ->spliceArray ($ offsetType , $ lengthType , $ replacementType );
1029
+ }
1030
+
1031
+ if ($ keyTypesCount + $ offset <= 0 ) {
1032
+ // A negative offset cannot reach left outside the array twice
1033
+ $ offset = 0 ;
1034
+ }
1035
+
1036
+ if ($ keyTypesCount + $ length <= 0 ) {
1037
+ // A negative length cannot reach left outside the array twice
1038
+ $ length = 0 ;
1039
+ }
1040
+
1041
+ if ($ offset < 0 ) {
1042
+ $ offset = $ keyTypesCount + $ offset ;
1043
+ }
1044
+
1045
+ if ($ length < 0 ) {
1046
+ $ length = $ keyTypesCount - $ offset + $ length ;
1047
+ }
1048
+
1049
+ $ removeKeysCount = 0 ;
1050
+ $ optionalKeysIgnored = 0 ;
1051
+ $ optionalKeysBeforeReplacement = 0 ;
1052
+ $ builder = ConstantArrayTypeBuilder::createEmpty ();
1053
+ for ($ i = 0 ;; $ i ++) {
1054
+ $ isOptional = $ this ->isOptionalKey ($ i );
1055
+
1056
+ if ($ i < $ offset && $ isOptional ) {
1057
+ $ optionalKeysIgnored ++;
1058
+ $ optionalKeysBeforeReplacement ++;
1059
+ }
1060
+
1061
+ if ($ i === $ offset + $ optionalKeysBeforeReplacement ) {
1062
+ // When the offset is reached we have to a) put the replacement array in and b) remove $length elements
1063
+ $ removeKeysCount = $ length ;
1064
+ $ optionalKeysIgnored += $ optionalKeysBeforeReplacement ;
1065
+
1066
+ $ replacementArrayType = $ replacementType ->toArray ();
1067
+ $ constantArrays = $ replacementArrayType ->getConstantArrays ();
1068
+ if (count ($ constantArrays ) === 1 ) {
1069
+ $ valuesArray = $ constantArrays [0 ]->getValuesArray ();
1070
+ for ($ j = 0 , $ jMax = count ($ valuesArray ->keyTypes ); $ j < $ jMax ; $ j ++) {
1071
+ $ builder ->setOffsetValueType (null , $ valuesArray ->valueTypes [$ j ], $ valuesArray ->isOptionalKey ($ j ));
1072
+ }
1073
+ } else {
1074
+ $ builder ->degradeToGeneralArray ();
1075
+ $ builder ->setOffsetValueType (null , $ replacementArrayType ->getIterableValueType (), true );
1076
+ }
1077
+ }
1078
+
1079
+ if (!isset ($ this ->keyTypes [$ i ])) {
1080
+ break ;
1081
+ }
1082
+
1083
+ if ($ removeKeysCount > 0 ) {
1084
+ $ removeKeysCount --;
1085
+
1086
+ if ($ optionalKeysIgnored === 0 ) {
1087
+ if ($ isOptional && $ removeKeysCount === 0 ) {
1088
+ $ optionalKeysIgnored ++;
1089
+ }
1090
+ continue ;
1091
+ }
1092
+ }
1093
+
1094
+ if (!$ isOptional && $ optionalKeysIgnored > 0 ) {
1095
+ $ optionalKeysIgnored --;
1096
+ $ isOptional = true ;
1097
+ }
1098
+
1099
+ $ builder ->setOffsetValueType (
1100
+ $ this ->keyTypes [$ i ]->isInteger ()->no () ? $ this ->keyTypes [$ i ] : null ,
1101
+ $ this ->valueTypes [$ i ],
1102
+ $ isOptional ,
1103
+ );
1104
+ }
1105
+
1106
+ return $ builder ->getArray ();
1107
+ }
1108
+
1009
1109
public function isIterableAtLeastOnce (): TrinaryLogic
1010
1110
{
1011
1111
$ keysCount = count ($ this ->keyTypes );
0 commit comments