Replace most strpos() !== FALSE or === FALSE with str_contains()

Created on 21 December 2022, almost 2 years ago
Updated 18 March 2023, almost 2 years ago

Problem/Motivation

Part of 🌱 Replace strpos/substr with PHP 8's str_starts_with() / str_contains() / str_ends_with() Active .

Proposed resolution

  1. Replace strpos($a, $b) !== FALSE with str_contains($a, $b)
  2. Replace strpos($a, $b) === FALSE with !str_contains($a, $b)
  3. A few are deliberately omitted where they would cause merge conflicts with other child issues.

Remaining tasks

  • When reviewing, consider using git diff --color-words or git diff --porecelain.
  • Pay attention to the !. It should be in the expression once either before the change or after the change.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

📌 Task
Status

Fixed

Version

10.1

Component
Other 

Last updated about 3 hours ago

Created by

🇺🇸United States xjm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • First commit to issue fork.
  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Status changed to Needs review almost 2 years ago
  • 🇫🇷France nod_ Lille

    While I'll look into it

  • 🇺🇸United States xjm

    There was a merge conflict due to 📌 Replace most strpos() !== FALSE or === FALSE with str_contains() Fixed moving a line we're changing to a different file. Addressed with the latest merge.

  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    Been avoiding this ticket a week haha

    Finally got around to reviewing, searching for strpos( and reviewing each found a few files

    PathProcessorImageStyles (few instances)
    BookJavascriptTest (not sure if tests are included)
    UserNameConstraintValidator

  • First commit to issue fork.
  • Status changed to Needs review almost 2 years ago
  • 🇨🇴Colombia jidrone

    Hi,

    I added the cases mentioned by @smustgrave and others that I found.

    I'm only taking into account the following patterns as suggested in the description:

    • strpos($a, $b) !== FALSE
    • strpos($a, $b) === FALSE

    I was not including:

    • ($pos = strpos($input, '@', $pos)) !== false
    • ($start = strpos($message, '{')) !== FALSE
    • strpos($value, $condition['value']) === 0
  • Status changed to RTBC almost 2 years ago
  • 🇺🇸United States smustgrave

    Thank you!

    I think not including is correct. Think that would make the scope of this too large IMO. This looks good!

  • Status changed to Needs work almost 2 years ago
  • 🇬🇧United Kingdom catch

    There are at least two changes to str_starts_with(), which isn't the scope defined in #16. All the in-scope changes look fine, but let's please limit the MR to those, makes it a lot harder to scan the hundreds of lines of diff if there are multiple patterns dealt with at once.

    Note to self: got down to AliasPathProcessor

  • Status changed to Needs review almost 2 years ago
  • 🇨🇴Colombia jidrone

    Reverted the 2 cases that were not strpos replacements.

  • Status changed to RTBC almost 2 years ago
  • 🇺🇸United States smustgrave

    Thanks @catch for the review.

    • catch committed b3fac4a0 on 10.1.x
      Issue #3328454 by xjm, Bhanu951, jidrone, smustgrave: Replace most...
  • Status changed to Fixed almost 2 years ago
  • 🇬🇧United Kingdom catch

    I'm getting a phpstan error trying to commit this locally, which I don't think is the patch but I sometimes see weird failures from phpstan when it runs on particular files.

    I've committed this with --no-verify, and if I'm wrong, well mea culpa.

    Committed b3fac4a and pushed to 10.1.x. Thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024