-
Notifications
You must be signed in to change notification settings - Fork 477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve preg_split()
function ReturnType
#3757
base: 2.0.x
Are you sure you want to change the base?
Conversation
This pull request has been marked as ready for review. |
} else { | ||
$flagType = $scope->getType($flagArg->value); | ||
$flags = $flagType->getConstantScalarValues(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By replacing it as follows, type checking within multiple Constant loops will no longer be necessary.
$flags = [];
$flagType = $scope->getType($flagArg->value);
foreach ($flagType->getConstantScalarValues() as $flag) {
if (!is_int()) {
return new ErrorType();
}
$flags[] = $flag;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved 8cb3030
@@ -9081,7 +9081,7 @@ | |||
'preg_replace' => ['string|array|null', 'regex'=>'string|array', 'replace'=>'string|array', 'subject'=>'string|array', 'limit='=>'int', '&w_count='=>'int'], | |||
'preg_replace_callback' => ['string|array|null', 'regex'=>'string|array', 'callback'=>'callable(array<int|string, string>):string', 'subject'=>'string|array', 'limit='=>'int', '&w_count='=>'int'], | |||
'preg_replace_callback_array' => ['string|array|null', 'pattern'=>'array<string,callable>', 'subject'=>'string|array', 'limit='=>'int', '&w_count='=>'int'], | |||
'preg_split' => ['list<string>|false', 'pattern'=>'string', 'subject'=>'string', 'limit='=>'?int', 'flags='=>'int'], | |||
'preg_split' => ['__benevolent<list<string>|list<array{string, int<0, max>}>|false>', 'pattern'=>'string', 'subject'=>'string', 'limit='=>'?int', 'flags='=>'int'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other preg_ method are not using benevolent union, so I would think more consistent to not use a benevolent union here too.
I developed further extended the extension for the
preg_split()
function.The
preg_split()
function can specify ReturnType with the following cases:$pattern
argument is invalid.$pattern
and$subject
arguments are constants.$subject
argument is non-empty-string.$flag
argument is set to one or more of the following:PREG_SPLIT_OFFSET_CAPTURE
,PREG_SPLIT_NO_EMPTY
, orPREG_SPLIT_DELIM_CAPTURE
.The detailed cases are specified in the test cases.
Additionally, the current default return type is set to
list<string>|false
. However, depending on the flags, it also returnlist<array{string, int<0, max>}>
. To account for this, I have added support for this possibility and applied a benevolent union to cover all three types. So, I set__benevolent<list<string>|list<array{string, int<0, max>}>|false>
to the basic return type.