-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Normalize substr() behavior #6182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
77744ac
to
d7ebdce
Compare
This comment has been minimized.
This comment has been minimized.
|
I had a quick look in our code base as well as a few OS projects of mine, and none of the |
d7ebdce
to
b8f0a5a
Compare
Did you check the |
@Girgias I have now. substr(), mb_substr(), iconv_substr() and grapheme_substr() should now all have the same behavior. |
I'm assuming the mentioning of strpos() in this pull request is a typo. |
This just broke PHP_CodeSniffer... I will submit a fix there, however, I would like to kindly request for this change to get a very clear changelog entry as it is an insidious change and currently not mentioned in the changelog at all. What broke was the "$text1" case from the above code-sample being checked against |
A very recent change in PHP 8.0 changes the possible return values of the `substr()` function from: > Pre-PHP 8: > Returns the extracted part of string; or FALSE on failure, or an empty string. > PHP 8-RC1: > Returns the extracted part of string; or an empty string. This is an insidious change as basically all code (strict) checking the return value of `substr()` against `false` will now be broken. Checking the return value with `empty()` will fix this in a cross-version compatible manner as it allows for both `false` as well as an empty string being returned. This change broke the ignore annotations as implemented in PHPCS. The existing unit tests for the ignore annotations cover this change. Includes removing some unnecessary, duplicate function calls to `substr()`. Ref: php/php-src#6182
Thanks for the reminder, I've added an upgrading note in d7243ce. |
Thanks @nikic ! |
A very recent change in PHP 8.0 changes the possible return values of the `substr()` function from: > Pre-PHP 8: > Returns the extracted part of string; or FALSE on failure, or an empty string. > PHP 8-RC1: > Returns the extracted part of string; or an empty string. This is an insidious change as basically all code (strict) checking the return value of `substr()` against `false` will now be broken. Checking the return value with `empty()` will fix this in a cross-version compatible manner as it allows for both `false` as well as an empty string being returned. This change broke the ignore annotations as implemented in PHPCS. The existing unit tests for the ignore annotations cover this change. Includes removing some unnecessary, duplicate function calls to `substr()`. Ref: php/php-src#6182
A very recent change in PHP 8.0 changes the possible return values of the `substr()` function from: > Pre-PHP 8: > Returns the extracted part of string; or FALSE on failure, or an empty string. > PHP 8-RC1: > Returns the extracted part of string; or an empty string. This is an insidious change as basically all code (strict) checking the return value of `substr()` against `false` will now be broken. Checking the return value with `empty()` will fix this in a cross-version compatible manner as it allows for both `false` as well as an empty string being returned. This change broke the ignore annotations as implemented in PHPCS. The existing unit tests for the ignore annotations cover this change. Includes removing some unnecessary, duplicate function calls to `substr()`. Ref: php/php-src#6182
A very recent change in PHP 8.0 changes the possible return values of the `substr()` function from: > Pre-PHP 8: > Returns the extracted part of string; or FALSE on failure, or an empty string. > PHP 8-RC1: > Returns the extracted part of string; or an empty string. This is an insidious change as basically all code (strict) checking the return value of `substr()` against `false` will now be broken. Checking the return value with `empty()` will fix this in a cross-version compatible manner as it allows for both `false` as well as an empty string being returned. This change broke the ignore annotations as implemented in PHPCS. The existing unit tests for the ignore annotations cover this change. Includes removing some unnecessary, duplicate function calls to `substr()`. Ref: php/php-src#6182
Change substr() to not treat out-of-bounds start offsets or lengths specially and always return a string. Under the premise that this function shouldn't throw for out-of-bounds values, the current behavior of returning false in some cases doesn't serve a purpose -- it only corrupts the return type by adding a
|false
union.This makes
substr()
line up with the behavior ofmb_substr()
/mb_strcut()
.