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
Merged

Conversation

dhensby
Copy link
Contributor

@dhensby dhensby commented Feb 15, 2018

see #1898

The test that was merged with #1893 didn't actually test that lines after a user statement weren't blank as PHPCS would determine this to be the end of the file due to lack of class declaration or other valid code lines.

@dhensby
Copy link
Contributor Author

dhensby commented Feb 15, 2018

Right, I've pushed a fix that allows a trailing comment on the same line as a use statement.

I'd have thought this should be a config option, but as #1893 was merged without a new config I've assumed it's intended that trailing comments are allowed.

@dhensby dhensby force-pushed the pulls/test-is-bs branch 2 times, most recently from 8723383 to 9577ba8 Compare February 15, 2018 14:32
@@ -137,12 +137,19 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
$nextComment = $phpcsFile->findNext(T_COMMENT, ($end + 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Tokens::$emptyTokens to allow for phpcs:ignore, etc.?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: What we are checking for here is the next inline comment, if that's on the same line as the use declaration then that can be classed as the "end" of the use declaration instead of the semicolon.

As I see it: Allowing for any "empty token" would mean a /* could be allowed (which it shouldn't be IMO) and other such tokens that shouldn't or couldn't be there.

I'm happy to be told that it should use Tokens::$emptyTokens I'm just of the opinion that the only thing that can come next should be an inline comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.2.0 changed how overrides are annotated, which added several tokens for comments containing phpcs: overrides: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.2.0

My PR changed T_COMMENT to Tokens::$emptyTokens in order to allow the T_PHPCS_* tokens. That also allows T_DOC_COMMENT_*, which seemed safe but isn't 100% correct in hindsight. I think this should be an array with T_COMMENT and T_PHPCS_*.

@jrfnl Does that sound right to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a look at this PR later today.

@dhensby
Copy link
Contributor Author

dhensby commented Feb 15, 2018

@iammattcoleman i've quickly tried to add Tokens::$emptyTokens and a while loop to get this working but I'm now getting other failures:

PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

...............................................................  63 / 294 ( 21%)
............................................................... 126 / 294 ( 42%)
............................................................... 189 / 294 ( 64%)
.F.............................................SS.............. 252 / 294 ( 85%)
.......................................S..                      294 / 294 (100%)

Tests generated 618 unique error codes; 350 were fixable (56.63%)

Time: 27.52 seconds, Memory: 38.00MB

There was 1 failure:

1) PHP_CodeSniffer\Standards\PSR2\Tests\Namespaces\UseDeclarationUnitTest::testSniff
[LINE 12] Expected 0 error(s) in UseDeclarationUnitTest.1.inc but found 1 error(s). The error(s) found were:
 -> There must be one blank line after the last USE statement; 0 found; (PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse)
[LINE 2] Expected 1 error(s) in UseDeclarationUnitTest.10.inc but found 0 error(s).
[LINE 10] Expected 2 error(s) in UseDeclarationUnitTest.2.inc but found 1 error(s). The error(s) found were:
 -> USE declarations must go after the first namespace declaration (PSR2.Namespaces.UseDeclaration.UseAfterNamespace)
Fixed version of UseDeclarationUnitTest.2.inc does not match expected version in UseDeclarationUnitTest.2.inc.fixed; the diff is
--- src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed
+++ PHP_CodeSniffer
@@ -10,6 +10,7 @@

 use ArrayObject;

+
 $foo = 'bar';

 ?>
Fixed version of UseDeclarationUnitTest.3.inc does not match expected version in UseDeclarationUnitTest.3.inc.fixed; the diff is
--- src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.3.inc.fixed
+++ PHP_CodeSniffer
@@ -3,8 +3,9 @@

 use someNS\A;
 use someNS\B;
+class

-class Bug
+Bug
 {
     public function __construct()
     {
[LINE 2] Expected 0 error(s) in UseDeclarationUnitTest.4.inc but found 1 error(s). The error(s) found were:
 -> There must be one blank line after the last USE statement; 0 found; (PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse)
[LINE 19] Expected 1 error(s) in UseDeclarationUnitTest.5.inc but found 2 error(s). The error(s) found were:
 -> There must be one USE keyword per declaration (PSR2.Namespaces.UseDeclaration.MultipleDeclarations)
 -> There must be one blank line after the last USE statement; 0 found; (PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse)
Fixed version of UseDeclarationUnitTest.5.inc does not match expected version in UseDeclarationUnitTest.5.inc.fixed; the diff is
--- src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.5.inc.fixed
+++ PHP_CodeSniffer
@@ -27,6 +27,7 @@
 use function foo\math\cos;
 use function foo\math\cosh;

+
 class PHP7 {

 }
[LINE 2] Expected 0 error(s) in UseDeclarationUnitTest.8.inc but found 1 error(s). The error(s) found were:
 -> There must be one blank line after the last USE statement; 0 found; (PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse)
[LINE 2] Expected 0 error(s) in UseDeclarationUnitTest.9.inc but found 1 error(s). The error(s) found were:
 -> There must be one blank line after the last USE statement; 0 found; (PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse)

/vagrant/www/vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php:205
/vagrant/www/vendor/squizlabs/php_codesniffer/tests/TestSuite.php:28

FAILURES!
Tests: 294, Assertions: 302, Failures: 1, Skipped: 3.

This is the diff I tried:

diff --git a/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Namespaces/UseDecindex 4448a08..390629b 100644
--- a/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php
+++ b/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php
@@ -138,13 +138,16 @@ class UseDeclarationSniff implements Sniff
         }

         $end         = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
-        $nextComment = $phpcsFile->findNext(T_COMMENT, ($end + 1));
+        $nextComment = $end;

-        // The end of the line is allowed to be a comment as well as a semi colon.
-        if ($nextComment !== false && $tokens[$nextComment]['line'] === $tokens[$end]['line']) {
+        do {
+            $nextComment = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextComment + 1));
+        } while ($nextComment !== false && $tokens[$nextComment]['line'] === $tokens[$end]['line']);
+
+        if ($nextComment !== false) {
             $end = $nextComment;
         }
-
+
         if ($end === false) {
             return;
         }

@dhensby
Copy link
Contributor Author

dhensby commented Feb 15, 2018

Gah, I see my problem but don't have time to fix right now. We need the $nextComment from the previous iteration

@dhensby
Copy link
Contributor Author

dhensby commented Feb 15, 2018

OK - I've pushed in another commit that uses Tokens::$emptyTokens and passes the tests. Maintainers have push rights to the branch so can reset that commit out if it's decided it's not needed.

@iammattcoleman
Copy link
Contributor

@gsherwood @jrfnl This should probably be included in 3.2.3.

@dhensby
Copy link
Contributor Author

dhensby commented Feb 15, 2018

@gsherwood @jrfnl This should probably be included in 3.2.3.

Yep, unless you want 3.2.3 to be broken :)

@iammattcoleman
Copy link
Contributor

iammattcoleman commented Feb 15, 2018

@gsherwood I'm helping with the update to PHP 7.2 for Ubuntu 18.04. As of now, PHP_CodeSniffer 3.2.2 is slated to be included. I've let them know that there are some important fixes coming soon, but I felt you should know that they're currently packaging 3.2.2 along with its known issues.

@jrfnl
Copy link
Contributor

jrfnl commented Feb 16, 2018

As per @iammattcoleman @dhensby request, I've had a look at this PR. I believe the logic can be simplified slightly by just checking for either next statement or the start of the next line, whichever comes first and not focusing completely on "next comment".

I've just send in a PR to @dhensby's repo with an optional suggestion for amending this PR.
See dhensby#1

@jrfnl
Copy link
Contributor

jrfnl commented Feb 17, 2018

Since the original downstream PR to amend this PR, @iammattcoleman and me have identified some additional issues and the downstream PR has been updated to allow for problems in the fixer when encountering various combinations of trailing comments after use statements.
So my downstream PR is no longer optional.

@dhensby
Copy link
Contributor Author

dhensby commented Feb 18, 2018

I've merged the downstream PR - all looks good - thanks for the thorough investigation

@jrfnl
Copy link
Contributor

jrfnl commented Feb 18, 2018

@dhensby Thanks for working with me to get this sorted.

@dhensby
Copy link
Contributor Author

dhensby commented Feb 18, 2018

@jrfnl no problem! Thanks for the help :)

@gsherwood gsherwood added this to the 3.2.3 milestone Feb 18, 2018
@gsherwood gsherwood merged commit 5d58565 into squizlabs:master Feb 18, 2018
gsherwood added a commit that referenced this pull request Feb 18, 2018
@gsherwood
Copy link
Member

@dhensby Thanks for finding and fixing this before release. Very much appreciated. Thanks to those who reviewed as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants