Skip to content

Commit de1cd17

Browse files
authored
Merge pull request #10 from ewbarnard/master
Upgrade to support Sniffer 3.x with tests for all sniffs
2 parents 2814338 + d61fc80 commit de1cd17

File tree

271 files changed

+8615
-2063
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

271 files changed

+8615
-2063
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ script:
2424
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --coverage-clover build/logs/clover.xml; fi"
2525
- sh -c "if [ '$COVERALLS' = '1' ]; then vendor/bin/coveralls -c .coveralls.yml -v; fi"
2626

27-
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=PSR2R/ruleset.xml --ignore=vendor/,tests/files/ . ; fi"
27+
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=PSR2R/ruleset.xml --ignore=vendor/,tests/files/,*.inc,*.fixed . ; fi"
2828

2929
notifications:
3030
email: false

PSR2R/Sniffs/Classes/BraceOnSameLineSniff.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,21 @@
22

33
namespace PSR2R\Sniffs\Classes;
44

5-
use PHP_CodeSniffer_File;
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
67

78
/**
89
* Makes sure opening braces are on the same line for class, interface and trait.
910
*
1011
* @author Mark Scherer
1112
* @license MIT
1213
*/
13-
class BraceOnSameLineSniff implements \PHP_CodeSniffer_Sniff {
14+
class BraceOnSameLineSniff implements Sniff {
1415

1516
/**
1617
* @inheritDoc
1718
*/
18-
public function register() {
19-
return [
20-
T_CLASS,
21-
T_INTERFACE,
22-
T_TRAIT,
23-
T_FUNCTION
24-
];
25-
}
26-
27-
/**
28-
* @inheritDoc
29-
*/
30-
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
19+
public function process(File $phpcsFile, $stackPtr) {
3120
$tokens = $phpcsFile->getTokens();
3221
$errorData = [strtolower($tokens[$stackPtr]['content'])];
3322

@@ -36,7 +25,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
3625
}
3726

3827
$curlyBrace = $tokens[$stackPtr]['scope_opener'];
39-
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($curlyBrace - 1), $stackPtr, true);
28+
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $curlyBrace - 1, $stackPtr, true);
4029
$classLine = $tokens[$lastContent]['line'];
4130
$braceLine = $tokens[$curlyBrace]['line'];
4231
if ($braceLine !== $classLine) {
@@ -58,4 +47,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
5847
}
5948
}
6049

50+
/**
51+
* @inheritDoc
52+
*/
53+
public function register() {
54+
return [
55+
T_CLASS,
56+
T_INTERFACE,
57+
T_TRAIT,
58+
T_FUNCTION,
59+
];
60+
}
61+
6162
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<?php
2+
23
namespace PSR2R\Sniffs\Classes;
34

4-
use PHP_CodeSniffer_File;
5-
use PHP_CodeSniffer_Tokens;
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Util\Tokens;
67
use PSR2R\Tools\AbstractSniff;
78

89
class ClassCreateInstanceSniff extends AbstractSniff {
910

1011
/**
1112
* @inheritDoc
1213
*/
13-
public function register() {
14-
return [T_NEW];
15-
}
16-
17-
/**
18-
* @inheritDoc
19-
*/
20-
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
14+
public function process(File $phpcsFile, $stackPtr) {
2115
$tokens = $phpcsFile->getTokens();
2216

2317
$nextParenthesisIndex = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
@@ -28,7 +22,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
2822
}
2923

3024
$error = 'Calling class constructors must always include parentheses';
31-
$constructorIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
25+
$constructorIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
3226

3327
// We can only invoke the fixer if we know this is a static constructor function call.
3428
if ($tokens[$constructorIndex]['code'] !== T_STRING && $tokens[$constructorIndex]['code'] !== T_NS_SEPARATOR) {
@@ -40,15 +34,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
4034
$nextConstructorPart = $constructorIndex;
4135
while (true) {
4236
$nextConstructorPart = $phpcsFile->findNext(
43-
PHP_CodeSniffer_Tokens::$emptyTokens,
44-
($nextConstructorPart + 1),
37+
Tokens::$emptyTokens,
38+
$nextConstructorPart + 1,
4539
null,
4640
true,
4741
null,
4842
true
4943
);
5044
if ($nextConstructorPart === false
51-
|| ($tokens[$nextConstructorPart]['code'] !== T_STRING && $tokens[$nextConstructorPart]['code'] !== T_NS_SEPARATOR)
45+
|| ($tokens[$nextConstructorPart]['code'] !== T_STRING &&
46+
$tokens[$nextConstructorPart]['code'] !== T_NS_SEPARATOR)
5247
) {
5348
break;
5449
}
@@ -62,4 +57,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
6257
}
6358
}
6459

60+
/**
61+
* @inheritDoc
62+
*/
63+
public function register() {
64+
return [T_NEW];
65+
}
66+
6567
}

PSR2R/Sniffs/Classes/ClassFileNameSniff.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
1313
* @link http://pear.php.net/package/PHP_CodeSniffer
1414
*/
15+
1516
namespace PSR2R\Sniffs\Classes;
1617

17-
use PHP_CodeSniffer_File;
18+
use PHP_CodeSniffer\Files\File;
1819
use PSR2R\Tools\AbstractSniff;
1920

2021
/**
@@ -23,29 +24,19 @@
2324
* Tests that the file name and the name of the class contained within the file
2425
* match.
2526
*
26-
* @author Greg Sherwood <gsherwood@squiz.net>
27-
* @author Marc McIntyre <mmcintyre@squiz.net>
27+
* @author Greg Sherwood <gsherwood@squiz.net>
28+
* @author Marc McIntyre <mmcintyre@squiz.net>
2829
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
29-
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
30-
* @version Release: @package_version@
31-
* @link http://pear.php.net/package/PHP_CodeSniffer
30+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31+
* @version Release: @package_version@
32+
* @link http://pear.php.net/package/PHP_CodeSniffer
3233
*/
3334
class ClassFileNameSniff extends AbstractSniff {
3435

3536
/**
3637
* @inheritDoc
3738
*/
38-
public function register() {
39-
return [
40-
T_CLASS,
41-
T_INTERFACE,
42-
];
43-
}
44-
45-
/**
46-
* @inheritDoc
47-
*/
48-
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
39+
public function process(File $phpcsFile, $stackPtr) {
4940
$fullPath = basename($phpcsFile->getFilename());
5041
$fileName = substr($fullPath, 0, strrpos($fullPath, '.'));
5142
if ($fileName === '') {
@@ -74,4 +65,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
7465
}
7566
}
7667

68+
/**
69+
* @inheritDoc
70+
*/
71+
public function register() {
72+
return [
73+
T_CLASS,
74+
T_INTERFACE,
75+
];
76+
}
77+
7778
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
23
namespace PSR2R\Sniffs\Classes;
34

4-
use PHP_CodeSniffer_File;
5+
use PHP_CodeSniffer\Files\File;
56
use PSR2R\Tools\AbstractSniff;
67

78
/**
@@ -12,17 +13,10 @@ class InterfaceNameSniff extends AbstractSniff {
1213
/**
1314
* @inheritDoc
1415
*/
15-
public function register() {
16-
return [T_INTERFACE];
17-
}
18-
19-
/**
20-
* @inheritDoc
21-
*/
22-
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
16+
public function process(File $phpcsFile, $stackPtr) {
2317
$tokens = $phpcsFile->getTokens();
2418

25-
$nameIndex = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
19+
$nameIndex = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
2620
$name = $tokens[$nameIndex]['content'];
2721
if (substr($name, -9) === 'Interface') {
2822
return;
@@ -32,4 +26,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
3226
$phpcsFile->addWarning($warn, $nameIndex, 'MissingSuffix');
3327
}
3428

29+
/**
30+
* @inheritDoc
31+
*/
32+
public function register() {
33+
return [T_INTERFACE];
34+
}
35+
3536
}

PSR2R/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,34 @@
22

33
namespace PSR2R\Sniffs\Classes;
44

5-
use PHP_CodeSniffer_Exception;
6-
use PHP_CodeSniffer_File;
7-
use PHP_CodeSniffer_Standards_AbstractVariableSniff;
8-
use PHP_CodeSniffer_Tokens;
9-
10-
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
11-
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
12-
}
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
7+
use PHP_CodeSniffer\Util\Tokens;
138

149
/**
1510
* Verifies that properties are declared correctly.
1611
*
17-
* @author Greg Sherwood <gsherwood@squiz.net>
12+
* @author Greg Sherwood <gsherwood@squiz.net>
1813
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
19-
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
14+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
2015
*
21-
* @version Release: @package_version@
16+
* @version Release: @package_version@
2217
*
23-
* @link http://pear.php.net/package/PHP_CodeSniffer
18+
* @link http://pear.php.net/package/PHP_CodeSniffer
2419
*/
25-
class PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff {
20+
class PropertyDeclarationSniff extends AbstractVariableSniff {
2621
/**
2722
* @inheritDoc
2823
*/
29-
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
24+
protected function processMemberVar(File $phpcsFile, $stackPtr) {
3025
$tokens = $phpcsFile->getTokens();
3126

3227
// Detect multiple properties defined at the same time. Throw an error
3328
// for this, but also only process the first property in the list so we don't
3429
// repeat errors.
35-
$find = PHP_CodeSniffer_Tokens::$scopeModifiers;
30+
$find = Tokens::$scopeModifiers;
3631
$find = array_merge($find, [T_VARIABLE, T_VAR, T_SEMICOLON]);
37-
$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
32+
$prev = $phpcsFile->findPrevious($find, $stackPtr - 1);
3833
if ($tokens[$prev]['code'] === T_VARIABLE) {
3934
return;
4035
}
@@ -44,13 +39,13 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4439
$phpcsFile->addError($error, $stackPtr, 'VarUsed');
4540
}
4641

47-
$next = $phpcsFile->findNext([T_VARIABLE, T_SEMICOLON], ($stackPtr + 1));
42+
$next = $phpcsFile->findNext([T_VARIABLE, T_SEMICOLON], $stackPtr + 1);
4843
if ($tokens[$next]['code'] === T_VARIABLE) {
4944
$error = 'There must not be more than one property declared per statement';
5045
$phpcsFile->addError($error, $stackPtr, 'Multiple');
5146
}
5247

53-
$modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
48+
$modifier = $phpcsFile->findPrevious(Tokens::$scopeModifiers, $stackPtr);
5449
if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
5550
$error = 'Visibility must be declared on property "%s"';
5651
$data = [$tokens[$stackPtr]['content']];
@@ -61,7 +56,7 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6156
/**
6257
* @inheritDoc
6358
*/
64-
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
59+
protected function processVariable(File $phpcsFile, $stackPtr) {
6560
/*
6661
We don't care about normal variables.
6762
*/
@@ -70,7 +65,7 @@ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
7065
/**
7166
* @inheritDoc
7267
*/
73-
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
68+
protected function processVariableInString(File $phpcsFile, $stackPtr) {
7469
/*
7570
We don't care about normal variables.
7671
*/

0 commit comments

Comments
 (0)