2727 * @license http://spdx.org/licenses/MIT MIT License
2828 * @link https://github.com/Mayflower/mo4-coding-standard
2929 */
30- class MO4_Sniffs_Formatting_AlphabeticalUseStatementsSniff
31- extends PSR2_Sniffs_Namespaces_UseDeclarationSniff
30+
31+ namespace MO4 \Sniffs \Formatting ;
32+
33+ use PHP_CodeSniffer \Files \File ;
34+ use PHP_CodeSniffer \Standards \PSR2 \Sniffs \Namespaces \UseDeclarationSniff ;
35+ use PHP_CodeSniffer \Util \Tokens as PHP_CodeSniffer_Tokens ;
36+
37+ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff
3238{
3339 /**
3440 * Last import seen in group
3541 *
3642 * @var string
3743 */
38- private $ _lastImport = '' ;
44+ private $ lastImport = '' ;
3945
4046 /**
4147 * Line number of the last seen use statement
4248 *
43- * @var int
49+ * @var integer
4450 */
45- private $ _lastLine = -1 ;
51+ private $ lastLine = -1 ;
4652
4753 /**
4854 * Current file
4955 *
5056 * @var string
5157 */
52- private $ _currentFile = null ;
58+ private $ currentFile = null ;
5359
5460
5561 /**
5662 * Processes this test, when one of its tokens is encountered.
5763 *
58- * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
59- * @param int $stackPtr The position of the current token in
60- * the stack passed in $tokens.
64+ * @param File $phpcsFile The file being scanned.
65+ * @param int $stackPtr The position of the current token in
66+ * the stack passed in $tokens.
6167 *
6268 * @return void
6369 */
64- public function process (PHP_CodeSniffer_File $ phpcsFile , $ stackPtr )
70+ public function process (File $ phpcsFile , $ stackPtr )
6571 {
6672 parent ::process ($ phpcsFile , $ stackPtr );
6773
68- if ($ this ->_currentFile !== $ phpcsFile ->getFilename ()) {
69- $ this ->_lastLine = -1 ;
70- $ this ->_lastImport = '' ;
71- $ this ->_currentFile = $ phpcsFile ->getFilename ();
74+ if ($ this ->currentFile !== $ phpcsFile ->getFilename ()) {
75+ $ this ->lastLine = -1 ;
76+ $ this ->lastImport = '' ;
77+ $ this ->currentFile = $ phpcsFile ->getFilename ();
7278 }
7379
7480 $ tokens = $ phpcsFile ->getTokens ();
7581 $ line = $ tokens [$ stackPtr ]['line ' ];
7682
7783 // Ignore function () use () {...}.
78- $ isNonImportUse = $ this ->_checkIsNonImportUse ($ phpcsFile , $ stackPtr );
84+ $ isNonImportUse = $ this ->checkIsNonImportUse ($ phpcsFile , $ stackPtr );
7985 if (true === $ isNonImportUse ) {
8086 return ;
8187 }
8288
83- $ currentImportArr = $ this ->_getUseImport ($ phpcsFile , $ stackPtr );
89+ $ currentImportArr = $ this ->getUseImport ($ phpcsFile , $ stackPtr );
8490 $ currentPtr = $ currentImportArr ['startPtr ' ];
8591 $ currentImport = $ currentImportArr ['content ' ];
8692
87- if (($ this ->_lastLine + 1 ) < $ line ) {
88- $ this ->_lastLine = $ line ;
89- $ this ->_lastImport = $ currentImport ;
93+ if (($ this ->lastLine + 1 ) < $ line ) {
94+ $ this ->lastLine = $ line ;
95+ $ this ->lastImport = $ currentImport ;
9096
9197 return ;
9298 }
9399
94100 $ fixable = false ;
95- if ($ this ->_lastImport !== ''
96- && strcmp ($ this ->_lastImport , $ currentImport ) > 0
101+ if ($ this ->lastImport !== ''
102+ && strcmp ($ this ->lastImport , $ currentImport ) > 0
97103 ) {
98104 $ msg = 'USE statements must be sorted alphabetically ' ;
99105 $ code = 'MustBeSortedAlphabetically ' ;
@@ -103,31 +109,31 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
103109 if (true === $ fixable ) {
104110 // Find the correct position in current use block.
105111 $ newDestinationPtr
106- = $ this ->_findNewDestination ($ phpcsFile , $ stackPtr , $ currentImport );
112+ = $ this ->findNewDestination ($ phpcsFile , $ stackPtr , $ currentImport );
107113
108- $ currentUseStr = $ this ->_getUseStatementAsString ($ phpcsFile , $ stackPtr );
114+ $ currentUseStr = $ this ->getUseStatementAsString ($ phpcsFile , $ stackPtr );
109115
110116 $ phpcsFile ->fixer ->beginChangeset ();
111117 $ phpcsFile ->fixer ->addContentBefore ($ newDestinationPtr , $ currentUseStr );
112- $ this ->_fixerClearLine ($ phpcsFile , $ stackPtr );
118+ $ this ->fixerClearLine ($ phpcsFile , $ stackPtr );
113119 $ phpcsFile ->fixer ->endChangeset ();
114120 }//end if
115121
116- $ this ->_lastImport = $ currentImport ;
117- $ this ->_lastLine = $ line ;
122+ $ this ->lastImport = $ currentImport ;
123+ $ this ->lastLine = $ line ;
118124
119125 }//end process()
120126
121127
122128 /**
123129 * Get the import class name for use statement pointed by $stackPtr.
124130 *
125- * @param PHP_CodeSniffer_File $phpcsFile PHP CS File
126- * @param int $stackPtr pointer
131+ * @param File $phpcsFile PHP CS File
132+ * @param int $stackPtr pointer
127133 *
128134 * @return array
129135 */
130- private function _getUseImport ( PHP_CodeSniffer_File $ phpcsFile , $ stackPtr )
136+ private function getUseImport ( File $ phpcsFile , $ stackPtr )
131137 {
132138 $ importTokens = array (
133139 T_NS_SEPARATOR ,
@@ -148,19 +154,19 @@ private function _getUseImport(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
148154 'content ' => $ import ,
149155 );
150156
151- }//end _getUseImport ()
157+ }//end getUseImport ()
152158
153159
154160 /**
155161 * Get the full use statement as string, including trailing white space.
156162 *
157- * @param PHP_CodeSniffer_File $phpcsFile PHP CS File
158- * @param int $stackPtr pointer
163+ * @param File $phpcsFile PHP CS File
164+ * @param int $stackPtr pointer
159165 *
160166 * @return string
161167 */
162- private function _getUseStatementAsString (
163- PHP_CodeSniffer_File $ phpcsFile ,
168+ private function getUseStatementAsString (
169+ File $ phpcsFile ,
164170 $ stackPtr
165171 ) {
166172 $ tokens = $ phpcsFile ->getTokens ();
@@ -175,19 +181,19 @@ private function _getUseStatementAsString(
175181
176182 return $ useStr ;
177183
178- }//end _getUseStatementAsString ()
184+ }//end getUseStatementAsString ()
179185
180186
181187 /**
182188 * Check if "use" token is not used for import.
183189 * E.g. function () use () {...}.
184190 *
185- * @param PHP_CodeSniffer_File $phpcsFile PHP CS File
186- * @param int $stackPtr pointer
191+ * @param File $phpcsFile PHP CS File
192+ * @param int $stackPtr pointer
187193 *
188194 * @return bool
189195 */
190- private function _checkIsNonImportUse ( PHP_CodeSniffer_File $ phpcsFile , $ stackPtr )
196+ private function checkIsNonImportUse ( File $ phpcsFile , $ stackPtr )
191197 {
192198 $ tokens = $ phpcsFile ->getTokens ();
193199
@@ -210,20 +216,20 @@ private function _checkIsNonImportUse(PHP_CodeSniffer_File $phpcsFile, $stackPtr
210216
211217 return false ;
212218
213- }//end _checkIsNonImportUse ()
219+ }//end checkIsNonImportUse ()
214220
215221
216222 /**
217223 * Replace all the token in same line as the element pointed to by $stackPtr
218224 * the by the empty string.
219225 * This will delete the line.
220226 *
221- * @param PHP_CodeSniffer_File $phpcsFile PHP CS file
222- * @param int $stackPtr pointer
227+ * @param File $phpcsFile PHP CS file
228+ * @param int $stackPtr pointer
223229 *
224230 * @return void
225231 */
226- private function _fixerClearLine ( PHP_CodeSniffer_File $ phpcsFile , $ stackPtr )
232+ private function fixerClearLine ( File $ phpcsFile , $ stackPtr )
227233 {
228234 $ tokens = $ phpcsFile ->getTokens ();
229235 $ line = $ tokens [$ stackPtr ]['line ' ];
@@ -236,21 +242,21 @@ private function _fixerClearLine(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
236242 $ phpcsFile ->fixer ->replaceToken ($ i , '' );
237243 }
238244
239- }//end _fixerClearLine ()
245+ }//end fixerClearLine ()
240246
241247
242248 /**
243249 * Find a new destination pointer for the given import string in current
244250 * use block.
245251 *
246- * @param PHP_CodeSniffer_File $phpcsFile PHP CS File
247- * @param int $stackPtr pointer
248- * @param string $import import string requiring new position
252+ * @param File $phpcsFile PHP CS File
253+ * @param int $stackPtr pointer
254+ * @param string $import import string requiring new position
249255 *
250256 * @return int
251257 */
252- private function _findNewDestination (
253- PHP_CodeSniffer_File $ phpcsFile ,
258+ private function findNewDestination (
259+ File $ phpcsFile ,
254260 $ stackPtr ,
255261 $ import
256262 ) {
@@ -272,14 +278,14 @@ private function _findNewDestination(
272278 }
273279
274280 $ prevLine = $ tokens [$ prevPtr ]['line ' ];
275- $ prevImportArr = $ this ->_getUseImport ($ phpcsFile , $ prevPtr );
281+ $ prevImportArr = $ this ->getUseImport ($ phpcsFile , $ prevPtr );
276282 } while ($ prevLine === ($ line - 1 )
277283 && (strcmp ($ prevImportArr ['content ' ], $ import ) > 0 )
278284 );
279285
280286 return $ ptr ;
281287
282- }//end _findNewDestination ()
288+ }//end findNewDestination ()
283289
284290
285291}//end class
0 commit comments