Skip to content

Commit 93c2f5b

Browse files
committed
Merge branch 'pulls/test-is-bs' of https://github.com/dhensby/PHP_CodeSniffer
2 parents 9b4f0f3 + 5d58565 commit 93c2f5b

14 files changed

+125
-2
lines changed

src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,31 @@ public function process(File $phpcsFile, $stackPtr)
142142
return;
143143
}
144144

145-
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
145+
// Find either the start of the next line or the beginning of the next statement,
146+
// whichever comes first.
147+
for ($end = ++$end; $end < $phpcsFile->numTokens; $end++) {
148+
if (isset(Tokens::$emptyTokens[$tokens[$end]['code']]) === false) {
149+
break;
150+
}
151+
152+
if ($tokens[$end]['column'] === 1) {
153+
// Reached the next line.
154+
break;
155+
}
156+
}
157+
158+
--$end;
159+
160+
if (($tokens[$end]['code'] === T_COMMENT
161+
|| isset(Tokens::$phpcsCommentTokens[$tokens[$end]['code']]) === true)
162+
&& substr($tokens[$end]['content'], 0, 2) === '/*'
163+
&& substr($tokens[$end]['content'], -2) !== '*/'
164+
) {
165+
// Multi-line block comments are not allowed as trailing comment after a use statement.
166+
--$end;
167+
}
168+
169+
$next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true);
146170

147171
if ($next === false || $tokens[$next]['code'] === T_CLOSE_TAG) {
148172
return;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
use Foo\Bar; // trailing comment
3+
// This should fail.
4+
5+
class Baz
6+
{
7+
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
use Foo\Bar; // trailing comment
3+
4+
// This should fail.
5+
6+
class Baz
7+
{
8+
9+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
use Foo\Bar; class Baz {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
use Foo\Bar;
3+
4+
class Baz {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
use Foo\Bar; /*
3+
* This multi-line comment shouldn't be allowed here.
4+
*/
5+
6+
class Baz
7+
{
8+
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
use Foo\Bar;
3+
4+
/*
5+
* This multi-line comment shouldn't be allowed here.
6+
*/
7+
8+
class Baz
9+
{
10+
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
use Foo\Bar; // trailing comment
3+
/*
4+
* This multi-line comment shouldn't be allowed here.
5+
*/
6+
7+
class Baz
8+
{
9+
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
use Foo\Bar; // trailing comment
3+
4+
/*
5+
* This multi-line comment shouldn't be allowed here.
6+
*/
7+
8+
class Baz
9+
{
10+
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
use Foo\Bar; // trailing comment
3+
/* phpcs:ignore Standard.Category.Sniff -- for reasons */
4+
5+
class Baz
6+
{
7+
8+
}

0 commit comments

Comments
 (0)