Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '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';
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