Skip to content

Commit 2433a7b

Browse files
authored
refactor(phpcs): Upgrade to 4.x in composer and adapt all sniffs (#3548073)
1 parent adb8595 commit 2433a7b

31 files changed

+195
-588
lines changed

.github/workflows/testing.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
10+
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
1111
extra-tests: ['0']
1212
# We only need to run PHPStan and Drupal core regression tests once on
1313
# the latest PHP version.
@@ -82,4 +82,14 @@ jobs:
8282
# ignored temporarily, add them with the --ignore option.
8383
run: |
8484
cd drupal/core
85-
../../vendor/bin/phpcs -p -s --parallel=$(nproc)
85+
# Fix Drupal core PHPCS config by replacing removed sniffs.
86+
sed -i 's/Drupal.Classes.ClassCreateInstance/SlevomatCodingStandard.ControlStructures.NewWithParentheses/g' phpcs.xml.dist
87+
sed -i 's/Drupal.Classes.UnusedUseStatement/SlevomatCodingStandard.Namespaces.UnusedUses/g' phpcs.xml.dist
88+
sed -i '/<rule ref="SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch" \/>/a \ <rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace"/>' phpcs.xml.dist
89+
find . -type f -name "*.php" -exec sed -i 's#// phpcs:ignore Drupal.Classes.PropertyDeclaration, Drupal.NamingConventions.ValidVariableName.LowerCamelName, Drupal.Commenting.VariableComment.Missing#// phpcs:ignore Drupal.NamingConventions.ValidVariableName.LowerCamelName,PSR2.Classes.PropertyDeclaration.Underscore#g' {} +
90+
find . -type f -name "*.php" -exec sed -i 's#// @codingStandardsIgnoreLine#// phpcs:ignore#g' {} +
91+
find . -type f -name "*.php" -exec sed -i 's#// @codingStandardsIgnoreFile#// phpcs:ignoreFile#g' {} +
92+
find . -type f -exec sed -i '/\/\/ phpcs:ignore/ s/, /,/g' {} +
93+
find . -type f -name "*.php" -exec sed -i 's#// @codingStandardsIgnoreStart#// phpcs:disable#g' {} +
94+
find . -type f -name "*.php" -exec sed -i 's#// @codingStandardsIgnoreEnd#// phpcs:enable#g' {} +
95+
../../vendor/bin/phpcs -p -s --parallel=$(nproc) --ignore=lib/Drupal/Core/Command/GenerateTheme.php,modules/mysql/tests/src/Kernel/mysql/Console/DbDumpCommandTest.php

coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php

Lines changed: 0 additions & 125 deletions
This file was deleted.

coder_sniffer/Drupal/Sniffs/Classes/FullyQualifiedNamespaceSniff.php

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
/**
1617
* Checks that class references do not use FQN but use statements.
@@ -30,7 +31,10 @@ class FullyQualifiedNamespaceSniff implements Sniff
3031
*/
3132
public function register()
3233
{
33-
return [T_NS_SEPARATOR];
34+
return [
35+
T_NAME_FULLY_QUALIFIED,
36+
T_NAME_QUALIFIED,
37+
];
3438

3539
}//end register()
3640

@@ -61,7 +65,9 @@ public function process(File $phpcsFile, $stackPtr)
6165

6266
// We are only interested in a backslash embedded between strings, which
6367
// means this is a class reference with more than one namespace part.
64-
if ($tokens[($stackPtr - 1)]['code'] !== T_STRING || $tokens[($stackPtr + 1)]['code'] !== T_STRING) {
68+
if (substr_count($tokens[$stackPtr]['content'], '\\') === 1
69+
&& strpos($tokens[$stackPtr]['content'], '\\') === 0
70+
) {
6571
return;
6672
}
6773

@@ -71,25 +77,12 @@ public function process(File $phpcsFile, $stackPtr)
7177
}
7278

7379
// Check if this is a use statement and ignore those.
74-
$before = $phpcsFile->findPrevious([T_STRING, T_NS_SEPARATOR, T_WHITESPACE, T_COMMA, T_AS], $stackPtr, null, true);
80+
$before = $phpcsFile->findPrevious((Tokens::EMPTY_TOKENS + Tokens::NAME_TOKENS + [T_COMMA => T_COMMA, T_AS => T_AS]), ($stackPtr - 1), null, true);
7581
if ($tokens[$before]['code'] === T_USE || $tokens[$before]['code'] === T_NAMESPACE) {
76-
return $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR, T_WHITESPACE, T_COMMA, T_AS], ($stackPtr + 1), null, true);
77-
} else {
78-
$before = $phpcsFile->findPrevious([T_STRING, T_NS_SEPARATOR, T_WHITESPACE], $stackPtr, null, true);
79-
}
80-
81-
// If this is a namespaced function call then ignore this because use
82-
// statements for functions are not possible in PHP 5.5 and lower.
83-
$after = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR, T_WHITESPACE], $stackPtr, null, true);
84-
if ($tokens[$after]['code'] === T_OPEN_PARENTHESIS
85-
&& $tokens[$before]['code'] !== T_NEW
86-
&& $tokens[$before]['code'] !== T_ATTRIBUTE
87-
) {
88-
return ($after + 1);
82+
return;
8983
}
9084

91-
$fullName = $phpcsFile->getTokensAsString(($before + 1), ($after - 1 - $before));
92-
$fullName = trim($fullName, "\ \n");
85+
$fullName = trim($tokens[$stackPtr]['content'], '\\ ');
9386
$parts = explode('\\', $fullName);
9487
$className = end($parts);
9588

@@ -101,7 +94,7 @@ public function process(File $phpcsFile, $stackPtr)
10194
$useStatement = $phpcsFile->findNext(T_USE, 0);
10295
while ($useStatement !== false && empty($tokens[$useStatement]['conditions']) === true) {
10396
$endPtr = $phpcsFile->findEndOfStatement($useStatement);
104-
$useEnd = ($phpcsFile->findNext([T_STRING, T_NS_SEPARATOR, T_WHITESPACE], ($useStatement + 1), null, true) - 1);
97+
$useEnd = ($phpcsFile->findNext((Tokens::EMPTY_TOKENS + Tokens::NAME_TOKENS), ($useStatement + 1), null, true) - 1);
10598
$useFullName = trim($phpcsFile->getTokensAsString(($useStatement + 1), ($useEnd - $useStatement)));
10699

107100
// Check if use statement contains an alias.
@@ -163,18 +156,11 @@ public function process(File $phpcsFile, $stackPtr)
163156
if ($fix === true) {
164157
$phpcsFile->fixer->beginChangeset();
165158

166-
// Replace the fully qualified name with the local name.
167-
for ($i = ($before + 1); $i < $after; $i++) {
168-
if ($tokens[$i]['code'] !== T_WHITESPACE) {
169-
$phpcsFile->fixer->replaceToken($i, '');
170-
}
171-
}
172-
173159
// Use alias name if available.
174160
if ($aliasName !== false) {
175-
$phpcsFile->fixer->addContentBefore(($after - 1), $aliasName);
161+
$phpcsFile->fixer->replaceToken($stackPtr, $aliasName);
176162
} else {
177-
$phpcsFile->fixer->addContentBefore(($after - 1), $className);
163+
$phpcsFile->fixer->replaceToken($stackPtr, $className);
178164
}
179165

180166
// Insert use statement at the beginning of the file if it is not there
@@ -216,10 +202,6 @@ public function process(File $phpcsFile, $stackPtr)
216202
$phpcsFile->fixer->endChangeset();
217203
}//end if
218204

219-
// Continue after this class reference so that errors for this are not
220-
// flagged multiple times.
221-
return $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], ($stackPtr + 1), null, true);
222-
223205
}//end process()
224206

225207

0 commit comments

Comments
 (0)