Problem/Motivation

Part of #3324560: Replace strpos/substr with str_starts_with() / str_contains() / str_ends_with().

Proposed resolution

  1. Replace strpos($a, $b) === 0 with str_starts_with($a, $b)
  2. Replace strpos($a, $b) !== 0 with !str_starts_with($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

CommentFileSizeAuthor
#12 Selection_047.png85.09 KBrpayanm

Issue fork drupal-3328443

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

xjm created an issue. See original summary.

xjm’s picture

Title: Replace most strpos() === 0 or !== 0 with str_starts_with » Replace most strpos() === 0 or !== 0 with str_starts_with()

xjm’s picture

Status: Active » Needs review
xjm’s picture

Issue summary: View changes
xjm’s picture

Issue summary: View changes
xjm’s picture

Issue summary: View changes
xjm’s picture

Issue summary: View changes
xjm’s picture

Issue summary: View changes
rpayanm’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The patch does not apply anymore.

xjm’s picture

Status: Needs work » Needs review

Fixed the merge conflict, which was due to some code adopting str_starts_with() and using a slightly different logic structure than the one in HEAD.

rpayanm’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: -Needs reroll
StatusFileSize
new85.09 KB

I reviewed the patch like this:
1. Checked out the branch 10.1.x and pulled the latest changes, applied this patch.
2. Checked the patterns strpos($haystack, $needle) === 0 and strpos($haystack, $needle, 0) === 0 become to str_starts_with($haystack, $needle). And strpos($haystack, $needle) !== 0 become !str_starts_with($haystack, $needle). I checked each change visually with a Phpstorm tool (Compare with branch) on every changed line, it looks like this:
file
Everything was fine!
3. Searched for the strpos patterns mentioned before in the Drupal core code. But I found some changes to be done:
core/lib/Drupal/Core/Config/Entity/Query/Query.php:
strpos($id, $value) === 0
core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/PhpSelection.php:
strpos($label, $match) === 0
core/lib/Drupal/Core/Config/Entity/Query/Condition.php:
strpos($value, $condition['value']) === 0
core/lib/Drupal/Core/ParamConverter/EntityConverter.php:
if (strpos($definition['type'], '{') !== FALSE) {
core/modules/image/src/PathProcessor/PathProcessorImageStyles.php:
if (strpos($path, '/' . $directory_path . '/styles/') === 0) {
core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php:
if (strpos($path, '//') === 0) {

xjm’s picture

Status: Needs work » Needs review

Thanks @rpayanm.

core/lib/Drupal/Core/Config/Entity/Query/Query.php:
strpos($id, $value) === 0
core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/PhpSelection.php:
strpos($label, $match) === 0
core/lib/Drupal/Core/Config/Entity/Query/Condition.php:
strpos($value, $condition['value']) === 0
core/lib/Drupal/Core/ParamConverter/EntityConverter.php:
if (strpos($definition['type'], '{') !== FALSE) {
core/modules/image/src/PathProcessor/PathProcessorImageStyles.php:
if (strpos($path, '/' . $directory_path . '/styles/') === 0) {
core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php:
if (strpos($path, '//') === 0) {

These are explicitly excluded from the scope to avoid merge conflicts for the other three sibling merge requests. As per the issue summary:

A few are deliberately omitted where they would cause merge conflicts with other child issues.

The idea is to merge these four conflict-free merge requests, then clean up the last dangling references in a final issue. That's why the title starts with "Replace most ..." :)

See #3324560: Replace strpos/substr with str_starts_with() / str_contains() / str_ends_with() for more information.

rpayanm’s picture

Status: Needs review » Reviewed & tested by the community

oh! great, so the patch looks good to me.

xjm’s picture

Saving credits.

xjm credited alexpott.

xjm credited catch.

xjm credited longwave.

xjm’s picture

Adding credits from the original MR. (All I did was split it up; others created it first.)

  • catch committed 652709e4 on 10.1.x
    Issue #3328443 by xjm, rpayanm, alexpott, catch, longwave: Replace most...
catch’s picture

Status: Reviewed & tested by the community » Fixed

All the changes look good. Agreed with doing the easy ones here and stragglers at the end.

Status: Fixed » Closed (fixed)

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