Skip to content

Commit c206ccc

Browse files
committed
feat: change variable name, fix: check type of limit/flag
1 parent 9388d23 commit c206ccc

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/Type/Php/PregSplitDynamicReturnTypeExtension.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class PregSplitDynamicReturnTypeExtension implements DynamicFunctionReturn
3737
{
3838

3939
public function __construct(
40-
private BitwiseFlagHelper $bitwiseFlagAnalyser,
40+
private readonly BitwiseFlagHelper $bitwiseFlagAnalyser,
4141
)
4242
{
4343
}
@@ -56,7 +56,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5656
$patternArg = $args[0];
5757
$subjectArg = $args[1];
5858
$limitArg = $args[2] ?? null;
59-
$flagArg = $args[3] ?? null;
59+
$capturesOffset = $args[3] ?? null;
6060
$patternType = $scope->getType($patternArg->value);
6161
$patternConstantTypes = $patternType->getConstantStrings();
6262
if (count($patternConstantTypes) > 0) {
@@ -75,7 +75,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7575
$limits = [-1];
7676
} else {
7777
$limitType = $scope->getType($limitArg->value);
78-
if (!$limitType->isInteger()->yes() && !$limitType->isString()->yes()) {
78+
if (!$this->isIntOrStringValue($limitType)) {
7979
return new ErrorType();
8080
}
8181
foreach ($limitType->getConstantScalarValues() as $limit) {
@@ -87,11 +87,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
8787
}
8888

8989
$flags = [];
90-
if ($flagArg === null) {
90+
if ($capturesOffset === null) {
9191
$flags = [0];
9292
} 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)) {
9595
return new ErrorType();
9696
}
9797
foreach ($flagType->getConstantScalarValues() as $flag) {
@@ -103,8 +103,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
103103
}
104104

105105
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()) {
108108
$returnStringType = TypeCombinator::intersect(
109109
new StringType(),
110110
new AccessoryNonEmptyStringType(),
@@ -122,8 +122,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
122122
);
123123

124124
$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');
127127
if ($flagState->yes()) {
128128
$capturedArrayListType = TypeCombinator::intersect(
129129
new ArrayType(new IntegerType(), $capturedArrayType),
@@ -133,7 +133,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
133133
if ($subjectType->isNonEmptyString()->yes()) {
134134
$capturedArrayListType = TypeCombinator::intersect($capturedArrayListType, new NonEmptyArrayType());
135135
}
136-
137136
return TypeCombinator::union($capturedArrayListType, new ConstantBooleanType(false));
138137
}
139138
if ($flagState->maybe()) {
@@ -179,6 +178,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
179178
}
180179
}
181180
}
181+
182182
return TypeCombinator::union(...$resultTypes);
183183
}
184184

@@ -201,4 +201,8 @@ private function isValidPattern(string $pattern): bool
201201
return true;
202202
}
203203

204+
private function isIntOrStringValue(Type $type): bool
205+
{
206+
return $type->isInteger()->yes() || $type->isString()->yes() || $type->isConstantScalarValue()->yes();
207+
}
204208
}

0 commit comments

Comments
 (0)