• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHPCSUtils / 7142774330

08 Dec 2023 02:50PM UTC coverage: 99.172% (-0.2%) from 99.343%
7142774330

push

github

web-flow
Merge pull request #527 from PHPCSStandards/develop

Release 1.0.9

21 of 21 new or added lines in 4 files covered. (100.0%)

6 existing lines in 2 files now uncovered.

2873 of 2897 relevant lines covered (99.17%)

147.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.75
/PHPCSUtils/Tokens/Collections.php
1
<?php
2
/**
3
 * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4
 *
5
 * @package   PHPCSUtils
6
 * @copyright 2019-2020 PHPCSUtils Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCSStandards/PHPCSUtils
9
 */
10

11
namespace PHPCSUtils\Tokens;
12

13
use PHPCSUtils\BackCompat\Helper;
14
use PHPCSUtils\Exceptions\InvalidTokenArray;
15

16
/**
17
 * Collections of related tokens as often used and needed for sniffs.
18
 *
19
 * These are additional "token groups" to compliment the ones available through the PHPCS
20
 * native {@see \PHP_CodeSniffer\Util\Tokens} class.
21
 *
22
 * @see \PHP_CodeSniffer\Util\Tokens    PHPCS native token groups.
23
 * @see \PHPCSUtils\BackCompat\BCTokens Backward compatible version of the PHPCS native token groups.
24
 *
25
 * @since 1.0.0
26
 *
27
 * @method static array alternativeControlStructureSyntaxes()      Tokens for control structures which can use the
28
 *                                                                 alternative control structure syntax.
29
 * @method static array alternativeControlStructureSyntaxClosers() Tokens representing alternative control structure
30
 *                                                                 syntax closer keywords.
31
 * @method static array arrayTokens()                              Tokens which are used to create arrays.
32
 * @method static array classModifierKeywords()                    Modifier keywords which can be used for a class
33
 *                                                                 declaration.
34
 * @method static array closedScopes()                             List of tokens which represent "closed" scopes.
35
 * @method static array constantModifierKeywords()                 Tokens which can be used as modifiers for a constant
36
 *                                                                 declaration (in OO structures).
37
 * @method static array controlStructureTokens()                   Control structure tokens.
38
 * @method static array functionDeclarationTokens()                Tokens which represent a keyword which starts
39
 *                                                                 a function declaration.
40
 * @method static array incrementDecrementOperators()              Increment/decrement operator tokens.
41
 * @method static array listTokens()                               Tokens which are used to create lists.
42
 * @method static array namespaceDeclarationClosers()              List of tokens which can end a namespace
43
 *                                                                 declaration statement.
44
 * @method static array nameTokens()                               Tokens used for "names", be it namespace, OO,
45
 *                                                                 function
46
 *                                                                 or constant names.
47
 * @method static array objectOperators()                          Object operator tokens.
48
 * @method static array ooCanExtend()                              OO structures which can use the "extends" keyword.
49
 * @method static array ooCanImplement()                           OO structures which can use the "implements" keyword.
50
 * @method static array ooConstantScopes()                         OO scopes in which constants can be declared.
51
 * @method static array ooHierarchyKeywords()                      Tokens types used for "forwarding" calls within
52
 *                                                                 OO structures.
53
 * @method static array ooPropertyScopes()                         OO scopes in which properties can be declared.
54
 * @method static array phpOpenTags()                              Tokens which open PHP.
55
 * @method static array propertyModifierKeywords()                 Modifier keywords which can be used for a property
56
 *                                                                 declaration.
57
 * @method static array shortArrayTokens()                         Tokens which are used for short arrays.
58
 * @method static array shortListTokens()                          Tokens which are used for short lists.
59
 * @method static array textStringStartTokens()                    Tokens which can start a - potentially multi-line -
60
 *                                                                 text string.
61
 */
62
final class Collections
63
{
64

65
    /**
66
     * Tokens for control structures which can use the alternative control structure syntax.
67
     *
68
     * @since 1.0.0 Use the {@see Collections::alternativeControlStructureSyntaxes()} method for access.
69
     *
70
     * @var array<int, int>
71
     */
72
    private static $alternativeControlStructureSyntaxes = [
73
        \T_IF      => \T_IF,
74
        \T_ELSEIF  => \T_ELSEIF,
75
        \T_ELSE    => \T_ELSE,
76
        \T_FOR     => \T_FOR,
77
        \T_FOREACH => \T_FOREACH,
78
        \T_SWITCH  => \T_SWITCH,
79
        \T_WHILE   => \T_WHILE,
80
        \T_DECLARE => \T_DECLARE,
81
    ];
82

83
    /**
84
     * Tokens representing alternative control structure syntax closer keywords.
85
     *
86
     * @since 1.0.0 Use the {@see Collections::alternativeControlStructureSyntaxClosers()} method for access.
87
     *
88
     * @var array<int, int>
89
     */
90
    private static $alternativeControlStructureSyntaxClosers = [
91
        \T_ENDIF      => \T_ENDIF,
92
        \T_ENDFOR     => \T_ENDFOR,
93
        \T_ENDFOREACH => \T_ENDFOREACH,
94
        \T_ENDWHILE   => \T_ENDWHILE,
95
        \T_ENDSWITCH  => \T_ENDSWITCH,
96
        \T_ENDDECLARE => \T_ENDDECLARE,
97
    ];
98

99
    /**
100
     * Tokens which can open an array (PHPCS cross-version compatible).
101
     *
102
     * Should only be used selectively.
103
     * Depending on the PHPCS version, the token array will be expanded in the associated method.
104
     *
105
     * @see \PHPCSUtils\Tokens\Collections::arrayTokensBC()      Related method to retrieve tokens used
106
     *                                                           for arrays (PHPCS cross-version).
107
     * @see \PHPCSUtils\Tokens\Collections::shortArrayTokensBC() Related method to retrieve only tokens used
108
     *                                                           for short arrays (PHPCS cross-version).
109
     *
110
     * @since 1.0.0 Use the {@see Collections::arrayOpenTokensBC()} method for access.
111
     *
112
     * @var array<int|string, int|string>
113
     */
114
    private static $arrayOpenTokensBC = [
115
        \T_ARRAY            => \T_ARRAY,
116
        \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
117
    ];
118

119
    /**
120
     * Tokens which are used to create arrays.
121
     *
122
     * @see \PHPCSUtils\Tokens\Collections::arrayOpenTokensBC() Related method to retrieve only the "open" tokens
123
     *                                                          used for arrays (PHPCS cross-version compatible).
124
     * @see \PHPCSUtils\Tokens\Collections::arrayTokensBC()     Related method to retrieve tokens used
125
     *                                                          for arrays (PHPCS cross-version compatible).
126
     * @see \PHPCSUtils\Tokens\Collections::shortArrayTokens()  Related method to retrieve only tokens used
127
     *                                                          for short arrays.
128
     *
129
     * @since 1.0.0 Use the {@see Collections::arrayTokens()} method for access.
130
     *
131
     * @var array<int|string, int|string>
132
     */
133
    private static $arrayTokens = [
134
        \T_ARRAY             => \T_ARRAY,
135
        \T_OPEN_SHORT_ARRAY  => \T_OPEN_SHORT_ARRAY,
136
        \T_CLOSE_SHORT_ARRAY => \T_CLOSE_SHORT_ARRAY,
137
    ];
138

139
    /**
140
     * Modifier keywords which can be used for a class declaration.
141
     *
142
     * @since 1.0.0 Use the {@see Collections::classModifierKeywords()} method for access.
143
     *
144
     * @var array<int|string, int|string>
145
     */
146
    private static $classModifierKeywords = [
147
        \T_FINAL    => \T_FINAL,
148
        \T_ABSTRACT => \T_ABSTRACT,
149
        \T_READONLY => \T_READONLY,
150
    ];
151

152
    /**
153
     * List of tokens which represent "closed" scopes.
154
     *
155
     * I.e. anything declared within that scope - except for other closed scopes - is
156
     * outside of the global namespace.
157
     *
158
     * This list doesn't contain the `T_NAMESPACE` token on purpose as variables declared
159
     * within a namespace scope are still global and not limited to that namespace.
160
     *
161
     * @since 1.0.0 Use the {@see Collections::closedScopes()} method for access.
162
     *
163
     * @var array<int|string, int|string>
164
     */
165
    private static $closedScopes = [
166
        \T_CLASS      => \T_CLASS,
167
        \T_ANON_CLASS => \T_ANON_CLASS,
168
        \T_INTERFACE  => \T_INTERFACE,
169
        \T_TRAIT      => \T_TRAIT,
170
        \T_ENUM       => \T_ENUM,
171
        \T_FUNCTION   => \T_FUNCTION,
172
        \T_CLOSURE    => \T_CLOSURE,
173
    ];
174

175
    /**
176
     * Modifier keywords which can be used for constant declarations (in OO structures).
177
     *
178
     * - PHP 7.1 added class constants visibility support.
179
     * - PHP 8.1 added support for final class constants.
180
     *
181
     * @since 1.0.0 Use the {@see Collections::constantModifierKeywords()} method for access.
182
     *
183
     * @var array<int|string, int|string>
184
     */
185
    private static $constantModifierKeywords = [
186
        \T_PUBLIC    => \T_PUBLIC,
187
        \T_PRIVATE   => \T_PRIVATE,
188
        \T_PROTECTED => \T_PROTECTED,
189
        \T_FINAL     => \T_FINAL,
190
    ];
191

192
    /**
193
     * Control structure tokens.
194
     *
195
     * @since 1.0.0 Use the {@see Collections::controlStructureTokens()} method for access.
196
     *
197
     * @var array<int|string, int|string>
198
     */
199
    private static $controlStructureTokens = [
200
        \T_IF      => \T_IF,
201
        \T_ELSEIF  => \T_ELSEIF,
202
        \T_ELSE    => \T_ELSE,
203
        \T_FOR     => \T_FOR,
204
        \T_FOREACH => \T_FOREACH,
205
        \T_SWITCH  => \T_SWITCH,
206
        \T_DO      => \T_DO,
207
        \T_WHILE   => \T_WHILE,
208
        \T_DECLARE => \T_DECLARE,
209
        \T_MATCH   => \T_MATCH,
210
    ];
211

212
    /**
213
     * Tokens which represent a keyword which starts a function declaration.
214
     *
215
     * @since 1.0.0 Use the {@see Collections::functionDeclarationTokens()} method for access.
216
     *
217
     * @var array<int|string, int|string>
218
     */
219
    private static $functionDeclarationTokens = [
220
        \T_FUNCTION => \T_FUNCTION,
221
        \T_CLOSURE  => \T_CLOSURE,
222
        \T_FN       => \T_FN,
223
    ];
224

225
    /**
226
     * Increment/decrement operator tokens.
227
     *
228
     * @since 1.0.0 Use the {@see Collections::incrementDecrementOperators()} method for access.
229
     *
230
     * @var array<int, int>
231
     */
232
    private static $incrementDecrementOperators = [
233
        \T_DEC => \T_DEC,
234
        \T_INC => \T_INC,
235
    ];
236

237
    /**
238
     * Tokens which can open a list construct (PHPCS cross-version compatible).
239
     *
240
     * Should only be used selectively.
241
     * Depending on the PHPCS version, the token array will be expanded in the associated method.
242
     *
243
     * @see \PHPCSUtils\Tokens\Collections::listTokensBC()      Related method to retrieve tokens used
244
     *                                                          for lists (PHPCS cross-version).
245
     * @see \PHPCSUtils\Tokens\Collections::shortListTokensBC() Related method to retrieve only tokens used
246
     *                                                          for short lists (PHPCS cross-version).
247
     *
248
     * @since 1.0.0 Use the {@see Collections::listOpenTokensBC()} method for access.
249
     *
250
     * @var array<int|string, int|string>
251
     */
252
    private static $listOpenTokensBC = [
253
        \T_LIST             => \T_LIST,
254
        \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
255
    ];
256

257
    /**
258
     * Tokens which are used to create lists.
259
     *
260
     * @see \PHPCSUtils\Tokens\Collections::listTokensBC()    Related method to retrieve tokens used
261
     *                                                        for lists (PHPCS cross-version).
262
     * @see \PHPCSUtils\Tokens\Collections::shortListTokens() Related method to retrieve only tokens used
263
     *                                                        for short lists.
264
     *
265
     * @since 1.0.0 Use the {@see Collections::listTokens()} method for access.
266
     *
267
     * @var array<int|string, int|string>
268
     */
269
    private static $listTokens = [
270
        \T_LIST              => \T_LIST,
271
        \T_OPEN_SHORT_ARRAY  => \T_OPEN_SHORT_ARRAY,
272
        \T_CLOSE_SHORT_ARRAY => \T_CLOSE_SHORT_ARRAY,
273
    ];
274

275
    /**
276
     * List of tokens which can end a namespace declaration statement.
277
     *
278
     * @since 1.0.0 Use the {@see Collections::namespaceDeclarationClosers()} method for access.
279
     *
280
     * @var array<int|string, int|string>
281
     */
282
    private static $namespaceDeclarationClosers = [
283
        \T_SEMICOLON          => \T_SEMICOLON,
284
        \T_OPEN_CURLY_BRACKET => \T_OPEN_CURLY_BRACKET,
285
        \T_CLOSE_TAG          => \T_CLOSE_TAG,
286
    ];
287

288
    /**
289
     * Tokens used for "names", be it namespace, OO, function or constant names.
290
     *
291
     * Includes the tokens introduced in PHP 8.0 for "Namespaced names as single token".
292
     *
293
     * Note: the PHP 8.0 namespaced name tokens are backfilled in PHPCS since PHPCS 3.5.7,
294
     * but are not used yet (the PHP 8.0 tokenization is "undone" in PHPCS).
295
     * As of PHPCS 4.0.0, these tokens _will_ be used and the PHP 8.0 tokenization is respected.
296
     *
297
     * @link https://wiki.php.net/rfc/namespaced_names_as_token PHP RFC on namespaced names as single token
298
     *
299
     * @since 1.0.0 Use the {@see Collections::nameTokens()} method for access.
300
     *
301
     * @var array<int|string, int|string>
302
     */
303
    private static $nameTokens = [
304
        \T_STRING               => \T_STRING,
305
        \T_NAME_QUALIFIED       => \T_NAME_QUALIFIED,
306
        \T_NAME_FULLY_QUALIFIED => \T_NAME_FULLY_QUALIFIED,
307
        \T_NAME_RELATIVE        => \T_NAME_RELATIVE,
308
    ];
309

310
    /**
311
     * Object operator tokens.
312
     *
313
     * @since 1.0.0 Use the {@see Collections::objectOperators()} method for access.
314
     *
315
     * @var array<int|string, int|string>
316
     */
317
    private static $objectOperators = [
318
        \T_DOUBLE_COLON             => \T_DOUBLE_COLON,
319
        \T_OBJECT_OPERATOR          => \T_OBJECT_OPERATOR,
320
        \T_NULLSAFE_OBJECT_OPERATOR => \T_NULLSAFE_OBJECT_OPERATOR,
321
    ];
322

323
    /**
324
     * OO structures which can use the "extends" keyword.
325
     *
326
     * @since 1.0.0 Use the {@see Collections::ooCanExtend()} method for access.
327
     *
328
     * @var array<int|string, int|string>
329
     */
330
    private static $ooCanExtend = [
331
        \T_CLASS      => \T_CLASS,
332
        \T_ANON_CLASS => \T_ANON_CLASS,
333
        \T_INTERFACE  => \T_INTERFACE,
334
    ];
335

336
    /**
337
     * OO structures which can use the "implements" keyword.
338
     *
339
     * @since 1.0.0 Use the {@see Collections::ooCanImplement()} method for access.
340
     *
341
     * @var array<int|string, int|string>
342
     */
343
    private static $ooCanImplement = [
344
        \T_CLASS      => \T_CLASS,
345
        \T_ANON_CLASS => \T_ANON_CLASS,
346
        \T_ENUM       => \T_ENUM,
347
    ];
348

349
    /**
350
     * OO scopes in which constants can be declared.
351
     *
352
     * Note: traits can only declare constants since PHP 8.2.
353
     *
354
     * @since 1.0.0 Use the {@see Collections::ooConstantScopes()} method for access.
355
     *
356
     * @var array<int|string, int|string>
357
     */
358
    private static $ooConstantScopes = [
359
        \T_CLASS      => \T_CLASS,
360
        \T_ANON_CLASS => \T_ANON_CLASS,
361
        \T_INTERFACE  => \T_INTERFACE,
362
        \T_ENUM       => \T_ENUM,
363
        \T_TRAIT      => \T_TRAIT,
364
    ];
365

366
    /**
367
     * Tokens types used for "forwarding" calls within OO structures.
368
     *
369
     * @link https://www.php.net/language.oop5.paamayim-nekudotayim PHP Manual on OO forwarding calls
370
     *
371
     * @since 1.0.0 Use the {@see Collections::ooHierarchyKeywords()} method for access.
372
     *
373
     * @var array<int|string, int|string>
374
     */
375
    private static $ooHierarchyKeywords = [
376
        \T_PARENT => \T_PARENT,
377
        \T_SELF   => \T_SELF,
378
        \T_STATIC => \T_STATIC,
379
    ];
380

381
    /**
382
     * OO scopes in which properties can be declared.
383
     *
384
     * Note: interfaces can not declare properties.
385
     *
386
     * @since 1.0.0 Use the {@see Collections::ooPropertyScopes()} method for access.
387
     *
388
     * @var array<int|string, int|string>
389
     */
390
    private static $ooPropertyScopes = [
391
        \T_CLASS      => \T_CLASS,
392
        \T_ANON_CLASS => \T_ANON_CLASS,
393
        \T_TRAIT      => \T_TRAIT,
394
    ];
395

396
    /**
397
     * Token types which can be encountered in a parameter type declaration.
398
     *
399
     * @since 1.0.0 Use the {@see Collections::parameterTypeTokens()} method for access.
400
     *
401
     * @var array<int|string, int|string>
402
     */
403
    private static $parameterTypeTokens = [
404
        \T_CALLABLE          => \T_CALLABLE,
405
        \T_SELF              => \T_SELF,
406
        \T_PARENT            => \T_PARENT,
407
        \T_FALSE             => \T_FALSE,
408
        \T_TRUE              => \T_TRUE,
409
        \T_NULL              => \T_NULL,
410
        \T_TYPE_UNION        => \T_TYPE_UNION,
411
        \T_TYPE_INTERSECTION => \T_TYPE_INTERSECTION,
412
    ];
413

414
    /**
415
     * Tokens which open PHP.
416
     *
417
     * @since 1.0.0 Use the {@see Collections::phpOpenTags()} method for access.
418
     *
419
     * @var array<int, int>
420
     */
421
    private static $phpOpenTags = [
422
        \T_OPEN_TAG           => \T_OPEN_TAG,
423
        \T_OPEN_TAG_WITH_ECHO => \T_OPEN_TAG_WITH_ECHO,
424
    ];
425

426
    /**
427
     * Modifier keywords which can be used for a property declaration.
428
     *
429
     * @since 1.0.0 Use the {@see Collections::propertyModifierKeywords()} method for access.
430
     *
431
     * @var array<int|string, int|string>
432
     */
433
    private static $propertyModifierKeywords = [
434
        \T_PUBLIC    => \T_PUBLIC,
435
        \T_PRIVATE   => \T_PRIVATE,
436
        \T_PROTECTED => \T_PROTECTED,
437
        \T_STATIC    => \T_STATIC,
438
        \T_VAR       => \T_VAR,
439
        \T_READONLY  => \T_READONLY,
440
    ];
441

442
    /**
443
     * Token types which can be encountered in a property type declaration.
444
     *
445
     * @since 1.0.0 Use the {@see Collections::propertyTypeTokens()} method for access.
446
     *
447
     * @var array<int|string, int|string>
448
     */
449
    private static $propertyTypeTokens = [
450
        \T_CALLABLE          => \T_CALLABLE,
451
        \T_SELF              => \T_SELF,
452
        \T_PARENT            => \T_PARENT,
453
        \T_FALSE             => \T_FALSE,
454
        \T_TRUE              => \T_TRUE,
455
        \T_NULL              => \T_NULL,
456
        \T_TYPE_UNION        => \T_TYPE_UNION,
457
        \T_TYPE_INTERSECTION => \T_TYPE_INTERSECTION,
458
    ];
459

460
    /**
461
     * Token types which can be encountered in a return type declaration.
462
     *
463
     * @since 1.0.0 Use the {@see Collections::returnTypeTokens()} method for access.
464
     *
465
     * @var array<int|string, int|string>
466
     */
467
    private static $returnTypeTokens = [
468
        \T_CALLABLE          => \T_CALLABLE,
469
        \T_FALSE             => \T_FALSE,
470
        \T_TRUE              => \T_TRUE,
471
        \T_NULL              => \T_NULL,
472
        \T_TYPE_UNION        => \T_TYPE_UNION,
473
        \T_TYPE_INTERSECTION => \T_TYPE_INTERSECTION,
474
    ];
475

476
    /**
477
     * Tokens which can open a short array or short list (PHPCS cross-version compatible).
478
     *
479
     * Should only be used selectively.
480
     * Depending on the PHPCS version, the token array will be expanded in the associated method.
481
     *
482
     * @since 1.0.0 Use the {@see Collections::shortArrayListOpenTokensBC()} method for access.
483
     *
484
     * @var array<int|string, int|string>
485
     */
486
    private static $shortArrayListOpenTokensBC = [
487
        \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
488
    ];
489

490
    /**
491
     * Tokens which are used for short arrays.
492
     *
493
     * @see \PHPCSUtils\Tokens\Collections::arrayTokens() Related method to retrieve all tokens used for arrays.
494
     *
495
     * @since 1.0.0 Use the {@see Collections::shortArrayTokens()} method for access.
496
     *
497
     * @var array<int|string, int|string>
498
     */
499
    private static $shortArrayTokens = [
500
        \T_OPEN_SHORT_ARRAY  => \T_OPEN_SHORT_ARRAY,
501
        \T_CLOSE_SHORT_ARRAY => \T_CLOSE_SHORT_ARRAY,
502
    ];
503

504
    /**
505
     * Tokens which are used for short lists.
506
     *
507
     * @see \PHPCSUtils\Tokens\Collections::listTokens() Related method to retrieve all tokens used for lists.
508
     *
509
     * @since 1.0.0 Use the {@see Collections::shortListTokens()} method for access.
510
     *
511
     * @var array<int|string, int|string>
512
     */
513
    private static $shortListTokens = [
514
        \T_OPEN_SHORT_ARRAY  => \T_OPEN_SHORT_ARRAY,
515
        \T_CLOSE_SHORT_ARRAY => \T_CLOSE_SHORT_ARRAY,
516
    ];
517

518
    /**
519
     * Tokens which can start a - potentially multi-line - text string.
520
     *
521
     * @since 1.0.0 Use the {@see Collections::textStringStartTokens()} method for access.
522
     *
523
     * @var array<int|string, int|string>
524
     */
525
    private static $textStringStartTokens = [
526
        \T_START_HEREDOC            => \T_START_HEREDOC,
527
        \T_START_NOWDOC             => \T_START_NOWDOC,
528
        \T_CONSTANT_ENCAPSED_STRING => \T_CONSTANT_ENCAPSED_STRING,
529
        \T_DOUBLE_QUOTED_STRING     => \T_DOUBLE_QUOTED_STRING,
530
    ];
531

532
    /**
533
     * Handle calls to (undeclared) methods for token arrays which don't need special handling.
534
     *
535
     * @since 1.0.0
536
     *
537
     * @param string       $name The name of the method which has been called.
538
     * @param array<mixed> $args Any arguments passed to the method.
539
     *                           Unused as none of the methods take arguments.
540
     *
541
     * @return array<int|string, int|string> Token array
542
     *
543
     * @throws \PHPCSUtils\Exceptions\InvalidTokenArray When an invalid token array is requested.
544
     */
545
    public static function __callStatic($name, $args)
96✔
546
    {
547
        if (isset(self::${$name})) {
96✔
548
            return self::${$name};
92✔
549
        }
550

551
        // Unknown token array requested.
552
        throw InvalidTokenArray::create($name);
4✔
553
    }
554

555
    /**
556
     * Throw a deprecation notice with a standardized deprecation message.
557
     *
558
     * @since 1.0.0
559
     *
560
     * @param string $method      The name of the method which is deprecated.
561
     * @param string $version     The version since which the method is deprecated.
562
     * @param string $replacement What to use instead.
563
     *
564
     * @return void
565
     */
UNCOV
566
    private static function triggerDeprecation($method, $version, $replacement)
×
567
    {
568
        \trigger_error(
×
569
            \sprintf(
×
570
                'The %1$s::%2$s() method is deprecated since PHPCSUtils %3$s.'
571
                . ' Use %4$s instead.',
×
572
                __CLASS__,
×
573
                $method,
×
574
                $version,
×
575
                $replacement
576
            ),
×
577
            \E_USER_DEPRECATED
578
        );
×
579
    }
580

581
    /**
582
     * Tokens which can open an array (PHPCS cross-version compatible).
583
     *
584
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` to allow for
585
     * handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
586
     * Should only be used selectively.
587
     *
588
     * @see \PHPCSUtils\Tokens\Collections::arrayTokensBC()      Related method to retrieve tokens used
589
     *                                                           for arrays (PHPCS cross-version).
590
     * @see \PHPCSUtils\Tokens\Collections::shortArrayTokensBC() Related method to retrieve only tokens used
591
     *                                                           for short arrays (PHPCS cross-version).
592
     *
593
     * @since 1.0.0
594
     *
595
     * @return array<int|string, int|string>
596
     */
597
    public static function arrayOpenTokensBC()
4✔
598
    {
599
        return self::$arrayOpenTokensBC;
4✔
600
    }
601

602
    /**
603
     * Tokens which are used to create arrays (PHPCS cross-version compatible).
604
     *
605
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` and `T_CLOSE_SQUARE_BRACKET`
606
     * to allow for handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
607
     * Should only be used selectively.
608
     *
609
     * @see \PHPCSUtils\Tokens\Collections::arrayOpenTokensBC()  Related method to retrieve only the "open" tokens
610
     *                                                           used for arrays (PHPCS cross-version compatible).
611
     * @see \PHPCSUtils\Tokens\Collections::shortArrayTokensBC() Related method to retrieve only tokens used
612
     *                                                           for short arrays (PHPCS cross-version compatible).
613
     *
614
     * @since 1.0.0
615
     *
616
     * @return array<int|string, int|string>
617
     */
618
    public static function arrayTokensBC()
4✔
619
    {
620
        return self::$arrayTokens;
4✔
621
    }
622

623
    /**
624
     * Tokens which can represent function calls and function-call-like language constructs.
625
     *
626
     * @see \PHPCSUtils\Tokens\Collections::parameterPassingTokens() Related method.
627
     *
628
     * @since 1.0.0
629
     *
630
     * @return array<int|string, int|string>
631
     */
632
    public static function functionCallTokens()
4✔
633
    {
634
        // Function calls and class instantiation.
635
        $tokens              = self::$nameTokens;
4✔
636
        $tokens[\T_VARIABLE] = \T_VARIABLE;
4✔
637

638
        // Class instantiation only.
639
        $tokens[\T_ANON_CLASS] = \T_ANON_CLASS;
4✔
640
        $tokens               += self::$ooHierarchyKeywords;
4✔
641

642
        return $tokens;
4✔
643
    }
644

645
    /**
646
     * Tokens which can open a list construct (PHPCS cross-version compatible).
647
     *
648
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` to allow for
649
     * handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
650
     * Should only be used selectively.
651
     *
652
     * @see \PHPCSUtils\Tokens\Collections::listTokensBC()      Related method to retrieve tokens used
653
     *                                                          for lists (PHPCS cross-version).
654
     * @see \PHPCSUtils\Tokens\Collections::shortListTokensBC() Related method to retrieve only tokens used
655
     *                                                          for short lists (PHPCS cross-version).
656
     *
657
     * @since 1.0.0
658
     *
659
     * @return array<int|string, int|string>
660
     */
661
    public static function listOpenTokensBC()
4✔
662
    {
663
        return self::$listOpenTokensBC;
4✔
664
    }
665

666
    /**
667
     * Tokens which are used to create lists (PHPCS cross-version compatible).
668
     *
669
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` and `T_CLOSE_SQUARE_BRACKET`
670
     * to allow for handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
671
     * Should only be used selectively.
672
     *
673
     * @see \PHPCSUtils\Tokens\Collections::shortListTokensBC() Related method to retrieve only tokens used
674
     *                                                          for short lists (cross-version).
675
     *
676
     * @since 1.0.0
677
     *
678
     * @return array<int|string, int|string>
679
     */
680
    public static function listTokensBC()
4✔
681
    {
682
        return self::$listTokens;
4✔
683
    }
684

685
    /**
686
     * Tokens types which can be encountered in a fully, partially or unqualified name.
687
     *
688
     * Example:
689
     * ```php
690
     * echo namespace\Sub\ClassName::method();
691
     * ```
692
     *
693
     * @since 1.0.0
694
     *
695
     * @return array<int|string, int|string>
696
     */
697
    public static function namespacedNameTokens()
4✔
698
    {
699
        $tokens = [
2✔
700
            \T_NS_SEPARATOR => \T_NS_SEPARATOR,
4✔
701
            \T_NAMESPACE    => \T_NAMESPACE,
4✔
702
        ];
4✔
703

704
        $tokens += self::$nameTokens;
4✔
705

706
        return $tokens;
4✔
707
    }
708

709
    /**
710
     * Tokens which can be passed to the methods in the PassedParameter class.
711
     *
712
     * @see \PHPCSUtils\Utils\PassedParameters
713
     *
714
     * @since 1.0.0
715
     *
716
     * @return array<int|string, int|string>
717
     */
718
    public static function parameterPassingTokens()
4✔
719
    {
720
        // Function call and class instantiation tokens.
721
        $tokens = self::functionCallTokens();
4✔
722

723
        // Function-look-a-like language constructs which can take multiple "parameters".
724
        $tokens[\T_ISSET] = \T_ISSET;
4✔
725
        $tokens[\T_UNSET] = \T_UNSET;
4✔
726

727
        // Array tokens.
728
        $tokens += self::arrayOpenTokensBC();
4✔
729

730
        return $tokens;
4✔
731
    }
732

733
    /**
734
     * Token types which can be encountered in a parameter type declaration.
735
     *
736
     * @since 1.0.0
737
     *
738
     * @return array<int|string, int|string>
739
     */
740
    public static function parameterTypeTokens()
4✔
741
    {
742
        $tokens  = self::$parameterTypeTokens;
4✔
743
        $tokens += self::namespacedNameTokens();
4✔
744

745
        return $tokens;
4✔
746
    }
747

748
    /**
749
     * Token types which can be encountered in a property type declaration.
750
     *
751
     * @since 1.0.0
752
     *
753
     * @return array<int|string, int|string>
754
     */
755
    public static function propertyTypeTokens()
4✔
756
    {
757
        $tokens  = self::$propertyTypeTokens;
4✔
758
        $tokens += self::namespacedNameTokens();
4✔
759

760
        return $tokens;
4✔
761
    }
762

763
    /**
764
     * Token types which can be encountered in a return type declaration.
765
     *
766
     * @since 1.0.0
767
     *
768
     * @return array<int|string, int|string>
769
     */
770
    public static function returnTypeTokens()
4✔
771
    {
772
        $tokens  = self::$returnTypeTokens;
4✔
773
        $tokens += self::$ooHierarchyKeywords;
4✔
774
        $tokens += self::namespacedNameTokens();
4✔
775

776
        return $tokens;
4✔
777
    }
778

779
    /**
780
     * Tokens which can open a short array or short list (PHPCS cross-version compatible).
781
     *
782
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` to allow for
783
     * handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
784
     * Should only be used selectively.
785
     *
786
     * @since 1.0.0
787
     *
788
     * @return array<int|string, int|string>
789
     */
790
    public static function shortArrayListOpenTokensBC()
4✔
791
    {
792
        return self::$shortArrayListOpenTokensBC;
4✔
793
    }
794

795
    /**
796
     * Tokens which are used for short arrays (PHPCS cross-version compatible).
797
     *
798
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` and `T_CLOSE_SQUARE_BRACKET`
799
     * to allow for handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
800
     * Should only be used selectively.
801
     *
802
     * @see \PHPCSUtils\Tokens\Collections::arrayTokensBC() Related method to retrieve all tokens used for arrays
803
     *                                                      (cross-version).
804
     *
805
     * @since 1.0.0
806
     *
807
     * @return array<int|string, int|string>
808
     */
809
    public static function shortArrayTokensBC()
4✔
810
    {
811
        return self::$shortArrayTokens;
4✔
812
    }
813

814
    /**
815
     * Tokens which are used for short lists (PHPCS cross-version compatible).
816
     *
817
     * For those PHPCS versions which need it, includes `T_OPEN_SQUARE_BRACKET` and `T_CLOSE_SQUARE_BRACKET`
818
     * to allow for handling tokenizer issues related to the retokenization to `T_OPEN_SHORT_ARRAY`.
819
     * Should only be used selectively.
820
     *
821
     * @see \PHPCSUtils\Tokens\Collections::listTokensBC() Related method to retrieve all tokens used for lists
822
     *                                                     (cross-version).
823
     *
824
     * @since 1.0.0
825
     *
826
     * @return array<int|string, int|string>
827
     */
828
    public static function shortListTokensBC()
4✔
829
    {
830
        return self::$shortListTokens;
4✔
831
    }
832
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc