Skip to content

Commit 3fb5152

Browse files
committed
added Alphabetical Use Statements Sniff
1 parent 49d93e0 commit 3fb5152

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the mo4-coding-standard (phpcs standard)
5+
*
6+
* PHP version 5
7+
*
8+
* @category PHP
9+
* @package PHP_CodeSniffer-MO4
10+
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
11+
* @license http://spdx.org/licenses/MIT MIT License
12+
* @version GIT: master
13+
* @link https://github.com/Mayflower/mo4-coding-standard
14+
*/
15+
16+
/**
17+
* Alphabetical Use Statements sniff.
18+
*
19+
* Use statements must be in alphabetical order, grouped by empty lines
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer-MO4
23+
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
24+
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
25+
* @license http://spdx.org/licenses/MIT MIT License
26+
* @link https://github.com/Mayflower/mo4-coding-standard
27+
*/
28+
class MO4_Sniffs_Formatting_AlphabeticalUseStatementsSniff
29+
extends PSR2_Sniffs_Namespaces_UseDeclarationSniff
30+
{
31+
/**
32+
* last use statement seen in group
33+
*
34+
* @var string
35+
*/
36+
private $_lastUseStatement = '';
37+
38+
/**
39+
* line number of the last seen use statement
40+
*
41+
* @var int
42+
*/
43+
private $_lastLine = -1;
44+
45+
/**
46+
* current file
47+
*
48+
* @var string
49+
*/
50+
private $_currentFile = null;
51+
52+
/**
53+
* Processes this test, when one of its tokens is encountered.
54+
*
55+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
56+
* @param int $stackPtr The position of the current token in
57+
* the stack passed in $tokens.
58+
*
59+
* @return void
60+
*/
61+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
62+
{
63+
parent::process($phpcsFile, $stackPtr);
64+
65+
if ($this->_currentFile !== $phpcsFile->getFilename()) {
66+
$this->_lastLine = -1;
67+
$this->_lastUseStatement = '';
68+
$this->_currentFile = $phpcsFile->getFilename();
69+
}
70+
71+
$tokens = $phpcsFile->getTokens();
72+
$line = $tokens[$stackPtr]['line'];
73+
$start = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
74+
$end = $phpcsFile->findNext([T_AS, T_SEMICOLON, T_COMMA], $stackPtr + 1);
75+
76+
$currentUseStatement = $phpcsFile->getTokensAsString($start, $end - $start);
77+
78+
if (($this->_lastLine + 1) < $line) {
79+
$this->_lastLine = $line;
80+
$this->_lastUseStatement = $currentUseStatement;
81+
82+
return;
83+
}
84+
85+
if ($this->_lastUseStatement !== '') {
86+
if (strcmp($this->_lastUseStatement, $currentUseStatement) > 0) {
87+
$msg = 'USE statements mus be sorted alphabetically';
88+
89+
$phpcsFile->addError($msg, $stackPtr);
90+
}
91+
}
92+
93+
$this->_lastUseStatement = $currentUseStatement;
94+
$this->_lastLine = $line;
95+
}
96+
}
97+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use C;
4+
use B;
5+
use A;
6+
7+
use A\B\C as CC;
8+
use A\B\B as BB;
9+
use A\B\A as AA;
10+
11+
use Z;
12+
use X;
13+
14+
class Foo ()
15+
{
16+
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use A;
4+
use B;
5+
use C;
6+
7+
use A\B\A as AA;
8+
use A\B\B as BB;
9+
use A\B\C as CC;
10+
11+
use Z;
12+
13+
use X;
14+
15+
class Foo ()
16+
{
17+
18+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the mo4-coding-standard (phpcs standard)
5+
*
6+
* PHP version 5
7+
*
8+
* @category PHP
9+
* @package PHP_CodeSniffer-MO4
10+
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
11+
* @license http://spdx.org/licenses/MIT MIT License
12+
* @version GIT: master
13+
* @link https://github.com/Mayflower/mo4-coding-standard
14+
*/
15+
16+
/**
17+
* Unit test class for the AlphabeticalUseStatements sniff.
18+
*
19+
* A sniff unit test checks a .inc file for expected violations of a single
20+
* coding standard. Expected errors and warnings are stored in this class.
21+
*
22+
* @category PHP
23+
* @package PHP_CodeSniffer-MO4
24+
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
25+
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
26+
* @license http://spdx.org/licenses/MIT MIT License
27+
* @link https://github.com/Mayflower/mo4-coding-standard
28+
*/
29+
class MO4_Tests_Formatting_AlphabeticalUseStatementsUnitTest
30+
extends AbstractSniffUnitTest
31+
{
32+
/**
33+
* Returns the lines where errors should occur.
34+
*
35+
* The key of the array should represent the line number and the value
36+
* should represent the number of errors that should occur on that line.
37+
*
38+
* @param string $testFile test file
39+
*
40+
* @return array(int => int)
41+
*/
42+
protected function getErrorList($testFile = '')
43+
{
44+
switch ($testFile) {
45+
case 'AlphabeticalUseStatementsUnitTest.pass.inc':
46+
return array();
47+
case 'AlphabeticalUseStatementsUnitTest.fail.inc':
48+
return array(
49+
4 => 1,
50+
5 => 1,
51+
8 => 1,
52+
9 => 1,
53+
12 => 1,
54+
);
55+
}
56+
57+
return null;
58+
59+
}
60+
61+
/**
62+
* Returns the lines where warnings should occur.
63+
*
64+
* The key of the array should represent the line number and the value
65+
* should represent the number of warnings that should occur on that line.
66+
*
67+
* @return array(int => int)
68+
*/
69+
protected function getWarningList()
70+
{
71+
return array();
72+
}
73+
}
74+

ruleset.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66

77
<!-- Include the whole Symfony PSR-2 standard -->
88
<rule ref="Symfony">
9+
<!-- exclude sniffs that are extended by this standard -->
910
<exclude name="PEAR.Functions.FunctionCallSignature"/>
11+
<exclude name="PSR2.Namespaces.UseDeclaration"/>
12+
</rule>
13+
14+
<!-- Have 8 chars padding maximum and always show as errors -->
15+
<rule ref="Generic.Formatting.MultipleStatementAlignment">
16+
<properties>
17+
<property name="maxPadding" value="8"/>
18+
<property name="ignoreMultiLine" value="true"/>
19+
<property name="error" value="true"/>
20+
</properties>
1021
</rule>
1122
</ruleset>
1223

0 commit comments

Comments
 (0)