Skip to content

Commit 311bfe7

Browse files
committed
update to phpcs 3.x
1 parent f62303e commit 311bfe7

14 files changed

+229
-164
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ php:
1111
before_script:
1212
- phpenv config-rm xdebug.ini
1313
- composer install
14-
- mkdir -p vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/MO4
15-
- cp ruleset.xml vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/MO4/
16-
- cp -R Sniffs vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/MO4/
17-
- cp -R Tests vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/MO4/
18-
- (cd vendor/squizlabs/php_codesniffer/CodeSniffer/Standards ; git clone https://github.com/xalopp/symfony-coding-standard.git Symfony)
14+
- mkdir -p vendor/squizlabs/php_codesniffer/src/Standards/MO4
15+
- cp ruleset.xml vendor/squizlabs/php_codesniffer/src/Standards/MO4/
16+
- cp -R Sniffs vendor/squizlabs/php_codesniffer/src/Standards/MO4/
17+
- cp -R Tests vendor/squizlabs/php_codesniffer/src/Standards/MO4/
1918

2019
script:
2120
- (cd vendor/squizlabs/php_codesniffer ; ../../bin/phpunit --filter MO4)
22-
- ./vendor/squizlabs/php_codesniffer/scripts/phpcs ./vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/MO4/Sniffs --standard=PHPCS --report=summary -np
21+
- (mkdir -p vendor/squizlabs/php_codesniffer/src/Standards/PHPCS ; cp vendor/squizlabs/php_codesniffer/phpcs.xml.dist vendor/squizlabs/php_codesniffer/src/Standards/PHPCS/ruleset.xml)
22+
- ./vendor/squizlabs/php_codesniffer/bin/phpcs ./vendor/squizlabs/php_codesniffer/src/Standards/MO4/Sniffs --standard=PHPCS --report=summary -np

Sniffs/Commenting/PropertyCommentSniff.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
* @link https://github.com/Mayflower/mo4-coding-standard
1414
*/
1515

16+
namespace MO4\Sniffs\Commenting;
17+
18+
use PHP_CodeSniffer\Files\File;
19+
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
20+
1621
/**
1722
* Alphabetical Use Statements sniff.
1823
*
@@ -25,8 +30,7 @@
2530
* @license http://spdx.org/licenses/MIT MIT License
2631
* @link https://github.com/Mayflower/mo4-coding-standard
2732
*/
28-
class MO4_Sniffs_Commenting_PropertyCommentSniff
29-
extends PHP_CodeSniffer_Standards_AbstractScopeSniff
33+
class PropertyCommentSniff extends AbstractScopeSniff
3034
{
3135
/**
3236
* A list of tokenizers this sniff supports.
@@ -40,20 +44,20 @@ class MO4_Sniffs_Commenting_PropertyCommentSniff
4044
*
4145
* @var array
4246
*/
43-
private $_tokenTypes = array(
44-
T_VARIABLE,
45-
T_CONST,
46-
);
47+
private $myTokenTypes = array(
48+
T_VARIABLE,
49+
T_CONST,
50+
);
4751

4852

4953
/**
5054
* Construct PropertyCommentSniff
5155
*/
52-
function __construct()
56+
public function __construct()
5357
{
5458
$scopes = array(T_CLASS);
5559

56-
parent::__construct($scopes, $this->_tokenTypes, true);
60+
parent::__construct($scopes, $this->myTokenTypes, true);
5761

5862
}//end __construct()
5963

@@ -62,17 +66,17 @@ function __construct()
6266
* Processes a token that is found within the scope that this test is
6367
* listening to.
6468
*
65-
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
66-
* @param int $stackPtr The position in the stack where this
67-
* token was found.
68-
* @param int $currScope The position in the tokens array that
69-
* opened the scope that this test is
70-
* listening for.
69+
* @param File $phpcsFile The file where this token was found.
70+
* @param int $stackPtr The position in the stack where this
71+
* token was found.
72+
* @param int $currScope The position in the tokens array that
73+
* opened the scope that this test is
74+
* listening for.
7175
*
7276
* @return void
7377
*/
7478
protected function processTokenWithinScope(
75-
PHP_CodeSniffer_File $phpcsFile,
79+
File $phpcsFile,
7680
$stackPtr,
7781
$currScope
7882
) {
@@ -143,7 +147,7 @@ protected function processTokenWithinScope(
143147
// and has a variable preceding it in the same line.
144148
// If yes, it doesn't count.
145149
$firstTokenOnLine = $phpcsFile->findFirstOnLine(
146-
$this->_tokenTypes,
150+
$this->myTokenTypes,
147151
$commentEnd
148152
);
149153
if ($tokens[$commentStart]['line'] === $tokens[$commentEnd]['line']
@@ -212,7 +216,7 @@ protected function processTokenWithinScope(
212216
// a variable definition on the same line.
213217
// If yes, it doesn't count.
214218
$firstOnLine = $phpcsFile->findFirstOnLine(
215-
$this->_tokenTypes,
219+
$this->myTokenTypes,
216220
$commentEnd
217221
);
218222

@@ -234,4 +238,19 @@ protected function processTokenWithinScope(
234238
}//end processTokenWithinScope()
235239

236240

241+
/**
242+
* Process tokens outside scope.
243+
*
244+
* @param File $phpcsFile The file where this token was found.
245+
* @param int $stackPtr The position in the stack where this
246+
* token was found.
247+
*
248+
* @return void
249+
*/
250+
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
251+
{
252+
253+
}//end processTokenOutsideScope()
254+
255+
237256
}//end class

Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,73 +27,79 @@
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

Comments
 (0)