diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dea0c2182..7b981ccf6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -164,6 +164,11 @@ The file documents changes to the PHP_CodeSniffer project. - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Fixed bug #3854 : Fatal error when using Gitblame report in combination with `--basepath` and running from project subdirectory - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch +- Fixed bug #3856 : PSR12.Traits.UseDeclaration was using the wrong error code - SpacingAfterAs - for spacing issues after the use keyword + - These will now be reported using the SpacingAfterUse error code. + - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch +- Fixed bug #3856 : PSR12.Traits.UseDeclaration did not check spacing after use keyword for multi-line trait use statements + - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Fixed bug #3867 : Tokenizer/PHP: union type and intersection type operators were not correctly tokenized for static properties without explicit visibility - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Fixed bug #3877 : Filter names can be case-sensitive. The -h help text will now display the correct case for the available filters diff --git a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php index 109e23e9e6..f9fb808a9b 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, 'SpaceAfterAs', $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, 'SpaceAfterAs', $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 5c7e237df6..2b895e1973 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,