-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathOperatorSpacingSniff.php
More file actions
185 lines (159 loc) · 7.14 KB
/
OperatorSpacingSniff.php
File metadata and controls
185 lines (159 loc) · 7.14 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Verifies that operators have valid spacing surrounding them.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2023 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\OperatorSpacingSniff as SquizOperatorSpacingSniff;
use PHP_CodeSniffer\Util\Tokens;
class OperatorSpacingSniff extends SquizOperatorSpacingSniff
{
/**
* The PER version to be compatible with. For backwards compatibility this is set to 1.0 by default.
*
* @var string
*/
public $perCompatible = '1.0';
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
parent::register();
$targets = Tokens::COMPARISON_TOKENS;
$targets += Tokens::OPERATORS;
$targets += Tokens::ASSIGNMENT_TOKENS;
$targets += Tokens::BOOLEAN_OPERATORS;
$targets[] = T_INLINE_THEN;
$targets[] = T_INLINE_ELSE;
$targets[] = T_STRING_CONCAT;
$targets[] = T_INSTANCEOF;
// Also register the contexts we want to specifically skip over.
$targets[] = T_DECLARE;
return $targets;
}
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
* pointer is reached. Return `$phpcsFile->numTokens` to skip
* the rest of the file.
*/
public function process(File $phpcsFile, int $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Skip over declare statements as those should be handled by different sniffs.
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
// Parse error / live coding.
return $phpcsFile->numTokens;
}
return $tokens[$stackPtr]['parenthesis_closer'];
}
if ($this->isOperator($phpcsFile, $stackPtr) === false) {
return;
}
$operator = $tokens[$stackPtr]['content'];
// PER-CS 3.0: Exception to the rule for pipe operators in multi-catch blocks where no space is required.
// As union types didn't exist when PSR-12 was created, the pipe in catch statements
// was originally treated as a bitwise operator. This check changes the spacing requirement
// for that specific case when opting in to PER-CS 3.0 or higher.
if ($tokens[$stackPtr]['code'] === T_BITWISE_OR
&& isset($tokens[$stackPtr]['nested_parenthesis']) === true
&& version_compare($this->perCompatible, '3.0', '>=') === true
) {
// Calling array_keys() on a nested sub-array can have memory leaks, assign to a variable first.
// See https://github.com/squizlabs/PHP_CodeSniffer/pull/2273 .
$parenthesis = $tokens[$stackPtr]['nested_parenthesis'];
$parenthesisKeys = array_keys($parenthesis);
$bracket = array_pop($parenthesisKeys);
if (isset($tokens[$bracket]['parenthesis_owner']) === true
&& $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH
) {
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE
&& strpos($tokens[($stackPtr - 1)]['content'], $phpcsFile->eolChar) === false
&& $tokens[($stackPtr - 1)]['column'] !== 1
) {
$error = 'Expected 0 spaces before "%s" in multi-catch statement; %s found';
$data = [
$operator,
$tokens[($stackPtr - 1)]['length'],
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiCatchSpaceBefore', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
}
}
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
&& strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) === false
) {
$error = 'Expected 0 spaces after "%s" in multi-catch statement; %s found';
$data = [
$operator,
$tokens[($stackPtr + 1)]['length'],
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiCatchSpaceAfter', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
}
}
// Now that this special case is handled, we can return early as we don't need to do
// further checks.
return;
}
}
$checkBefore = true;
$checkAfter = true;
// Skip short ternary.
if ($tokens[($stackPtr)]['code'] === T_INLINE_ELSE
&& $tokens[($stackPtr - 1)]['code'] === T_INLINE_THEN
) {
$checkBefore = false;
}
// Skip operator with comment on previous line.
if ($tokens[($stackPtr - 1)]['code'] === T_COMMENT
&& $tokens[($stackPtr - 1)]['line'] < $tokens[$stackPtr]['line']
) {
$checkBefore = false;
}
if (isset($tokens[($stackPtr + 1)]) === true) {
// Skip short ternary.
if ($tokens[$stackPtr]['code'] === T_INLINE_THEN
&& $tokens[($stackPtr + 1)]['code'] === T_INLINE_ELSE
) {
$checkAfter = false;
}
} else {
// Skip partial files.
$checkAfter = false;
}
if ($checkBefore === true && $tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected at least 1 space before "%s"; 0 found';
$data = [$operator];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore', $data);
if ($fix === true) {
$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
}
}
if ($checkAfter === true && $tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected at least 1 space after "%s"; 0 found';
$data = [$operator];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfter', $data);
if ($fix === true) {
$phpcsFile->fixer->addContent($stackPtr, ' ');
}
}
}
}