Skip to content

[Php80] Skip non-zero strpos() offset in StrContainsRector - #8640

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
guillaume-sainthillier:strcontains-skip-offset
Oct 12, 2026
Merged

TomasVotruba merged 1 commit into
rectorphp:mainfrom
guillaume-sainthillier:strcontains-skip-offset

Conversation

@guillaume-sainthillier

Copy link
Copy Markdown
Contributor

str_contains() has no offset, so offset strpos() calls are rewritten with a substr() copy of the haystack (#3086, #5257):

-false === strpos($content, self::TABLE_BORDER, $end + $length)
+!str_contains(substr($content, $end + $length), self::TABLE_BORDER)

This turns an allocation-free search into an O(n) copy on every call, which is what the offset was there to avoid. Real case: silarhi/caf-parser PaymentSlipParser::extractTableContent() walks a large file with offsets, and had to skip the rule (silarhi/caf-parser#111).

1,000 calls on a 1 MB string, offset 1000, PHP 8.5:

case strpos(..., $offset) str_contains(substr(...))
needle ~1 KB after offset 0.05 ms 15.4 ms
needle absent 20.1 ms 36.3 ms

Change:

@TomasVotruba

Copy link
Copy Markdown
Member

Valid change, thank you 👍

@TomasVotruba
TomasVotruba merged commit db207c7 into rectorphp:main Oct 12, 2026
46 of 49 checks passed
@guillaume-sainthillier
guillaume-sainthillier deleted the strcontains-skip-offset 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