-
Notifications
You must be signed in to change notification settings - Fork 7.9k
str_(starts|ends)_with variadic needle #18825
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
base: master
Are you sure you want to change the base?
Conversation
str_starts_with(string $haystack, string ...$needle): bool
This comment was marked as outdated.
This comment was marked as outdated.
This requires a discussion on the mailing list and probably an RFC. |
asked internals. https://news-web.php.net/php.internals/127637 |
I see a theoretical issue with using variadic specifically: it will be difficult to add new arguments, like That can be avoided by accepting That also more closely resemble how Python handles it |
A signature of Current behavior: str_starts_with('foo'); // Uncaught ArgumentCountError: str_starts_with() expects exactly 2 arguments, 1 given If this should be maintained the signature should be str_starts_with(string $haystack, string $needle, string ...$additionalNeedles): bool I'm not sure how this is handled with other core functions that accept variadic inputs or if this minor behavior change is considered a bc break. |
@bcremer this branch, as currently written, still requires a minimum of 2 arguments.
it's subtle, but i believe
means
would mean |
@divinity76 Also tools like phpstan loose the information about the required arguments when using the provided stub. See: https://phpstan.org/r/8c158f00-493b-44c4-8cb6-e5b87b53511e Is the stub file auto-generated or can it be modified manually? |
it can be modified manually. do you mean it should be
then? |
Disclaimer, I'm not a core contributor but user of static code analysis in userland code. But yes, the following signature should match the implementation and produces the same error message in phpstan as before: function str_starts_with(string $haystack, string $needle, string ...$needles): bool |
The first parameter is ignored there (probably for backwards compatibility reasons). |
Allowing zero needles would not be a BC break (it's widening the signature) and would be consistent with:
returning false for an empty array. |
make str_starts_with and str_ends_with variadic:
a simple example could be
which seems easier than
inspired by Python's
str.startswith()
which supports python-tuples likeif str.startswith(("foo","bar")): ...