From bc674f16026a11a469216a4254e19a89a2dad63c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 19 Jul 2023 03:06:15 +0200 Subject: [PATCH 1/2] PSR12.Traits.UseDeclaration: fix typo in error code The `processUseStatement()` method checking single-line trait `use` statements checks the spacing after the `use` keyword, but the error did not have the correct error code. --- src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php index 113e8b9f84..0751a34af3 100644 --- a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php +++ b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php @@ -655,7 +655,7 @@ protected function processUseStatement(File $phpcsFile, $stackPtr) $error = 'Expected 1 space after USE in trait import statement; %s found'; if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { $data = ['0']; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterAs', $data); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse', $data); if ($fix === true) { $phpcsFile->fixer->addContent($stackPtr, ' '); } @@ -668,7 +668,7 @@ protected function processUseStatement(File $phpcsFile, $stackPtr) } $data = [$found]; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterAs', $data); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse', $data); if ($fix === true) { if ($found === 'newline') { $phpcsFile->fixer->beginChangeset(); From 7b0475b5cc0df0ffc7f86276d586ababf346b046 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 19 Jul 2023 03:13:35 +0200 Subject: [PATCH 2/2] PSR12.Traits.UseDeclaration: check spacing after `use` keyword for multi-line statements While the `processUseStatement()` method checking single-line trait `use` statements would check the spacing after the `use` keyword, the `processUseGroup()` method checking multi-line trait `use` statements did not execute that same check, while the rule applies to both single- as well as multi-line `use` statements. By moving the check for the spacing after the `use` keyword to the `process()` method, it will now be executed for both situations. Tested by adjusting a pre-existing test. --- .../Sniffs/Traits/UseDeclarationSniff.php | 64 +++++++++---------- .../Tests/Traits/UseDeclarationUnitTest.inc | 2 +- .../Tests/Traits/UseDeclarationUnitTest.php | 2 +- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php index 0751a34af3..2095dc1e1c 100644 --- a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php +++ b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php @@ -177,6 +177,38 @@ public function process(File $phpcsFile, $stackPtr) }//end if }//end if + $error = 'Expected 1 space after USE in trait import statement; %s found'; + if ($tokens[($useToken + 1)]['code'] !== T_WHITESPACE) { + $data = ['0']; + $fix = $phpcsFile->addFixableError($error, $useToken, 'SpaceAfterUse', $data); + if ($fix === true) { + $phpcsFile->fixer->addContent($useToken, ' '); + } + } else if ($tokens[($useToken + 1)]['content'] !== ' ') { + $next = $phpcsFile->findNext(T_WHITESPACE, ($useToken + 1), null, true); + if ($tokens[$next]['line'] !== $tokens[$useToken]['line']) { + $found = 'newline'; + } else { + $found = $tokens[($useToken + 1)]['length']; + } + + $data = [$found]; + $fix = $phpcsFile->addFixableError($error, $useToken, 'SpaceAfterUse', $data); + if ($fix === true) { + if ($found === 'newline') { + $phpcsFile->fixer->beginChangeset(); + for ($x = ($useToken + 1); $x < $next; $x++) { + $phpcsFile->fixer->replaceToken($x, ''); + } + + $phpcsFile->fixer->addContent($useToken, ' '); + $phpcsFile->fixer->endChangeset(); + } else { + $phpcsFile->fixer->replaceToken(($useToken + 1), ' '); + } + } + }//end if + // Check the formatting of the statement. if (isset($tokens[$useToken]['scope_opener']) === true) { $this->processUseGroup($phpcsFile, $useToken); @@ -652,38 +684,6 @@ protected function processUseStatement(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $error = 'Expected 1 space after USE in trait import statement; %s found'; - if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { - $data = ['0']; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse', $data); - if ($fix === true) { - $phpcsFile->fixer->addContent($stackPtr, ' '); - } - } else if ($tokens[($stackPtr + 1)]['content'] !== ' ') { - $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); - if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { - $found = 'newline'; - } else { - $found = $tokens[($stackPtr + 1)]['length']; - } - - $data = [$found]; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse', $data); - if ($fix === true) { - if ($found === 'newline') { - $phpcsFile->fixer->beginChangeset(); - for ($x = ($stackPtr + 1); $x < $next; $x++) { - $phpcsFile->fixer->replaceToken($x, ''); - } - - $phpcsFile->fixer->addContent($stackPtr, ' '); - $phpcsFile->fixer->endChangeset(); - } else { - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); - } - } - }//end if - $next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON], ($stackPtr + 1)); if ($next !== false && $tokens[$next]['code'] === T_COMMA) { $error = 'Each imported trait must have its own "use" import statement'; diff --git a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc index c8ad746a73..152121cf1e 100644 --- a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc @@ -54,7 +54,7 @@ class ClassName7 class ClassName8 { - use A , B, + use A , B, C { diff --git a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php index 797a2912e7..83736ad2d5 100644 --- a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php +++ b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php @@ -30,7 +30,7 @@ public function getErrorList() 29 => 2, 30 => 1, 42 => 1, - 57 => 3, + 57 => 4, 59 => 3, 61 => 1, 63 => 5,