Skip to content

Commit

Permalink
Merge pull request #1 from jrfnl/feature/test-is-bs-simplification
Browse files Browse the repository at this point in the history
PSR2/UseDeclaration: simplify the fix
  • Loading branch information
dhensby authored Feb 18, 2018
2 parents 1f1e919 + 1530543 commit 5d58565
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 8 deletions.
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

0 comments on commit 5d58565

Please sign in to comment.