Skip to content

Commit 4d4c38c

Browse files
committed
Fixed bug #2673 : PSR12.Traits.UseDeclaration does not allow comments or blank lines between use statements
1 parent f6732bc commit 4d4c38c

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4040
- Fixed bug #2663 : Generic.NamingConventions.ConstructorName complains about old constructor in interfaces
4141
- Fixed bug #2664 : PSR12.Files.OpenTag incorrectly identifies PHP file with only an opening tag
4242
- Fixed bug #2665 : PSR12.Files.ImportStatement should not apply to traits
43+
- Fixed bug #2673 : PSR12.Traits.UseDeclaration does not allow comments or blank lines between use statements
4344
</notes>
4445
<contents>
4546
<dir name="/">

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ public function process(File $phpcsFile, $stackPtr)
135135
}//end if
136136
}//end if
137137
} else {
138-
// Make sure this use statement immediately follows the previous one.
138+
// Make sure this use statement is not on the same line as the previous one.
139139
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
140-
if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
141-
$error = 'Each imported trait must be on the line after the previous import';
140+
if ($prev !== false && $tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
141+
$error = 'Each imported trait must be on it\'s own line';
142142
$prevNonWs = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
143143
if ($prevNonWs !== $prev) {
144144
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeImport');

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

+18
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,21 @@ class Foo implements Bar
131131

132132
use Baz;
133133
}
134+
135+
class ClassName
136+
{
137+
/**
138+
* DocBlockContent
139+
*/
140+
use FirstTrait;
141+
142+
/**
143+
* DocBlockContent
144+
*/
145+
use SecondTrait;
146+
147+
/**
148+
* DocBlockContent
149+
*/
150+
use ThirdTrait;
151+
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class ClassName9
7777
class ClassName10
7878
{
7979
use TransactionTrait;
80+
8081
use PermissionAwareTrait;
82+
8183
use FirstTrait;
8284
use SecondTrait;
8385
use ThirdTrait;
@@ -124,3 +126,21 @@ class Foo implements Bar
124126

125127
use Baz;
126128
}
129+
130+
class ClassName
131+
{
132+
/**
133+
* DocBlockContent
134+
*/
135+
use FirstTrait;
136+
137+
/**
138+
* DocBlockContent
139+
*/
140+
use SecondTrait;
141+
142+
/**
143+
* DocBlockContent
144+
*/
145+
use ThirdTrait;
146+
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function getErrorList()
3838
71 => 1,
3939
73 => 2,
4040
76 => 1,
41-
84 => 1,
42-
86 => 3,
41+
86 => 2,
4342
103 => 1,
4443
112 => 1,
4544
122 => 1,

0 commit comments

Comments
 (0)