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

Prove regression from #1893 #1899

Merged
merged 9 commits into from
Feb 18, 2018
26 changes: 25 additions & 1 deletion src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,31 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
// 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;
}

if ($tokens[$end]['column'] === 1) {
// Reached the next line.
break;
}
}

--$end;

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);

if ($next === false || $tokens[$next]['code'] === T_CLOSE_TAG) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
use Foo\Bar; // trailing comment
// This should fail.

class Baz
{

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

// This should fail.

class Baz
{

}
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
@@ -1,4 +1,8 @@
<?php
use Foo\Bar; //comment
use Foo\Bar;

// This should pass.
class Baz
{

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

// This should pass.
class Baz
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public function getErrorList($testFile='')
18 => 1,
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 [];
}//end switch
Expand Down