Skip to content
Snippets Groups Projects
Commit 7fb01f1e authored by Larry Garfield's avatar Larry Garfield Committed by Georg Ringer
Browse files

[TASK] Switch specific strpos() usages with equivalent string methods

Due to a former lack of missing proper functions to
check for strings starting, ending or containing
sub-strings, workarounds with strpos() constructs
have been used in TYPO3 as well as in just about
every PHP application.

PHP finally implemented proper native functions for
these kind of tasks, which make code more readable
and avoid to easily oversee bugs.

Additionally, php-cs-fixer provides a rule-set to replace
most of these changes, see:
https://cs.symfony.com/doc/ruleSets/PHP80MigrationRisky.html

This rule is now enabled and used to ensure cleaner code
in the future. The rule is applied to ensure green cgl checks.

Replacements covered by php-cs-fixer rule:

* replace `strpos($haystack, $needle) === 0`
  with `str_starts_with($haystack, $needle)`
* replace `strpos($haystack, $needle) !== 0`
  with `!str_starts_with($haystack, $needle)`
* replace `strpos($haystack, $needle) !== false`
  with `str_contains($haystack, $needle)`
* replace `strpos($haystack, $needle) === false`
  with `!str_contains($haystack, $needle)`

Some additional (manual) replacements on nearby places to
changed code places:

* replace `substr($haystack, strlen($needle) * -1) === $needle`
  with `str_ends_with($expression, ']')`
* replace `substr($haystack, strlen($needle) * -1) !== $needle`
  with `!str_ends_with($expression, ']')`

Used commands:

> Build/Scripts/runTests.sh -s cgl

Resolves: #97438
Releases: main
Change-Id: Ia1206d4dd08b43597bfe794389f6db8bdeb3de0f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74365


Tested-by: default avatarcore-ci <typo3@b13.com>
Tested-by: default avatarNikita Hovratov <nikita.h@live.de>
Tested-by: default avatarStefan Bürk <stefan@buerk.tech>
Tested-by: default avatarGeorg Ringer <georg.ringer@gmail.com>
Reviewed-by: default avatarNikita Hovratov <nikita.h@live.de>
Reviewed-by: default avatarcrell <larry@garfieldtech.com>
Reviewed-by: default avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: default avatarGeorg Ringer <georg.ringer@gmail.com>
parent 0a757e06
Branches
Tags
Showing
with 31 additions and 28 deletions
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment