Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR2/UseDeclaration: simplify the fix #1

Merged
Merged
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
31 changes: 23 additions & 8 deletions src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,32 @@ public function process(File $phpcsFile, $stackPtr)
}

$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
$candidateComment = $end;
if ($end === false) {
return;
}

// Find either the start of the next line or the beginning of the next statement,
// whichever comes first.
for ($end = ++$end; $end < $phpcsFile->numTokens; $end++) {
if (isset(Tokens::$emptyTokens[$tokens[$end]['code']]) === false) {
break;
}

do {
$nextComment = $candidateComment;
$candidateComment = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextComment + 1));
} while ($candidateComment !== false && $tokens[$candidateComment]['line'] === $tokens[$end]['line']);
if ($tokens[$end]['column'] === 1) {
// Reached the next line.
break;
}
}

$end = $nextComment;
--$end;

if ($end === false) {
return;
if (($tokens[$end]['code'] === T_COMMENT
|| isset(Tokens::$phpcsCommentTokens[$tokens[$end]['code']]) === true)
&& substr($tokens[$end]['content'], 0, 2) === '/*'
&& substr($tokens[$end]['content'], -2) !== '*/'
) {
// Multi-line block comments are not allowed as trailing comment after a use statement.
--$end;
}

$next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
use Foo\Bar; class Baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
use Foo\Bar;

class Baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
use Foo\Bar; /*
* This multi-line comment shouldn't be allowed here.
*/

class Baz
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
use Foo\Bar;

/*
* This multi-line comment shouldn't be allowed here.
*/

class Baz
{

}
10 changes: 10 additions & 0 deletions src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.13.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
use Foo\Bar; // trailing comment
/*
* This multi-line comment shouldn't be allowed here.
*/

class Baz
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
use Foo\Bar; // trailing comment

/*
* This multi-line comment shouldn't be allowed here.
*/

class Baz
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
use Foo\Bar; // trailing comment
/* phpcs:ignore Standard.Category.Sniff -- for reasons */

class Baz
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
use Foo\Bar; // trailing comment

/* phpcs:ignore Standard.Category.Sniff -- for reasons */

class Baz
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function getErrorList($testFile='')
19 => 1,
];
case 'UseDeclarationUnitTest.10.inc':
case 'UseDeclarationUnitTest.11.inc':
case 'UseDeclarationUnitTest.12.inc':
case 'UseDeclarationUnitTest.13.inc':
case 'UseDeclarationUnitTest.14.inc':
return [2 => 1];
default:
return [];
Expand Down