Skip to content
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

Ignore annotations are broken on PHP 8.0 #3135

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Oct 2, 2020

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

@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Oct 2, 2020
@gsherwood gsherwood added this to the 3.5.7 milestone Oct 2, 2020
@jrfnl jrfnl force-pushed the feature/php-8-compat-bugfix-ignore-annotations branch from 028f868 to 3bc9ebf Compare October 3, 2020 01:43
@Ayesh
Copy link

Ayesh commented Oct 4, 2020

I was bit with this too. substr and iconv_substr functionality is changed recently in PHP master branch. mb_substr and grapheme_substr has not.

@jrfnl
Copy link
Contributor Author

jrfnl commented Oct 4, 2020

@Ayesh Yup, it's a nasty one. At least it's now mentioned in the changelog (after I made a remark about it).

@jrfnl jrfnl force-pushed the feature/php-8-compat-bugfix-ignore-annotations branch from 3bc9ebf to 4877700 Compare October 4, 2020 16:38
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
@gsherwood gsherwood changed the title [Hot fix] PHP 8.0 compatibility: ignore annotations are broken Ignore annotations are broken on PHP 8.0 Oct 13, 2020
gsherwood added a commit that referenced this pull request Oct 13, 2020
@gsherwood gsherwood merged commit 4877700 into squizlabs:master Oct 13, 2020
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Oct 13, 2020
@gsherwood
Copy link
Member

I'm very thankful you found and fixed this. Bit of a rough last minute change.

@jrfnl jrfnl deleted the feature/php-8-compat-bugfix-ignore-annotations branch October 13, 2020 21:45
@jrfnl
Copy link
Contributor Author

jrfnl commented Oct 13, 2020

Well, at least there were tests in place to find it quickly ;-)

From a PHP Core perspective, I do think it is a good change as it makes the function return type of these functions more consistent going forward, but yes, BC-break-wise it's nasty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

None yet

3 participants