Skip to content

Commit b10354a

Browse files
committed
Added PSR12.Files.ImportStatement to enforce the formatting of import statements within a file (ref #750)
1 parent 988c945 commit b10354a

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

package.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
6969
-- Enforce the use of a strict types declaration in PHP files
7070
- Added PSR12.Files.DeclareStatement sniff
7171
-- Enforces the formatting of declare statements within a file
72+
- Added PSR12.Files.ImportStatement sniff
73+
-- Enforces the formatting of import statements within a file
7274
- Added PSR12.Functions.ReturnTypeDeclaration sniff
7375
-- Enforces the formatting of return type declarations in functions and closures
7476
- Added PSR12.Properties.ConstantVisibility sniff
@@ -1109,6 +1111,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
11091111
</dir>
11101112
<dir name="Files">
11111113
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementSniff.php" role="php" />
1114+
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementSniff.php" role="php" />
11121115
</dir>
11131116
<dir name="Functions">
11141117
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationSniff.php" role="php" />
@@ -1140,6 +1143,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
11401143
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementUnitTest.inc" role="test" />
11411144
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementUnitTest.inc.fixed" role="test" />
11421145
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementUnitTest.php" role="test" />
1146+
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
1147+
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.php" role="test" />
11431148
</dir>
11441149
<dir name="Functions">
11451150
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationUnitTest.inc" role="test" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Verifies that import statements are defined correctly.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Files;
11+
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Util\Tokens;
15+
16+
class ImportStatementSniff implements Sniff
17+
{
18+
19+
20+
/**
21+
* Returns an array of tokens this test wants to listen for.
22+
*
23+
* @return array
24+
*/
25+
public function register()
26+
{
27+
return [T_USE];
28+
29+
}//end register()
30+
31+
32+
/**
33+
* Processes this test, when one of its tokens is encountered.
34+
*
35+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token in the
37+
* stack passed in $tokens.
38+
*
39+
* @return void
40+
*/
41+
public function process(File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
45+
// Make sure this is not a closure USE group.
46+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
47+
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
48+
return;
49+
}
50+
51+
if ($tokens[$next]['code'] === T_STRING
52+
&& (strtolower($tokens[$next]['content']) === 'function'
53+
|| strtolower($tokens[$next]['content']) === 'const')
54+
) {
55+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
56+
}
57+
58+
if ($tokens[$next]['code'] !== T_NS_SEPARATOR) {
59+
return;
60+
}
61+
62+
$error = 'Import statements must not begin with a leading backslash';
63+
$phpcsFile->addError($error, $next, 'LeadingSlash');
64+
65+
}//end process()
66+
67+
68+
}//end class
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
use \Vendor\Package\{ClassA as A, ClassB, ClassC as C};
3+
use Vendor\Package\SomeNamespace\ClassD as D;
4+
use /*comment*/ \Vendor\Package\AnotherNamespace\ClassE as E;
5+
6+
use function Vendor\Package\{functionA, functionB, functionC};
7+
use FUNCTION \Another\Vendor\functionD;
8+
9+
use CONST Vendor\Package\{CONSTANT_A, CONSTANT_B, CONSTANT_C};
10+
use const Another\Vendor\CONSTANT_D;
11+
12+
class ClassName3
13+
{
14+
use \FirstTrait;
15+
use SecondTrait;
16+
use ThirdTrait;
17+
}
18+
19+
$foo = function() use($bar) {};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Unit test class for the ImportStatement sniff.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\PSR12\Tests\Files;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
class ImportStatementUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
18+
/**
19+
* Returns the lines where errors should occur.
20+
*
21+
* The key of the array should represent the line number and the value
22+
* should represent the number of errors that should occur on that line.
23+
*
24+
* @return array<int, int>
25+
*/
26+
public function getErrorList()
27+
{
28+
return [
29+
2 => 1,
30+
4 => 1,
31+
7 => 1,
32+
14 => 1,
33+
];
34+
35+
}//end getErrorList()
36+
37+
38+
/**
39+
* Returns the lines where warnings should occur.
40+
*
41+
* The key of the array should represent the line number and the value
42+
* should represent the number of warnings that should occur on that line.
43+
*
44+
* @return array<int, int>
45+
*/
46+
public function getWarningList()
47+
{
48+
return [];
49+
50+
}//end getWarningList()
51+
52+
53+
}//end class

src/Standards/PSR12/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<!-- When the opening php tag is on the first line of the file, it MUST be on its own line with no other statements unless it is a file containing markup outside of PHP opening and closing tags. -->
102102

103103
<!-- Import statements MUST never begin with a leading backslash as they must always be fully qualified. -->
104+
<!-- checked by PSR12.Files.ImportStatement -->
104105

105106
<!-- Compound namespaces with a depth of more than two MUST NOT be used. -->
106107
<!-- checked by PSR12.Namespaces.CompoundNamespaceDepth -->

0 commit comments

Comments
 (0)