Skip to content

[Php80] Skip loose comparisons that differ from strict on StrStartsWithRector and StrEndsWithRector - #8643

Closed
guillaume-sainthillier wants to merge 1 commit into
rectorphp:mainfrom
guillaume-sainthillier:strstartswith-skip-loose-comparison
Closed

guillaume-sainthillier wants to merge 1 commit into
rectorphp:mainfrom
guillaume-sainthillier:strstartswith-skip-loose-comparison

Conversation

@guillaume-sainthillier

Copy link
Copy Markdown
Contributor

Some loose comparisons handled since #3246 don't behave like str_starts_with() / str_ends_with():

var_dump(strpos('abc', 'x') == 0);            // true, false == 0
var_dump(str_starts_with('abc', 'x'));        // false

var_dump(substr('x+1', -2) == '01');          // true, '+1' == '01' compares as numbers
var_dump(str_ends_with('x+1', '01'));         // false

Change:

  • strpos() == 0 / != 0: skipped
  • substr(...) == $needle / !=: only refactored when the needle is a non-numeric string literal, where == behaves like ===
  • strncmp() / substr_compare() loose comparisons: unchanged, they always return an int

Follow-up to #8641, which does the same for StrContainsRector / MbStrContainsRector.

$isMatch = substr($haystack, -strlen($needle)) == $needle;

$isMatch = $needle == substr($haystack, -strlen($needle));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you revert this? While technically it's correct removal, in legacy projects where this change would be valid most people use ==.

@guillaume-sainthillier

Copy link
Copy Markdown
Contributor Author

Closing, as it follows the same loose-comparison reasoning that was declined in #8641. Thanks!

@guillaume-sainthillier
guillaume-sainthillier deleted the strstartswith-skip-loose-comparison branch October 12, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants