5
5
use PHP_CodeSniffer \Files \File ;
6
6
7
7
class Helpers {
8
+ /**
9
+ * @param File $phpcsFile
10
+ * @param int $stackPtr
11
+ *
12
+ * @return int|bool
13
+ */
8
14
public static function findContainingOpeningSquareBracket (File $ phpcsFile , $ stackPtr ) {
9
- $ tokens = $ phpcsFile ->getTokens ();
10
15
$ previousStatementPtr = self ::getPreviousStatementPtr ($ phpcsFile , $ stackPtr );
11
16
return $ phpcsFile ->findPrevious (T_OPEN_SHORT_ARRAY , $ stackPtr - 1 , $ previousStatementPtr );
12
17
}
13
18
19
+ /**
20
+ * @param File $phpcsFile
21
+ * @param int $stackPtr
22
+ *
23
+ * @return int|bool
24
+ */
14
25
public static function findContainingClosingSquareBracket (File $ phpcsFile , $ stackPtr ) {
15
- $ tokens = $ phpcsFile ->getTokens ();
16
26
$ endOfStatementPtr = $ phpcsFile ->findNext ([T_SEMICOLON ], $ stackPtr + 1 );
17
- if (! $ endOfStatementPtr ) {
27
+ if (is_bool ( $ endOfStatementPtr) ) {
18
28
return false ;
19
29
}
20
30
return $ phpcsFile ->findNext (T_CLOSE_SHORT_ARRAY , $ stackPtr + 1 , $ endOfStatementPtr );
21
31
}
22
32
33
+ /**
34
+ * @param File $phpcsFile
35
+ * @param int $stackPtr
36
+ *
37
+ * @return int
38
+ */
23
39
public static function getPreviousStatementPtr (File $ phpcsFile , $ stackPtr ) {
24
- return $ phpcsFile ->findPrevious ([T_SEMICOLON , T_CLOSE_CURLY_BRACKET ], $ stackPtr - 1 ) ?: 1 ;
40
+ $ result = $ phpcsFile ->findPrevious ([T_SEMICOLON , T_CLOSE_CURLY_BRACKET ], $ stackPtr - 1 );
41
+ return is_bool ($ result ) ? 1 : $ result ;
25
42
}
26
43
44
+ /**
45
+ * @param File $phpcsFile
46
+ * @param int $stackPtr
47
+ *
48
+ * @return int|bool
49
+ */
27
50
public static function findContainingOpeningBracket (File $ phpcsFile , $ stackPtr ) {
28
51
$ tokens = $ phpcsFile ->getTokens ();
29
52
if (isset ($ tokens [$ stackPtr ]['nested_parenthesis ' ])) {
30
53
$ openPtrs = array_keys ($ tokens [$ stackPtr ]['nested_parenthesis ' ]);
31
- return end ($ openPtrs );
54
+ return ( int ) end ($ openPtrs );
32
55
}
33
56
return false ;
34
57
}
35
58
59
+ /**
60
+ * @param File $phpcsFile
61
+ * @param int $stackPtr
62
+ *
63
+ * @return int|bool
64
+ */
36
65
public static function findParenthesisOwner (File $ phpcsFile , $ stackPtr ) {
37
- $ tokens = $ phpcsFile ->getTokens ();
38
66
return $ phpcsFile ->findPrevious (T_WHITESPACE , $ stackPtr - 1 , null , true );
39
67
}
40
68
69
+ /**
70
+ * @param File $phpcsFile
71
+ * @param int[] $conditions
72
+ *
73
+ * @return bool
74
+ */
41
75
public static function areAnyConditionsAClosure (File $ phpcsFile , array $ conditions ) {
42
76
// self within a closure is invalid
43
77
$ tokens = $ phpcsFile ->getTokens ();
@@ -50,6 +84,12 @@ public static function areAnyConditionsAClosure(File $phpcsFile, array $conditio
50
84
return false ;
51
85
}
52
86
87
+
88
+ /**
89
+ * @param int[] $conditions
90
+ *
91
+ * @return bool
92
+ */
53
93
public static function areAnyConditionsAClass (array $ conditions ) {
54
94
foreach (array_reverse ($ conditions , true ) as $ scopePtr => $ scopeCode ) {
55
95
if ($ scopeCode === T_CLASS || $ scopeCode === T_TRAIT ) {
@@ -59,6 +99,11 @@ public static function areAnyConditionsAClass(array $conditions) {
59
99
return false ;
60
100
}
61
101
102
+ /**
103
+ * @param int[] $conditions
104
+ *
105
+ * @return bool
106
+ */
62
107
public static function areConditionsWithinFunctionBeforeClass (array $ conditions ) {
63
108
// Return true if the token conditions are within a function before
64
109
// they are within a class.
@@ -74,6 +119,12 @@ public static function areConditionsWithinFunctionBeforeClass(array $conditions)
74
119
return false ;
75
120
}
76
121
122
+ /**
123
+ * @param File $phpcsFile
124
+ * @param int $openPtr
125
+ *
126
+ * @return int|bool
127
+ */
77
128
public static function findPreviousFunctionPtr (File $ phpcsFile , $ openPtr ) {
78
129
// Function names are T_STRING, and return-by-reference is T_BITWISE_AND,
79
130
// so we look backwards from the opening bracket for the first thing that
@@ -87,13 +138,13 @@ public static function findPreviousFunctionPtr(File $phpcsFile, $openPtr) {
87
138
* @param File $phpcsFile
88
139
* @param int $stackPtr
89
140
*
90
- * @return int|false
141
+ * @return int|bool
91
142
*/
92
143
public static function findFunctionCall (File $ phpcsFile , $ stackPtr ) {
93
144
$ tokens = $ phpcsFile ->getTokens ();
94
145
95
146
$ openPtr = Helpers::findContainingOpeningBracket ($ phpcsFile , $ stackPtr );
96
- if ($ openPtr ) {
147
+ if (is_int ( $ openPtr) ) {
97
148
// First non-whitespace thing and see if it's a T_STRING function name
98
149
$ functionPtr = $ phpcsFile ->findPrevious (T_WHITESPACE , $ openPtr - 1 , null , true , null , true );
99
150
if ($ tokens [$ functionPtr ]['code ' ] === T_STRING ) {
@@ -103,6 +154,12 @@ public static function findFunctionCall(File $phpcsFile, $stackPtr) {
103
154
return false ;
104
155
}
105
156
157
+ /**
158
+ * @param File $phpcsFile
159
+ * @param int $stackPtr
160
+ *
161
+ * @return array[]|false
162
+ */
106
163
public static function findFunctionCallArguments (File $ phpcsFile , $ stackPtr ) {
107
164
$ tokens = $ phpcsFile ->getTokens ();
108
165
@@ -129,19 +186,27 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
129
186
$ argPtrs = [];
130
187
$ lastPtr = $ openPtr ;
131
188
$ lastArgComma = $ openPtr ;
132
- while (($ nextPtr = $ phpcsFile ->findNext (T_COMMA , $ lastPtr + 1 , $ closePtr )) !== false ) {
189
+ $ nextPtr = $ phpcsFile ->findNext (T_COMMA , $ lastPtr + 1 , $ closePtr );
190
+ while (is_int ($ nextPtr )) {
133
191
if (Helpers::findContainingOpeningBracket ($ phpcsFile , $ nextPtr ) == $ openPtr ) {
134
192
// Comma is at our level of brackets, it's an argument delimiter.
135
193
array_push ($ argPtrs , range ($ lastArgComma + 1 , $ nextPtr - 1 ));
136
194
$ lastArgComma = $ nextPtr ;
137
195
}
138
196
$ lastPtr = $ nextPtr ;
197
+ $ nextPtr = $ phpcsFile ->findNext (T_COMMA , $ lastPtr + 1 , $ closePtr );
139
198
}
140
199
array_push ($ argPtrs , range ($ lastArgComma + 1 , $ closePtr - 1 ));
141
200
142
201
return $ argPtrs ;
143
202
}
144
203
204
+ /**
205
+ * @param File $phpcsFile
206
+ * @param int $stackPtr
207
+ *
208
+ * @return int
209
+ */
145
210
public static function findWhereAssignExecuted (File $ phpcsFile , $ stackPtr ) {
146
211
$ tokens = $ phpcsFile ->getTokens ();
147
212
@@ -171,6 +236,12 @@ public static function findWhereAssignExecuted(File $phpcsFile, $stackPtr) {
171
236
return $ assignEndTokens [0 ];
172
237
}
173
238
239
+ /**
240
+ * @param File $phpcsFile
241
+ * @param int $stackPtr
242
+ *
243
+ * @return int|bool
244
+ */
174
245
public static function isNextThingAnAssign (File $ phpcsFile , $ stackPtr ) {
175
246
$ tokens = $ phpcsFile ->getTokens ();
176
247
@@ -184,16 +255,27 @@ public static function isNextThingAnAssign(File $phpcsFile, $stackPtr) {
184
255
return false ;
185
256
}
186
257
258
+ /**
259
+ * @param string $varName
260
+ *
261
+ * @return string
262
+ */
187
263
public static function normalizeVarName ($ varName ) {
188
- return preg_replace ('/[{}$]/ ' , '' , $ varName );
264
+ $ result = preg_replace ('/[{}$]/ ' , '' , $ varName );
265
+ return $ result ? $ result : $ varName ;
189
266
}
190
267
268
+ /**
269
+ * @param File $phpcsFile
270
+ * @param int $stackPtr
271
+ *
272
+ * @return int|bool
273
+ */
191
274
public static function findFunctionPrototype (File $ phpcsFile , $ stackPtr ) {
192
275
$ tokens = $ phpcsFile ->getTokens ();
193
- $ token = $ tokens [$ stackPtr ];
194
276
195
277
$ openPtr = Helpers::findContainingOpeningBracket ($ phpcsFile , $ stackPtr );
196
- if ($ openPtr === false ) {
278
+ if (! is_int ( $ openPtr) ) {
197
279
return false ;
198
280
}
199
281
$ functionPtr = Helpers::findPreviousFunctionPtr ($ phpcsFile , $ openPtr );
@@ -226,7 +308,7 @@ public static function findVariableScope(File $phpcsFile, $stackPtr) {
226
308
}
227
309
228
310
$ scopePtr = Helpers::findFunctionPrototype ($ phpcsFile , $ stackPtr );
229
- if ($ scopePtr !== false ) {
311
+ if (is_int ( $ scopePtr) ) {
230
312
return $ scopePtr ;
231
313
}
232
314
@@ -239,6 +321,11 @@ public static function findVariableScope(File $phpcsFile, $stackPtr) {
239
321
return 0 ;
240
322
}
241
323
324
+ /**
325
+ * @param string $message
326
+ *
327
+ * @return void
328
+ */
242
329
public static function debug ($ message ) {
243
330
if (! defined ('PHP_CODESNIFFER_VERBOSITY ' )) {
244
331
return ;
@@ -247,4 +334,15 @@ public static function debug($message) {
247
334
echo PHP_EOL . "VariableAnalysisSniff: DEBUG: $ message " . PHP_EOL ;
248
335
}
249
336
}
337
+
338
+ /**
339
+ * @param string $pattern
340
+ * @param string $value
341
+ *
342
+ * @return string[]
343
+ */
344
+ public static function splitStringToArray ($ pattern , $ value ) {
345
+ $ result = preg_split ($ pattern , $ value );
346
+ return is_array ($ result ) ? $ result : [];
347
+ }
250
348
}
0 commit comments