Skip to content

Commit 5e282de

Browse files
committed
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.
1 parent 782bf7d commit 5e282de

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ The file documents changes to the PHP_CodeSniffer project.
167167
- Fixed bug #3856 : PSR12.Traits.UseDeclaration was using the wrong error code - SpacingAfterAs - for spacing issues after the use keyword
168168
- These will now be reported using the SpacingAfterUse error code.
169169
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
170+
- Fixed bug #3856 : PSR12.Traits.UseDeclaration did not check spacing after use keyword for multi-line trait use statements
171+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
170172
- Fixed bug #3867 : Tokenizer/PHP: union type and intersection type operators were not correctly tokenized for static properties without explicit visibility
171173
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
172174
- Fixed bug #3877 : Filter names can be case-sensitive. The -h help text will now display the correct case for the available filters

src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,38 @@ public function process(File $phpcsFile, $stackPtr)
177177
}//end if
178178
}//end if
179179

180+
$error = 'Expected 1 space after USE in trait import statement; %s found';
181+
if ($tokens[($useToken + 1)]['code'] !== T_WHITESPACE) {
182+
$data = ['0'];
183+
$fix = $phpcsFile->addFixableError($error, $useToken, 'SpaceAfterUse', $data);
184+
if ($fix === true) {
185+
$phpcsFile->fixer->addContent($useToken, ' ');
186+
}
187+
} else if ($tokens[($useToken + 1)]['content'] !== ' ') {
188+
$next = $phpcsFile->findNext(T_WHITESPACE, ($useToken + 1), null, true);
189+
if ($tokens[$next]['line'] !== $tokens[$useToken]['line']) {
190+
$found = 'newline';
191+
} else {
192+
$found = $tokens[($useToken + 1)]['length'];
193+
}
194+
195+
$data = [$found];
196+
$fix = $phpcsFile->addFixableError($error, $useToken, 'SpaceAfterUse', $data);
197+
if ($fix === true) {
198+
if ($found === 'newline') {
199+
$phpcsFile->fixer->beginChangeset();
200+
for ($x = ($useToken + 1); $x < $next; $x++) {
201+
$phpcsFile->fixer->replaceToken($x, '');
202+
}
203+
204+
$phpcsFile->fixer->addContent($useToken, ' ');
205+
$phpcsFile->fixer->endChangeset();
206+
} else {
207+
$phpcsFile->fixer->replaceToken(($useToken + 1), ' ');
208+
}
209+
}
210+
}//end if
211+
180212
// Check the formatting of the statement.
181213
if (isset($tokens[$useToken]['scope_opener']) === true) {
182214
$this->processUseGroup($phpcsFile, $useToken);
@@ -652,38 +684,6 @@ protected function processUseStatement(File $phpcsFile, $stackPtr)
652684
{
653685
$tokens = $phpcsFile->getTokens();
654686

655-
$error = 'Expected 1 space after USE in trait import statement; %s found';
656-
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
657-
$data = ['0'];
658-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse', $data);
659-
if ($fix === true) {
660-
$phpcsFile->fixer->addContent($stackPtr, ' ');
661-
}
662-
} else if ($tokens[($stackPtr + 1)]['content'] !== ' ') {
663-
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
664-
if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
665-
$found = 'newline';
666-
} else {
667-
$found = $tokens[($stackPtr + 1)]['length'];
668-
}
669-
670-
$data = [$found];
671-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse', $data);
672-
if ($fix === true) {
673-
if ($found === 'newline') {
674-
$phpcsFile->fixer->beginChangeset();
675-
for ($x = ($stackPtr + 1); $x < $next; $x++) {
676-
$phpcsFile->fixer->replaceToken($x, '');
677-
}
678-
679-
$phpcsFile->fixer->addContent($stackPtr, ' ');
680-
$phpcsFile->fixer->endChangeset();
681-
} else {
682-
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
683-
}
684-
}
685-
}//end if
686-
687687
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON], ($stackPtr + 1));
688688
if ($next !== false && $tokens[$next]['code'] === T_COMMA) {
689689
$error = 'Each imported trait must have its own "use" import statement';

src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ClassName7
5454

5555
class ClassName8
5656
{
57-
use A , B,
57+
use A , B,
5858
C
5959
{
6060

src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getErrorList()
3030
29 => 2,
3131
30 => 1,
3232
42 => 1,
33-
57 => 3,
33+
57 => 4,
3434
59 => 3,
3535
61 => 1,
3636
63 => 5,

0 commit comments

Comments
 (0)