Skip to content

Commit

Permalink
PHP 8.0 compatibility: bug fix - ignore annotations are broken
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Oct 4, 2020
1 parent 33a4891 commit 4877700
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Tokenizers/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ private function createPositionMap()
$disabledSniffs = [];

$additionalText = substr($commentText, 14);
if ($additionalText === false) {
if (empty($additionalText) === true) {
$ignoring = ['.all' => true];
} else {
$parts = explode(',', substr($commentText, 13));
$parts = explode(',', $additionalText);
foreach ($parts as $sniffCode) {
$sniffCode = trim($sniffCode);
$disabledSniffs[$sniffCode] = true;
Expand Down Expand Up @@ -459,10 +459,10 @@ private function createPositionMap()
$enabledSniffs = [];

$additionalText = substr($commentText, 13);
if ($additionalText === false) {
if (empty($additionalText) === true) {
$ignoring = null;
} else {
$parts = explode(',', substr($commentText, 13));
$parts = explode(',', $additionalText);
foreach ($parts as $sniffCode) {
$sniffCode = trim($sniffCode);
$enabledSniffs[$sniffCode] = true;
Expand Down Expand Up @@ -520,10 +520,10 @@ private function createPositionMap()
$ignoreRules = [];

$additionalText = substr($commentText, 13);
if ($additionalText === false) {
if (empty($additionalText) === true) {
$ignoreRules = ['.all' => true];
} else {
$parts = explode(',', substr($commentText, 13));
$parts = explode(',', $additionalText);
foreach ($parts as $sniffCode) {
$ignoreRules[trim($sniffCode)] = true;
}
Expand Down

0 comments on commit 4877700

Please sign in to comment.