8000 [String] Add AbstractString::containsAny() by nicolas-grekas · Pull Request #35936 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[String] Add AbstractString::containsAny() #35936

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

Merged
merged 1 commit into from
Mar 16, 2020

Conversation

nicolas-grekas
Copy link
Member
@nicolas-grekas nicolas-grekas commented Mar 3, 2020
Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

We decided to not have a contains() method because we didn't know how to handle the case where several needles are passed. But https://wiki.php.net/rfc/str_contains made me reconsider this idea and I think containsAny() works great. WDYT?

Copy link
Member
@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking: why is the parameter string|string[] $needle instead of ...string $needles ?

Thanks!

{
$instance = static::createFromString($string);

$this->assertSame(null !== $instance->indexOf($needle), $instance->containsAny($needle));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does copy pasting the method content here actually test something? Shouldn't we create dedicated providers for this new methods?

Copy link
Member Author
@nicolas-grekas nicolas-grekas Mar 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better this way to me, easier to maintain.

@fancyweb
Copy link
Contributor
fancyweb commented Mar 3, 2020

Just asking: why is the parameter string|string[] $needle instead of ...string $needles ?

I think it's to pass directly (without alteration) the method argument to indexOf.

@nicolas-grekas
Copy link
Member Author
nicolas-grekas commented Mar 3, 2020

instead of ...string $needles

Clearly, for consistency.

@ro0NL
Copy link
Contributor
ro0NL commented Mar 6, 2020

containsAny('string') is a step back compared to contains('string') IMHO.

in this case does contains(string $needle) + containsAny(array $needles) makes sense?

assuming we dont ever need containsAll(array $needles). But if so, it's still possible ... (containsAny('string') + containsAll('string') would be another step back)

alternatively, shouldnt we advice to compose any/all logic in userland based on contains('string'), it's not like contains('a') || contains('b') is that bad actually ;) (same for contains('a') && contains('b')).

@nicolas-grekas
Copy link
Member Author

containsAny('string') is a step back compared to contains('string') IMHO.
in this case does contains(string $needle) + containsAny(array $needles) makes sense?

3 extra letters adding clarity win over a new method to me, especially considering that the class is already a collection of methods. We shouldn't inflate contains*() method in comparison to the other ones.

it's not like contains('a') || contains('b') is that bad actually

You should have a look again at the full interface of the class: several methods already support string|string[]. Not doing so for "contains" would be inconsistent.

Doing so is consistent, and useful.

@nicolas-grekas
Copy link
Member Author

Note that I'm fine naming this method contains() if that's the broader consensus. Please advise.

@javiereguiluz
Copy link
Member

I think that contains() is ambiguous. I will look at it and always wonder ... is it "contains all" or "contains any" ? 🤔

this is the same problem we suffered for years with the is_granted(array $roles) method ... is it "all roles required" or "any role required" ?

@ro0NL
Copy link
Contributor
ro0NL commented Mar 7, 2020

there's before/after() assuming OR already, is it less ambigious? is contains() specifically the confusing case ... thus worth a containsAny, but not before/afterAny? Is that inconsistent? if so, why dont we mind being inconsistent with contains(string $needle) also then?

long term, are we really, really, sure about a defacto standard String component/API... without a contains method 🙉

do we really mind adding N*3 methods (single, any, all) IF the case is ambiguous, in favor of clear behavior + type info... is it worth this debate we have?

@nicolas-grekas
Copy link
Member Author

Apparently ppl don't have an issue with before/after. I suppose that's because nobody expects a "beforeAny" or "afterAny". That wouldn't mean anything useful and it intuitively is a beforeAll and afterAll.

the discussion proves that "contains" can be confusing.

I'm therefor confirming that I definitely also prefer containsAny().

Thanks for raising the point.

@fabpot
Copy link
Member
fabpot commented Mar 16, 2020

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 34583b7 into symfony:master Mar 16, 2020
@nicolas-grekas nicolas-grekas deleted the str-contains-any branch April 5, 2020 16:59
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0