@@ -37,7 +37,7 @@ final class PregSplitDynamicReturnTypeExtension implements DynamicFunctionReturn
37
37
{
38
38
39
39
public function __construct (
40
- private BitwiseFlagHelper $ bitwiseFlagAnalyser ,
40
+ private readonly BitwiseFlagHelper $ bitwiseFlagAnalyser ,
41
41
)
42
42
{
43
43
}
@@ -56,7 +56,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
56
56
$ patternArg = $ args [0 ];
57
57
$ subjectArg = $ args [1 ];
58
58
$ limitArg = $ args [2 ] ?? null ;
59
- $ flagArg = $ args [3 ] ?? null ;
59
+ $ capturesOffset = $ args [3 ] ?? null ;
60
60
$ patternType = $ scope ->getType ($ patternArg ->value );
61
61
$ patternConstantTypes = $ patternType ->getConstantStrings ();
62
62
if (count ($ patternConstantTypes ) > 0 ) {
@@ -75,7 +75,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
75
75
$ limits = [-1 ];
76
76
} else {
77
77
$ limitType = $ scope ->getType ($ limitArg ->value );
78
- if (!$ limitType -> isInteger ()-> yes () && ! $ limitType-> isString ()-> yes ( )) {
78
+ if (!$ this -> isIntOrStringValue ( $ limitType )) {
79
79
return new ErrorType ();
80
80
}
81
81
foreach ($ limitType ->getConstantScalarValues () as $ limit ) {
@@ -87,11 +87,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
87
87
}
88
88
89
89
$ flags = [];
90
- if ($ flagArg === null ) {
90
+ if ($ capturesOffset === null ) {
91
91
$ flags = [0 ];
92
92
} else {
93
- $ flagType = $ scope ->getType ($ flagArg ->value );
94
- if (!$ flagType -> isInteger ()-> yes () && ! $ flagType-> isString ()-> yes ( )) {
93
+ $ flagType = $ scope ->getType ($ capturesOffset ->value );
94
+ if (!$ this -> isIntOrStringValue ( $ flagType )) {
95
95
return new ErrorType ();
96
96
}
97
97
foreach ($ flagType ->getConstantScalarValues () as $ flag ) {
@@ -103,8 +103,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
103
103
}
104
104
105
105
if ($ this ->isPatternOrSubjectEmpty ($ patternConstantTypes , $ subjectConstantTypes )) {
106
- $ returnNonEmptyStrings = $ flagArg !== null && $ this -> bitwiseFlagAnalyser -> bitwiseOrContainsConstant ( $ flagArg -> value , $ scope , ' PREG_SPLIT_NO_EMPTY ' )-> yes ();
107
- if ( $ returnNonEmptyStrings ) {
106
+ if ( $ capturesOffset !== null
107
+ && $ this -> bitwiseFlagAnalyser -> bitwiseOrContainsConstant ( $ capturesOffset -> value , $ scope , ' PREG_SPLIT_NO_EMPTY ' )-> yes () ) {
108
108
$ returnStringType = TypeCombinator::intersect (
109
109
new StringType (),
110
110
new AccessoryNonEmptyStringType (),
@@ -122,8 +122,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
122
122
);
123
123
124
124
$ returnInternalValueType = $ returnStringType ;
125
- if ($ flagArg !== null ) {
126
- $ flagState = $ this ->bitwiseFlagAnalyser ->bitwiseOrContainsConstant ($ flagArg ->value , $ scope , 'PREG_SPLIT_OFFSET_CAPTURE ' );
125
+ if ($ capturesOffset !== null ) {
126
+ $ flagState = $ this ->bitwiseFlagAnalyser ->bitwiseOrContainsConstant ($ capturesOffset ->value , $ scope , 'PREG_SPLIT_OFFSET_CAPTURE ' );
127
127
if ($ flagState ->yes ()) {
128
128
$ capturedArrayListType = TypeCombinator::intersect (
129
129
new ArrayType (new IntegerType (), $ capturedArrayType ),
@@ -133,7 +133,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
133
133
if ($ subjectType ->isNonEmptyString ()->yes ()) {
134
134
$ capturedArrayListType = TypeCombinator::intersect ($ capturedArrayListType , new NonEmptyArrayType ());
135
135
}
136
-
137
136
return TypeCombinator::union ($ capturedArrayListType , new ConstantBooleanType (false ));
138
137
}
139
138
if ($ flagState ->maybe ()) {
@@ -179,6 +178,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
179
178
}
180
179
}
181
180
}
181
+
182
182
return TypeCombinator::union (...$ resultTypes );
183
183
}
184
184
@@ -201,4 +201,8 @@ private function isValidPattern(string $pattern): bool
201
201
return true ;
202
202
}
203
203
204
+ private function isIntOrStringValue (Type $ type ): bool
205
+ {
206
+ return $ type ->isInteger ()->yes () || $ type ->isString ()->yes () || $ type ->isConstantScalarValue ()->yes ();
207
+ }
204
208
}
0 commit comments