-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathOperatorSpacingSniff.php
More file actions
149 lines (127 loc) · 5.33 KB
/
OperatorSpacingSniff.php
File metadata and controls
149 lines (127 loc) · 5.33 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
<?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.
*
* @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'];
$checkBefore = true;
$checkAfter = true;
// PER-CS 2.0: Exception to the rule for catch blocks (union types) 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 disables the spacing requirement
// for that specific case when opting in to PER-CS 2.0 or higher.
if ($operator === '|' && version_compare($this->perCompatible, '2.0', '>=') === true) {
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
$parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']);
$bracket = array_pop($parenthesis);
if (isset($tokens[$bracket]['parenthesis_owner']) === true
&& $tokens[$tokens[$bracket]['parenthesis_owner']]['code'] === T_CATCH
) {
$checkBefore = false;
$checkAfter = false;
}
}
}
// 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, ' ');
}
}
}
}