|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs; |
|
6
|
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\File; |
|
8
|
|
|
use BestIt\CodeSniffer\Helper\ExceptionHelper; |
|
9
|
|
|
use PHP_CodeSniffer\Files\File as BaseFile; |
|
10
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
11
|
|
|
use SlevomatCodingStandard\Helpers\SuppressHelper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class AbstractSniff |
|
15
|
|
|
* |
|
16
|
|
|
* @author Nick Lubisch <[email protected]> |
|
17
|
|
|
* @package BestIt\Sniffs |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractSniff implements Sniff |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The used file. |
|
23
|
|
|
* |
|
24
|
|
|
* @var File|void |
|
25
|
|
|
*/ |
|
26
|
|
|
private $file; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Position of the listened token. |
|
30
|
|
|
* |
|
31
|
|
|
* @var int|void |
|
32
|
|
|
*/ |
|
33
|
|
|
private $stackPos; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The used suppresshelper. |
|
37
|
|
|
* |
|
38
|
|
|
* @var SuppressHelper |
|
39
|
|
|
*/ |
|
40
|
|
|
private $suppressHelper = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The used token. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array|void |
|
46
|
|
|
*/ |
|
47
|
|
|
private $token; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* All tokens of the class. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array|void The tokens of the class. |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $tokens; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns true if the requirements for this sniff are met. |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool Are the requirements met and the sniff should proceed? |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function areRequirementsMet(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns an exception handler for the sniffed file. |
|
68
|
|
|
* |
|
69
|
|
|
* @return ExceptionHelper Returns the exception helper. |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getExceptionHandler(): ExceptionHelper |
|
72
|
|
|
{ |
|
73
|
|
|
return new ExceptionHelper($this->getFile()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get the sniff name. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string|null $sniffName If there is an optional sniff name. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string Returns the special sniff name in the code sniffer context. |
|
82
|
|
|
*/ |
|
83
|
|
|
private function getSniffName(?string $sniffName = null): string |
|
84
|
|
|
{ |
|
85
|
|
|
$sniffFQCN = preg_replace( |
|
86
|
|
|
'/Sniff$/', |
|
87
|
|
|
'', |
|
88
|
|
|
str_replace(['\\', '.Sniffs'], ['.', ''], static::class) |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
if ($sniffName) { |
|
|
|
|
|
|
92
|
|
|
$sniffFQCN .= '.' . $sniffName; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $sniffFQCN; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the used suppress helper. |
|
100
|
|
|
* |
|
101
|
|
|
* @return SuppressHelper The suppress helper. |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getSuppressHelper(): SuppressHelper |
|
104
|
|
|
{ |
|
105
|
|
|
if (!$this->suppressHelper) { |
|
106
|
|
|
$this->suppressHelper = new SuppressHelper(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->suppressHelper; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns true if this sniff or a rule of this sniff is suppressed with the slevomat suppress annotation. |
|
114
|
|
|
* |
|
115
|
|
|
* @param null|string $rule The optional rule. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool Returns true if the sniff is suppressed. |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function isSniffSuppressed(?string $rule = null): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->getSuppressHelper()->isSniffSuppressed( |
|
122
|
|
|
$this->getFile(), |
|
123
|
|
|
$this->getStackPosition(), |
|
124
|
|
|
$this->getSniffName($rule) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Called when one of the token types that this sniff is listening for is found. |
|
130
|
|
|
* |
|
131
|
|
|
* @param BaseFile $phpcsFile The PHP_CodeSniffer file where the token was found. |
|
132
|
|
|
* @param int $stackPos The position in the PHP_CodeSniffer file's token stack where the token was found. |
|
133
|
|
|
* |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
|
|
public function process(BaseFile $phpcsFile, $stackPos) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->file = new File($phpcsFile); |
|
139
|
|
|
$this->stackPos = $stackPos; |
|
140
|
|
|
$this->tokens = $phpcsFile->getTokens(); |
|
141
|
|
|
$this->token = $this->tokens[$stackPos]; |
|
142
|
|
|
|
|
143
|
|
|
$this->setUp(); |
|
144
|
|
|
|
|
145
|
|
|
if ($this->areRequirementsMet()) { |
|
146
|
|
|
$this->processToken(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->tearDown(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Processes the token. |
|
154
|
|
|
* |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
|
abstract protected function processToken(): void; |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Getter for deferred PHP_CodeSniffer class. |
|
161
|
|
|
* |
|
162
|
|
|
* @return File The deferred CodeSniffer file |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getFile(): File |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->file; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the position of our token in the stack. |
|
171
|
|
|
* |
|
172
|
|
|
* @return int The position of our token in the stack. |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getStackPosition(): int |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->stackPos; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Getter for the used token. |
|
181
|
|
|
* |
|
182
|
|
|
* @return array Returns token data of the listened token |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function getToken(): array |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->token; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Do you want to setup things before processing the token? |
|
191
|
|
|
* |
|
192
|
|
|
* @return void |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function setUp(): void |
|
195
|
|
|
{ |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Is there something to destroy after processing the token? |
|
200
|
|
|
* |
|
201
|
|
|
* @return void |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function tearDown(): void |
|
204
|
|
|
{ |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: