Permalink
Browse files

Fixed bug #19772 : PSR2_Sniffs_Namespaces_UseDeclarationSniff does no…

…t properly recognize last use
  • Loading branch information...
1 parent e439a1e commit 9f191df0d0dd536e464b30b6045ac304d56e9e2f @gsherwood gsherwood committed Jan 7, 2013
@@ -52,18 +52,11 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
- $tokens = $phpcsFile->getTokens();
-
- // Ignore USE keywords inside closures.
- $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
- if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
+ if ($this->_shouldIgnoreUse($phpcsFile, $stackPtr) === true) {
return;
}
- // Ignore USE keywords for traits.
- if ($phpcsFile->hasCondition($stackPtr, T_CLASS) === true) {
- return;
- }
+ $tokens = $phpcsFile->getTokens();
// Only one USE declaration allowed per statement.
$next = $phpcsFile->findNext(array(T_COMMA, T_SEMICOLON), ($stackPtr + 1));
@@ -84,6 +77,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// Only interested in the last USE statement from here onwards.
$nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1));
+ while ($this->_shouldIgnoreUse($phpcsFile, $nextUse) === true) {
+ $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1));
+ if ($nextUse === false) {
+ break;
+ }
+ }
+
if ($nextUse !== false) {
return;
}
@@ -104,6 +104,35 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}//end process()
+ /**
+ * Check if this use statement is part of the namespace block.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token in
+ * the stack passed in $tokens.
+ *
+ * @return void
+ */
+ private function _shouldIgnoreUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+
+ // Ignore USE keywords inside closures.
+ $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
+ if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
+ return true;
+ }
+
+ // Ignore USE keywords for traits.
+ if ($phpcsFile->hasCondition($stackPtr, T_CLASS) === true) {
+ return true;
+ }
+
+ return false;
+
+ }//end _shouldIgnoreUse()
+
+
}//end class
@@ -47,6 +47,11 @@ public function getErrorList($testFile='')
10 => 2,
);
break;
+ case 'UseDeclarationUnitTest.3.inc':
+ return array(
+ 5 => 1,
+ );
+ break;
default:
return array();
break;
View
@@ -35,6 +35,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #19759 : Squiz.PHP.NonExecutableCode fails for return function()...
- Fixed bug #19763 : Use statements for traits not recognised correctly for PSR2 code style
- Fixed bug #19764 : Instead of for traits throws uppercase constant name errors
+ - Fixed bug #19772 : PSR2_Sniffs_Namespaces_UseDeclarationSniff does not properly recognize last use
</notes>
<contents>
<dir name="/">

0 comments on commit 9f191df

Please sign in to comment.