From ed833786e531d8b60b9bb691bea8b69be90c6949 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 23 Nov 2025 17:19:18 +0100 Subject: [PATCH 1/2] refactor(ElseIf): Replace sniff with upstream PSR2 version --- .../Sniffs/ControlStructures/ElseIfSniff.php | 76 ------------------- coder_sniffer/Drupal/ruleset.xml | 1 + .../ControlStructures/ElseIfUnitTest.php | 44 ----------- tests/Drupal/bad/BadUnitTest.php | 13 +++- .../ElseIfUnitTest.inc | 0 .../ElseIfUnitTest.inc.fixed | 0 6 files changed, 13 insertions(+), 121 deletions(-) delete mode 100644 coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php delete mode 100644 tests/Drupal/ControlStructures/ElseIfUnitTest.php rename tests/Drupal/{ControlStructures => bad}/ElseIfUnitTest.inc (100%) rename tests/Drupal/{ControlStructures => bad}/ElseIfUnitTest.inc.fixed (100%) diff --git a/coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php b/coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php deleted file mode 100644 index 9df7a2b8..00000000 --- a/coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php +++ /dev/null @@ -1,76 +0,0 @@ - - */ - public function register() - { - return [T_ELSE]; - } - - - /** - * Processes this test, when one of its tokens is encountered. - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in the - * stack passed in $tokens. - * - * @return void - */ - public function process(File $phpcsFile, $stackPtr) - { - - $tokens = $phpcsFile->getTokens(); - - $nextNonWhiteSpace = $phpcsFile->findNext( - T_WHITESPACE, - ($stackPtr + 1), - null, - true, - null, - true - ); - - if ($tokens[$nextNonWhiteSpace]['code'] === T_IF) { - $fix = $phpcsFile->addFixableError('Use "elseif" in place of "else if"', $nextNonWhiteSpace, 'ElseIfDeclaration'); - if ($fix === true) { - $phpcsFile->fixer->beginChangeset(); - $phpcsFile->fixer->replaceToken($stackPtr, 'elseif'); - for ($i = ($stackPtr + 1); $i < $nextNonWhiteSpace; $i++) { - if ($tokens[$i]['code'] === T_WHITESPACE) { - $phpcsFile->fixer->replaceToken($i, ''); - } - } - - $phpcsFile->fixer->replaceToken($nextNonWhiteSpace, ''); - $phpcsFile->fixer->endChangeset(); - } - } - } -} diff --git a/coder_sniffer/Drupal/ruleset.xml b/coder_sniffer/Drupal/ruleset.xml index 21f1634e..27b5607e 100644 --- a/coder_sniffer/Drupal/ruleset.xml +++ b/coder_sniffer/Drupal/ruleset.xml @@ -114,6 +114,7 @@ + diff --git a/tests/Drupal/ControlStructures/ElseIfUnitTest.php b/tests/Drupal/ControlStructures/ElseIfUnitTest.php deleted file mode 100644 index 82396a03..00000000 --- a/tests/Drupal/ControlStructures/ElseIfUnitTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - */ - protected function getErrorList(string $testFile): array - { - return [ - 6 => 1, - 9 => 1, - ]; - } - - - /** - * Returns the lines where warnings should occur. - * - * The key of the array should represent the line number and the value - * should represent the number of warnings that should occur on that line. - * - * @param string $testFile The name of the file being tested. - * - * @return array - */ - protected function getWarningList(string $testFile): array - { - return []; - } -} diff --git a/tests/Drupal/bad/BadUnitTest.php b/tests/Drupal/bad/BadUnitTest.php index d7d8b4f8..538cf9bb 100644 --- a/tests/Drupal/bad/BadUnitTest.php +++ b/tests/Drupal/bad/BadUnitTest.php @@ -194,7 +194,7 @@ protected function getErrorList(string $testFile): array 216 => 2, 218 => 1, 222 => 2, - 225 => 2, + 225 => 1, 230 => 1, 233 => 1, 237 => 1, @@ -397,6 +397,11 @@ protected function getErrorList(string $testFile): array 21 => 1, 31 => 1, ]; + case 'ElseIfUnitTest.inc': + return [ + 1 => 1, + 9 => 1, + ]; case 'FinallySpacingUnitTest.inc': return [ 1 => 1, @@ -473,6 +478,7 @@ protected function getWarningList(string $testFile): array 156 => 1, 193 => 1, 202 => 1, + 225 => 1, 360 => 1, 363 => 1, 366 => 1, @@ -494,6 +500,11 @@ protected function getWarningList(string $testFile): array 824 => 1, 836 => 1, ]; + case 'ElseIfUnitTest.inc': + return [ + 6 => 1, + 9 => 1, + ]; }//end switch return []; diff --git a/tests/Drupal/ControlStructures/ElseIfUnitTest.inc b/tests/Drupal/bad/ElseIfUnitTest.inc similarity index 100% rename from tests/Drupal/ControlStructures/ElseIfUnitTest.inc rename to tests/Drupal/bad/ElseIfUnitTest.inc diff --git a/tests/Drupal/ControlStructures/ElseIfUnitTest.inc.fixed b/tests/Drupal/bad/ElseIfUnitTest.inc.fixed similarity index 100% rename from tests/Drupal/ControlStructures/ElseIfUnitTest.inc.fixed rename to tests/Drupal/bad/ElseIfUnitTest.inc.fixed From b8b73d9e45bf2749d5f95f51abc20aae4f657099 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 23 Nov 2025 17:21:43 +0100 Subject: [PATCH 2/2] fix up core tests --- .github/workflows/testing.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index e271b194..42ba67bb 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -101,4 +101,5 @@ jobs: sed -i '//d' phpcs.xml.dist sed -i 's/Drupal.Classes.InterfaceName/Generic.NamingConventions.InterfaceNameSuffix/g' phpcs.xml.dist sed -i '//d' phpcs.xml.dist + sed -i 's/Drupal.ControlStructures.ElseIf/PSR2.ControlStructures.ElseIfDeclaration/g' phpcs.xml.dist ../../vendor/bin/phpcs -p -s --parallel=$(nproc) --exclude=Drupal.ControlStructures.ControlSignature --ignore=lib/Drupal/Core/Command/GenerateTheme.php,modules/mysql/tests/src/Kernel/mysql/Console/DbDumpCommandTest.php,modules/big_pipe/tests/modules/big_pipe_test/src/BigPipePlaceholderTestCases.php,tests/fixtures/plugins/CustomPlugin.php,modules/package_manager/tests/modules/package_manager_test_api/src/ApiController.php,modules/views/tests/src/Kernel/Plugin/StyleGridTest.php