forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PSR12.Files.ImportStatement to enforce the formatting of import…
… statements within a file (ref squizlabs#750)
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Verifies that import statements are defined correctly. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Files; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
class ImportStatementSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_USE]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in the | ||
* stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
// Make sure this is not a closure USE group. | ||
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); | ||
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) { | ||
return; | ||
} | ||
|
||
if ($tokens[$next]['code'] === T_STRING | ||
&& (strtolower($tokens[$next]['content']) === 'function' | ||
|| strtolower($tokens[$next]['content']) === 'const') | ||
) { | ||
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); | ||
} | ||
|
||
if ($tokens[$next]['code'] !== T_NS_SEPARATOR) { | ||
return; | ||
} | ||
|
||
$error = 'Import statements must not begin with a leading backslash'; | ||
$phpcsFile->addError($error, $next, 'LeadingSlash'); | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
19 changes: 19 additions & 0 deletions
19
src/Standards/PSR12/Tests/Files/ImportStatementUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
use \Vendor\Package\{ClassA as A, ClassB, ClassC as C}; | ||
use Vendor\Package\SomeNamespace\ClassD as D; | ||
use /*comment*/ \Vendor\Package\AnotherNamespace\ClassE as E; | ||
|
||
use function Vendor\Package\{functionA, functionB, functionC}; | ||
use FUNCTION \Another\Vendor\functionD; | ||
|
||
use CONST Vendor\Package\{CONSTANT_A, CONSTANT_B, CONSTANT_C}; | ||
use const Another\Vendor\CONSTANT_D; | ||
|
||
class ClassName3 | ||
{ | ||
use \FirstTrait; | ||
use SecondTrait; | ||
use ThirdTrait; | ||
} | ||
|
||
$foo = function() use($bar) {}; |
53 changes: 53 additions & 0 deletions
53
src/Standards/PSR12/Tests/Files/ImportStatementUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* Unit test class for the ImportStatement sniff. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Tests\Files; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class ImportStatementUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return [ | ||
2 => 1, | ||
4 => 1, | ||
7 => 1, | ||
14 => 1, | ||
]; | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters