Skip to content

Commit

Permalink
Fixed bug #2673 : PSR12.Traits.UseDeclaration does not allow comments…
Browse files Browse the repository at this point in the history
… or blank lines between use statements
  • Loading branch information
gsherwood committed Oct 27, 2019
1 parent f6732bc commit 4d4c38c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2663 : Generic.NamingConventions.ConstructorName complains about old constructor in interfaces
- Fixed bug #2664 : PSR12.Files.OpenTag incorrectly identifies PHP file with only an opening tag
- Fixed bug #2665 : PSR12.Files.ImportStatement should not apply to traits
- Fixed bug #2673 : PSR12.Traits.UseDeclaration does not allow comments or blank lines between use statements
</notes>
<contents>
<dir name="/">
Expand Down
6 changes: 3 additions & 3 deletions src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ public function process(File $phpcsFile, $stackPtr)
}//end if
}//end if
} else {
// Make sure this use statement immediately follows the previous one.
// Make sure this use statement is not on the same line as the previous one.
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
$error = 'Each imported trait must be on the line after the previous import';
if ($prev !== false && $tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Each imported trait must be on it\'s own line';
$prevNonWs = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($prevNonWs !== $prev) {
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeImport');
Expand Down
18 changes: 18 additions & 0 deletions src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,21 @@ class Foo implements Bar

use Baz;
}

class ClassName
{
/**
* DocBlockContent
*/
use FirstTrait;

/**
* DocBlockContent
*/
use SecondTrait;

/**
* DocBlockContent
*/
use ThirdTrait;
}
20 changes: 20 additions & 0 deletions src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class ClassName9
class ClassName10
{
use TransactionTrait;

use PermissionAwareTrait;

use FirstTrait;
use SecondTrait;
use ThirdTrait;
Expand Down Expand Up @@ -124,3 +126,21 @@ class Foo implements Bar

use Baz;
}

class ClassName
{
/**
* DocBlockContent
*/
use FirstTrait;

/**
* DocBlockContent
*/
use SecondTrait;

/**
* DocBlockContent
*/
use ThirdTrait;
}
3 changes: 1 addition & 2 deletions src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function getErrorList()
71 => 1,
73 => 2,
76 => 1,
84 => 1,
86 => 3,
86 => 2,
103 => 1,
112 => 1,
122 => 1,
Expand Down

0 comments on commit 4d4c38c

Please sign in to comment.