-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
SpaceAfterNotSniff.php
143 lines (118 loc) · 3.85 KB
/
SpaceAfterNotSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Ensures there is a single space after a NOT operator.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class SpaceAfterNotSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [
'PHP',
'JS',
];
/**
* The number of spaces desired after the NOT operator.
*
* @var integer
*/
public $spacing = 1;
/**
* Allow newlines instead of spaces.
*
* @var boolean
*/
public $ignoreNewlines = false;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [T_BOOLEAN_NOT];
}//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();
$this->spacing = (int) $this->spacing;
$pluralizeSpace = 's';
if ($this->spacing === 1) {
$pluralizeSpace = '';
}
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextNonEmpty === false) {
return;
}
if ($this->ignoreNewlines === true
&& $tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']
) {
return;
}
if ($this->spacing === 0 && $nextNonEmpty === ($stackPtr + 1)) {
return;
}
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($nextNonEmpty !== $nextNonWhitespace) {
$error = 'Expected %s space%s after NOT operator; comment found';
$data = [
$this->spacing,
$pluralizeSpace,
];
$phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);
return;
}
$found = 0;
if ($tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) {
$found = 'newline';
} else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
$found = $tokens[($stackPtr + 1)]['length'];
}
if ($found === $this->spacing) {
return;
}
$error = 'Expected %s space%s after NOT operator; %s found';
$data = [
$this->spacing,
$pluralizeSpace,
$found,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$padding = str_repeat(' ', $this->spacing);
if ($found === 0) {
$phpcsFile->fixer->addContent($stackPtr, $padding);
} else {
$phpcsFile->fixer->beginChangeset();
$start = ($stackPtr + 1);
if ($this->spacing > 0) {
$phpcsFile->fixer->replaceToken($start, $padding);
++$start;
}
for ($i = $start; $i < $nextNonWhitespace; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
}//end process()
}//end class