|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\CodeSniffer; |
|
6
|
|
|
|
|
7
|
|
|
use PHP_CodeSniffer_File; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class File |
|
11
|
|
|
* |
|
12
|
|
|
* Wrapper Class for PHP_CodeSniffer_File to provide a consistent way to replace int|bool returns |
|
13
|
|
|
* with int returns (false => -1) |
|
14
|
|
|
* Additionally there could be some architecture changes in the future, like Token-Objects and so on. |
|
15
|
|
|
* |
|
16
|
|
|
* @package BestIt\CodeSniffer |
|
17
|
|
|
* @author Nick Lubisch <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class File |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The CodeSniffer file |
|
23
|
|
|
* |
|
24
|
|
|
* @var PHP_CodeSniffer_File |
|
25
|
|
|
*/ |
|
26
|
|
|
private $baseFile; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Wrapped PHP_CodeSniffer_Fixer |
|
30
|
|
|
* |
|
31
|
|
|
* @var Fixer |
|
32
|
|
|
*/ |
|
33
|
|
|
private $fixer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Contains the advanced token stack |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $tokens; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* File constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param PHP_CodeSniffer_File $baseFile CodeSniffer file |
|
46
|
|
|
*/ |
|
47
|
121 |
|
public function __construct(PHP_CodeSniffer_File $baseFile) |
|
48
|
|
|
{ |
|
49
|
121 |
|
$this->baseFile = $baseFile; |
|
50
|
121 |
|
$this->fixer = new Fixer($this, $this->baseFile->fixer); |
|
51
|
121 |
|
$this->tokens = $this->getAdvancedTokens(); |
|
52
|
121 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Adds the pointer to all token data arrays. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array Advanced token stack. |
|
58
|
|
|
*/ |
|
59
|
121 |
|
private function getAdvancedTokens(): array |
|
60
|
|
|
{ |
|
61
|
121 |
|
$tokens = []; |
|
62
|
|
|
|
|
63
|
121 |
|
foreach ($this->baseFile->getTokens() as $tokenPtr => $token) { |
|
64
|
121 |
|
$token['pointer'] = $tokenPtr; |
|
65
|
121 |
|
$tokens[$tokenPtr] = $token; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
121 |
|
return $tokens; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Records a fixable error against a specific token in the file. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $error The error message. |
|
75
|
|
|
* @param int $stackPtr The stack position where the error occurred. |
|
76
|
|
|
* @param string $code A violation code unique to the sniff message. |
|
77
|
|
|
* @param array $data Replacements for the error message. |
|
78
|
|
|
* @param int $severity The severity level for this error. |
|
79
|
|
|
* A value of 0 will be converted into the default severity level. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool Returns true if the error was recorded and should be fixed. |
|
82
|
|
|
*/ |
|
83
|
68 |
|
public function addFixableError( |
|
84
|
|
|
string $error, |
|
85
|
|
|
int $stackPtr, |
|
86
|
|
|
string $code = '', |
|
87
|
|
|
array $data = [], |
|
88
|
|
|
int $severity = 0 |
|
89
|
|
|
): bool { |
|
90
|
68 |
|
return $this->baseFile->addFixableError($error, $stackPtr, $code, $data, $severity); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the token stack for this file. |
|
95
|
|
|
* |
|
96
|
|
|
* @return array Return array of token data |
|
97
|
|
|
*/ |
|
98
|
121 |
|
public function getTokens(): array |
|
99
|
|
|
{ |
|
100
|
121 |
|
return $this->tokens; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns the position of the previous specified token(s). |
|
105
|
|
|
* |
|
106
|
|
|
* If a value is specified, the previous token of the specified type(s) |
|
107
|
|
|
* containing the specified value will be returned. |
|
108
|
|
|
* |
|
109
|
|
|
* Returns -1 if no token can be found. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $types The type(s) of tokens to search for. |
|
112
|
|
|
* @param int $start The position to start searching from in the token stack. |
|
113
|
|
|
* @param int $end The end position to fail if no token is found. |
|
114
|
|
|
* if not specified or null, end will default to the start of the token stack. |
|
115
|
|
|
* @param bool $exclude If true, find the previous token that are NOT of the types specified in $types. |
|
116
|
|
|
* @param string $value The value that the token(s) must be equal to. |
|
117
|
|
|
* If value is omitted, tokens with any value will be returned. |
|
118
|
|
|
* @param bool $local If true, tokens outside the current statement will not be checked. |
|
119
|
|
|
* IE. checking will stop at the previous semi-colon found. |
|
120
|
|
|
* |
|
121
|
|
|
* @return int Pointer to the found token |
|
122
|
|
|
* |
|
123
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
124
|
|
|
*/ |
|
125
|
121 |
|
public function findPrevious( |
|
126
|
|
|
array $types, |
|
127
|
|
|
int $start, |
|
128
|
|
|
?int $end = null, |
|
129
|
|
|
bool $exclude = false, |
|
130
|
|
|
?string $value = null, |
|
131
|
|
|
bool $local = false |
|
132
|
|
|
): int { |
|
133
|
121 |
|
$pointer = $this->baseFile->findPrevious($types, $start, $end, $exclude, $value, $local); |
|
134
|
|
|
|
|
135
|
121 |
|
return $this->preparePointer($pointer); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Records an error against a specific token in the file. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $error The error message. |
|
142
|
|
|
* @param int $stackPtr The stack position where the error occurred. |
|
143
|
|
|
* @param string $code A violation code unique to the sniff message. |
|
144
|
|
|
* @param array $data Replacements for the error message. |
|
145
|
|
|
* @param int $severity The severity level for this error. A value of 0 |
|
146
|
|
|
* will be converted into the default severity level. |
|
147
|
|
|
* @param bool $fixable Can the error be fixed by the sniff? |
|
148
|
|
|
* |
|
149
|
|
|
* @return bool Returns true if setting the error was done or false |
|
150
|
|
|
* |
|
151
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
152
|
|
|
*/ |
|
153
|
64 |
|
public function addError( |
|
154
|
|
|
string $error, |
|
155
|
|
|
int $stackPtr, |
|
156
|
|
|
string $code = '', |
|
157
|
|
|
array $data = [], |
|
158
|
|
|
int $severity = 0, |
|
159
|
|
|
bool $fixable = false |
|
160
|
|
|
): bool { |
|
161
|
64 |
|
return $this->baseFile->addError($error, $stackPtr, $code, $data, $severity, $fixable); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Records an error against a specific line in the file. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $error The error message. |
|
168
|
|
|
* @param int $line The line on which the error occurred. |
|
169
|
|
|
* @param string $code A violation code unique to the sniff message. |
|
170
|
|
|
* @param array $data Replacements for the error message. |
|
171
|
|
|
* @param int $severity The severity level for this error. A value of 0 |
|
172
|
|
|
* will be converted into the default severity level. |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool Returns true of the error got recorded |
|
175
|
|
|
*/ |
|
176
|
12 |
|
public function addErrorOnLine( |
|
177
|
|
|
string $error, |
|
178
|
|
|
int $line, |
|
179
|
|
|
string $code = '', |
|
180
|
|
|
array $data = [], |
|
181
|
|
|
int $severity = 0 |
|
182
|
|
|
): bool { |
|
183
|
12 |
|
return $this->baseFile->addErrorOnLine($error, $line, $code, $data, $severity); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the position of the next specified token(s). |
|
188
|
|
|
* |
|
189
|
|
|
* If a value is specified, the next token of the specified type(s) |
|
190
|
|
|
* containing the specified value will be returned. |
|
191
|
|
|
* |
|
192
|
|
|
* Returns false if no token can be found. |
|
193
|
|
|
* |
|
194
|
|
|
* @param array $types The type(s) of tokens to search for. |
|
195
|
|
|
* @param int $start The position to start searching from in the |
|
196
|
|
|
* token stack. |
|
197
|
|
|
* @param int $end The end position to fail if no token is found. if not specified or null, end will default to |
|
198
|
|
|
* the end of the token stack. |
|
199
|
|
|
* @param bool $exclude If true, find the next token that is NOT of a type specified in $types. |
|
200
|
|
|
* @param string $value The value that the token(s) must be equal to. |
|
201
|
|
|
* If value is omitted, tokens with any value will be returned. |
|
202
|
|
|
* @param bool $local If true, tokens outside the current statement will not be checked. i.e., checking will stop |
|
203
|
|
|
* at the next semi-colon found. |
|
204
|
|
|
* |
|
205
|
|
|
* @return int Returns the pointer of the token or -1 |
|
206
|
|
|
* |
|
207
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
208
|
|
|
*/ |
|
209
|
113 |
|
public function findNext( |
|
210
|
|
|
array $types, |
|
211
|
|
|
int $start, |
|
212
|
|
|
?int $end = null, |
|
213
|
|
|
bool $exclude = false, |
|
214
|
|
|
?string $value = null, |
|
215
|
|
|
bool $local = false |
|
216
|
|
|
): int { |
|
217
|
113 |
|
$result = $this->baseFile->findNext($types, $start, $end, $exclude, $value, $local); |
|
218
|
|
|
|
|
219
|
113 |
|
return $this->preparePointer($result); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Prepares given pointer result. |
|
224
|
|
|
* |
|
225
|
|
|
* @param int|bool $pointer Pointer of a token |
|
226
|
|
|
* |
|
227
|
|
|
* @return int Pointer or -1 when not found |
|
228
|
|
|
*/ |
|
229
|
121 |
|
public function preparePointer($pointer): int |
|
230
|
|
|
{ |
|
231
|
121 |
|
return $pointer !== false ? $pointer : -1; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Returns the Wrapped PHP_CodeSniffer_Fixer |
|
236
|
|
|
* |
|
237
|
|
|
* @return Fixer Returns the wrapped PHP_CodeSniffer_Fixer |
|
238
|
|
|
*/ |
|
239
|
33 |
|
public function getFixer(): Fixer |
|
240
|
|
|
{ |
|
241
|
33 |
|
return $this->fixer; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Returns the eol char of the file |
|
246
|
|
|
* |
|
247
|
|
|
* @return string Returns the EndOfLine-Character of the processed file |
|
248
|
|
|
*/ |
|
249
|
87 |
|
public function getEolChar(): string |
|
250
|
|
|
{ |
|
251
|
87 |
|
return $this->baseFile->eolChar; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|