-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
IsShortArrayOrListWithCache.php
272 lines (247 loc) · 8.5 KB
/
IsShortArrayOrListWithCache.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/
namespace PHPCSUtils\Internal;
use PHP_CodeSniffer\Files\File;
use PHPCSUtils\Internal\Cache;
use PHPCSUtils\Internal\IsShortArrayOrList;
use PHPCSUtils\Tokens\Collections;
/**
* Determination of short array vs short list vs square brackets.
*
* Uses caching of previous results to mitigate performance issues.
*
* ---------------------------------------------------------------------------------------------
* This class is only intended for internal use by PHPCSUtils and is not part of the public API.
* This also means that it has no promise of backward compatibility.
*
* End-users should use the {@see \PHPCSUtils\Utils\Arrays::isShortArray()}
* or the {@see \PHPCSUtils\Utils\Lists::isShortList()} method instead.
* ---------------------------------------------------------------------------------------------
*
* @internal
*
* @since 1.0.0-alpha4
*/
final class IsShortArrayOrListWithCache
{
/**
* The PHPCS file in which the current stackPtr was found.
*
* @since 1.0.0-alpha4
*
* @var \PHP_CodeSniffer\Files\File
*/
private $phpcsFile;
/**
* The token stack from the current file.
*
* @since 1.0.0-alpha4
*
* @var array
*/
private $tokens;
/**
* The current stack pointer.
*
* @since 1.0.0-alpha4
*
* @var int
*/
private $stackPtr;
/**
* Stack pointer to the open bracket.
*
* @since 1.0.0-alpha4
*
* @var int
*/
private $opener;
/**
* Valid return values from the IsShortArrayOrList class.
*
* @since 1.0.0-alpha4
*
* @var string[]
*/
private $validTypes = [
IsShortArrayOrList::SHORT_ARRAY => IsShortArrayOrList::SHORT_ARRAY,
IsShortArrayOrList::SHORT_LIST => IsShortArrayOrList::SHORT_LIST,
IsShortArrayOrList::SQUARE_BRACKETS => IsShortArrayOrList::SQUARE_BRACKETS,
];
/**
* Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short array construct
* and not a short list.
*
* This method also accepts `T_OPEN/CLOSE_SQUARE_BRACKET` tokens to allow it to be
* PHPCS cross-version compatible as the short array tokenizing has been plagued by
* a number of bugs over time.
*
* @since 1.0.0-alpha4
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the short array bracket token.
*
* @return bool `TRUE` if the token passed is the open/close bracket of a short array.
* `FALSE` if the token is a short list bracket, a plain square bracket
* or not one of the accepted tokens.
*/
public static function isShortArray(File $phpcsFile, $stackPtr)
{
return (self::getType($phpcsFile, $stackPtr) === IsShortArrayOrList::SHORT_ARRAY);
}
/**
* Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short list construct
* in contrast to a short array.
*
* This method also accepts `T_OPEN/CLOSE_SQUARE_BRACKET` tokens to allow it to be
* PHPCS cross-version compatible as the short array tokenizing has been plagued by
* a number of bugs over time, which affects the short list determination.
*
* @since 1.0.0-alpha4
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the short array bracket token.
*
* @return bool `TRUE` if the token passed is the open/close bracket of a short list.
* `FALSE` if the token is a short array bracket or plain square bracket
* or not one of the accepted tokens.
*/
public static function isShortList(File $phpcsFile, $stackPtr)
{
return (self::getType($phpcsFile, $stackPtr) === IsShortArrayOrList::SHORT_LIST);
}
/**
* Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short array or short list construct.
*
* This method also accepts `T_OPEN/CLOSE_SQUARE_BRACKET` tokens to allow it to be
* PHPCS cross-version compatible as the short array tokenizing has been plagued by
* a number of bugs over time, which affects the short list determination.
*
* @since 1.0.0-alpha4
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the short array bracket token.
*
* @return string|false The type of construct this bracket was determined to be.
* Either 'short array', 'short list' or 'square brackets'.
* Or FALSE is this was not a bracket token.
*/
public static function getType(File $phpcsFile, $stackPtr)
{
return (new self($phpcsFile, $stackPtr))->process();
}
/**
* Constructor.
*
* @since 1.0.0-alpha4
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the short array bracket token.
*
* @return void
*/
private function __construct(File $phpcsFile, $stackPtr)
{
$this->phpcsFile = $phpcsFile;
$this->tokens = $phpcsFile->getTokens();
$this->stackPtr = $stackPtr;
}
/**
* Determine whether a T_[OPEN|CLOSE}_[SHORT_ARRAY|SQUARE_BRACKET] token is a short array
* or short list construct using previously cached results whenever possible.
*
* @since 1.0.0-alpha4
*
* @return string|false The type of construct this bracket was determined to be.
* Either 'short array', 'short list' or 'square brackets'.
* Or FALSE is this was not a bracket token.
*/
private function process()
{
if ($this->isValidStackPtr() === false) {
return false;
}
$this->opener = $this->getOpener();
/*
* Check the cache in case we've seen this token before.
*/
$type = $this->getFromCache();
if ($type !== false) {
return $type;
}
/*
* If we've not seen the token before, try and solve it and cache the results.
*/
$solver = new IsShortArrayOrList($this->phpcsFile, $this->opener);
$type = $solver->solve();
$this->updateCache($type);
return $type;
}
/**
* Verify the passed token could potentially be a short array or short list token.
*
* @since 1.0.0-alpha4
*
* @return bool
*/
private function isValidStackPtr()
{
return (isset($this->tokens[$this->stackPtr]) === true
&& isset(Collections::shortArrayTokensBC()[$this->tokens[$this->stackPtr]['code']]) === true);
}
/**
* Get the stack pointer to the short array/list opener.
*
* @since 1.0.0-alpha4
*
* @return int
*/
private function getOpener()
{
$opener = $this->stackPtr;
if (isset($this->tokens[$this->stackPtr]['bracket_opener'])) {
$opener = $this->tokens[$this->stackPtr]['bracket_opener'];
}
return $opener;
}
/**
* Retrieve the bracket "type" of a token from the cache.
*
* @since 1.0.0-alpha4
*
* @return string|false The previously determined type (which could be an empty string)
* or FALSE if no cache entry was found for this token.
*/
private function getFromCache()
{
if (Cache::isCached($this->phpcsFile, __CLASS__, $this->opener) === true) {
return Cache::get($this->phpcsFile, __CLASS__, $this->opener);
}
return false;
}
/**
* Update the cache with information about a particular bracket token.
*
* @since 1.0.0-alpha4
*
* @param string $type The type this bracket has been determined to be.
* Either 'short array', 'short list' or 'square brackets'.
*
* @return void
*/
private function updateCache($type)
{
$result = false;
if (isset($this->validTypes[$type]) === true) {
$result = $type;
}
Cache::set($this->phpcsFile, __CLASS__, $this->opener, $result);
}
}