Skip to content

Commit 49c7c53

Browse files
authored
Merge pull request #375 from PHPCSStandards/feature/universal-nofqntruefalsenull-fix-for-phpcs-3.13.3-4.0
Universal/NoFQNTrueFalseNull: fix for changed tokenization in PHPCS 3.13.3 and 4.0
2 parents 639d162 + f6441ad commit 49c7c53

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ PHPCSExtra is a collection of sniffs and standards for use with [PHP_CodeSniffer
4343
## Minimum Requirements
4444

4545
* PHP 5.4 or higher.
46-
* [PHP_CodeSniffer][phpcs-gh] version **3.13.0** or higher.
47-
* [PHPCSUtils][phpcsutils-gh] version **1.1.0** or higher.
46+
* [PHP_CodeSniffer][phpcs-gh] version **3.13.4** or higher.
47+
* [PHPCSUtils][phpcsutils-gh] version **1.1.2** or higher.
4848

4949

5050
## Installation
@@ -58,15 +58,15 @@ Installing via Composer is highly recommended.
5858
Run the following from the root of your project:
5959
```bash
6060
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
61-
composer require --dev phpcsstandards/phpcsextra:"^1.2.0"
61+
composer require --dev phpcsstandards/phpcsextra:"^1.3.0"
6262
```
6363

6464
### Composer Global Installation
6565

6666
Alternatively, you may want to install this standard globally:
6767
```bash
6868
composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
69-
composer global require --dev phpcsstandards/phpcsextra:"^1.2.0"
69+
composer global require --dev phpcsstandards/phpcsextra:"^1.3.0"
7070
```
7171

7272
### Updating to a newer version

Universal/Sniffs/PHP/NoFQNTrueFalseNullSniff.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Sniffs\PHP;
1212

13+
use PHP_CodeSniffer\Config;
1314
use PHP_CodeSniffer\Files\File;
1415
use PHP_CodeSniffer\Sniffs\Sniff;
1516
use PHP_CodeSniffer\Util\Tokens;
@@ -31,18 +32,17 @@ final class NoFQNTrueFalseNullSniff implements Sniff
3132
*/
3233
public function register()
3334
{
34-
return [
35-
// PHPCS 3.x on PHP < 8.0.
35+
$targets = [
3636
\T_TRUE,
3737
\T_FALSE,
3838
\T_NULL,
39+
];
3940

40-
// PHPCS 3.x on PHP >= 8.0.
41-
\T_STRING,
41+
if (\version_compare(Config::VERSION, '4.0.0', '>=') === true) {
42+
$targets[] = \T_NS_SEPARATOR;
43+
}
4244

43-
// PHPCS 4.x.
44-
\T_NAME_FULLY_QUALIFIED,
45-
];
45+
return $targets;
4646
}
4747

4848
/**
@@ -62,17 +62,21 @@ public function process(File $phpcsFile, $stackPtr)
6262
$content = $tokens[$stackPtr]['content'];
6363
$contentLC = \strtolower($content);
6464

65-
if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
65+
if ($contentLC === '\true' || $contentLC === '\false' || $contentLC === '\null') {
6666
// PHPCS 4.x.
67-
if ($contentLC !== '\true' && $contentLC !== '\false' && $contentLC !== '\null') {
67+
} elseif ($tokens[$stackPtr]['code'] === \T_NS_SEPARATOR) {
68+
// PHPCS 4.x for code which is a parse error on PHP 8.0+.
69+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
70+
if ($tokens[$next]['code'] !== \T_STRING) {
6871
return;
6972
}
70-
} else {
71-
// PHPCS 3.x.
72-
if ($contentLC !== 'true' && $contentLC !== 'false' && $contentLC !== 'null') {
73+
74+
$nextContentLC = \strtolower($tokens[$next]['content']);
75+
if ($nextContentLC !== 'true' && $nextContentLC !== 'false' && $nextContentLC !== 'null') {
7376
return;
7477
}
75-
78+
} else {
79+
// PHPCS 3.x.
7680
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
7781
if ($tokens[$prev]['code'] !== \T_NS_SEPARATOR) {
7882
return;
@@ -97,9 +101,12 @@ public function process(File $phpcsFile, $stackPtr)
97101
);
98102

99103
if ($fix === true) {
100-
if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
104+
if ($contentLC === '\true' || $contentLC === '\false' || $contentLC === '\null') {
101105
// PHPCS 4.x.
102106
$phpcsFile->fixer->replaceToken($stackPtr, \ltrim($tokens[$stackPtr]['content'], '\\'));
107+
} elseif ($tokens[$stackPtr]['code'] === \T_NS_SEPARATOR) {
108+
// PHPCS 4.x for code which is a parse error on PHP 8.0+.
109+
$phpcsFile->fixer->replaceToken($stackPtr, '');
103110
} else {
104111
// PHPCS 3.x.
105112
$phpcsFile->fixer->replaceToken($prev, '');

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
},
2323
"require" : {
2424
"php" : ">=5.4",
25-
"squizlabs/php_codesniffer" : "^3.13.0 || ^4.0",
26-
"phpcsstandards/phpcsutils" : "^1.1.0"
25+
"squizlabs/php_codesniffer" : "^3.13.4 || ^4.0",
26+
"phpcsstandards/phpcsutils" : "^1.1.2"
2727
},
2828
"require-dev" : {
2929
"php-parallel-lint/php-parallel-lint": "^1.4.0",

0 commit comments

Comments
 (0)