From 8723383994ba76de4e4558d67b9a3cc7cc9f202d Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Thu, 15 Feb 2018 14:22:14 +0000 Subject: [PATCH] FIX #1891 Where inline comment fails UseDeclaration sniff --- .../PSR2/Sniffs/Namespaces/UseDeclarationSniff.php | 7 +++++++ .../PSR2/Tests/Namespaces/UseDeclarationUnitTest.10.inc | 8 ++++++++ .../Tests/Namespaces/UseDeclarationUnitTest.10.inc.fixed | 9 +++++++++ .../PSR2/Tests/Namespaces/UseDeclarationUnitTest.9.inc | 8 ++++++++ .../PSR2/Tests/Namespaces/UseDeclarationUnitTest.php | 4 ++++ 5 files changed, 36 insertions(+) create mode 100644 src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.10.inc create mode 100644 src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.10.inc.fixed create mode 100644 src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.9.inc diff --git a/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php index 6b5ae0a8c6..44aa6c3c77 100644 --- a/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php @@ -138,6 +138,13 @@ public function process(File $phpcsFile, $stackPtr) } $end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); + $nextComment = $phpcsFile->findNext(T_COMMENT, ($end + 1)); + + // 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']) { + $end = $nextComment; + } + if ($end === false) { return; } diff --git a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.10.inc b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.10.inc new file mode 100644 index 0000000000..1e85697674 --- /dev/null +++ b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.10.inc @@ -0,0 +1,8 @@ + 1, 19 => 1, ]; + case 'UseDeclarationUnitTest.10.inc': + return [ + 2 => 1, + ]; default: return []; }//end switch