str_(starts|ends)_with variadic needle#18825
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 givenIf this should be maintained the signature should be str_starts_with(string $haystack, string $needle, string ...$additionalNeedles): boolI'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. |
|
Is this PR still being pursued? |
|
@Girgias nope. I still believe it's a good idea, but not enough to pursue it myself, I have too much other stuff to deal with these days. Closing. Hope someone else may pursue it in the future though! |
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")): ...