Skip to content

Commit 4877700

Browse files
committed
PHP 8.0 compatibility: bug fix - ignore annotations are broken
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
1 parent 33a4891 commit 4877700

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/Tokenizers/Tokenizer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ private function createPositionMap()
424424
$disabledSniffs = [];
425425

426426
$additionalText = substr($commentText, 14);
427-
if ($additionalText === false) {
427+
if (empty($additionalText) === true) {
428428
$ignoring = ['.all' => true];
429429
} else {
430-
$parts = explode(',', substr($commentText, 13));
430+
$parts = explode(',', $additionalText);
431431
foreach ($parts as $sniffCode) {
432432
$sniffCode = trim($sniffCode);
433433
$disabledSniffs[$sniffCode] = true;
@@ -459,10 +459,10 @@ private function createPositionMap()
459459
$enabledSniffs = [];
460460

461461
$additionalText = substr($commentText, 13);
462-
if ($additionalText === false) {
462+
if (empty($additionalText) === true) {
463463
$ignoring = null;
464464
} else {
465-
$parts = explode(',', substr($commentText, 13));
465+
$parts = explode(',', $additionalText);
466466
foreach ($parts as $sniffCode) {
467467
$sniffCode = trim($sniffCode);
468468
$enabledSniffs[$sniffCode] = true;
@@ -520,10 +520,10 @@ private function createPositionMap()
520520
$ignoreRules = [];
521521

522522
$additionalText = substr($commentText, 13);
523-
if ($additionalText === false) {
523+
if (empty($additionalText) === true) {
524524
$ignoreRules = ['.all' => true];
525525
} else {
526-
$parts = explode(',', substr($commentText, 13));
526+
$parts = explode(',', $additionalText);
527527
foreach ($parts as $sniffCode) {
528528
$ignoreRules[trim($sniffCode)] = true;
529529
}

0 commit comments

Comments
 (0)