Skip to content

Commit

Permalink
Fixed bug #1876 : PSR2.Namespaces.UseDeclaration not giving error for…
Browse files Browse the repository at this point in the history
… use statements before the namespace declaration
  • Loading branch information
gsherwood committed Mar 19, 2018
1 parent 78ddbae commit c75c6ae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Squiz.PHP.CommentedOutCode now ignores a greater number of short comments so they are not flagged as commented out code
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #1863 : File::findEndOfStatement() not working when passed a scope opener
- Fixed bug #1876 : PSR2.Namespaces.UseDeclaration not giving error for use statements before the namespace declaration
-- Adds a new PSR2.Namespaces.UseDeclaration.UseBeforeNamespace error message
- Fixed bug #1915 : JS tokenizer fails to tokenize regular expression proceeded by boolean not operator
- Fixed bug #1922 : Equal sign alignment check broken when list syntax used before assignment operator
- Fixed bug #1925 : Generic.Formatting.MultipleStatementAlignment skipping assignments within closures
Expand Down Expand Up @@ -1039,6 +1041,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="UseDeclarationUnitTest.13.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="UseDeclarationUnitTest.14.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="UseDeclarationUnitTest.14.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="UseDeclarationUnitTest.15.inc" role="test" />
</dir>
</dir>
<file baseinstalldir="PHP/CodeSniffer" name="ruleset.xml" role="php" />
Expand Down
6 changes: 6 additions & 0 deletions src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public function process(File $phpcsFile, $stackPtr)
$error = 'USE declarations must go after the first namespace declaration';
$phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace');
}
} else {
$next = $phpcsFile->findNext(T_NAMESPACE, ($stackPtr + 1));

This comment has been minimized.

Copy link
@jrfnl

jrfnl Mar 19, 2018

Contributor

Just a thought, but I don't think this check now allows for scoped namespaces with multiple namespaces in one file, which would result in false positives.

<?php

namespace ABC {
    use something;

    // more code
}

namespace DEF {
    use something;

    // more code
}

This comment has been minimized.

Copy link
@gsherwood

gsherwood Mar 19, 2018

Author Member

PSR2 has always thrown an error for the second use in this case (USE declarations must go after the first namespace declaration). The new code doesn't actually throw an error for this syntax because is only looks for use statements sitting before any namespace declaration.

This comment has been minimized.

Copy link
@jrfnl

jrfnl Mar 19, 2018

Contributor

But won't it now also start throwing an error on the first use ?

This comment has been minimized.

Copy link
@gsherwood

gsherwood Mar 19, 2018

Author Member

No, because it wont get into that ELSE block. It only gets there if it can't find a previous namespace token.

This comment has been minimized.

Copy link
@jrfnl

jrfnl Mar 19, 2018

Contributor

Ah, sorry about that, I'd just looked at the changeset.

if ($next !== false) {
$error = 'USE declarations must go after the namespace declaration';
$phpcsFile->addError($error, $stackPtr, 'UseBeforeNamespace');
}
}

// Only interested in the last USE statement from here onwards.
Expand Down
10 changes: 10 additions & 0 deletions src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.15.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use BarClass as Bar;
use My\Full\Classname as Another;
use ArrayObject;

namespace AnotherProject;

$foo = 'bar';

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public function getErrorList($testFile='')
case 'UseDeclarationUnitTest.13.inc':
case 'UseDeclarationUnitTest.14.inc':
return [2 => 1];
case 'UseDeclarationUnitTest.15.inc':
return [
3 => 1,
4 => 1,
5 => 1,
];
default:
return [];
}//end switch
Expand Down

0 comments on commit c75c6ae

Please sign in to comment.