Skip to content

Commit

Permalink
PSR12.Traits.UseDeclaration: check spacing after use keyword for mu…
Browse files Browse the repository at this point in the history
…lti-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.
  • Loading branch information
jrfnl committed Nov 11, 2023
1 parent 4e81b6e commit 0efbab8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
64 changes: 32 additions & 32 deletions src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ClassName7

class ClassName8
{
use A , B,
use A , B,
C
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getErrorList()
29 => 2,
30 => 1,
42 => 1,
57 => 3,
57 => 4,
59 => 3,
61 => 1,
63 => 5,
Expand Down

0 comments on commit 0efbab8

Please sign in to comment.