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

PHPCSStandards / PHPCSUtils / 17871546333

19 Sep 2025 10:53PM UTC coverage: 99.781%. Remained the same
17871546333

push

github

web-flow
Merge pull request #718 from PHPCSStandards/dependabot/github_actions/action-runners-7dd87c42ac

GH Actions: Bump shivammathur/setup-php from 2.35.4 to 2.35.5 in the action-runners group

3641 of 3649 relevant lines covered (99.78%)

294.86 hits per line

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

100.0
/PHPCSUtils/BackCompat/BCTokens.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\BackCompat;
12

13
use PHP_CodeSniffer\Util\Tokens;
14
use PHPCSUtils\Exceptions\InvalidTokenArray;
15
use PHPCSUtils\Tokens\Collections;
16

17
/**
18
 * Token arrays related utility methods.
19
 *
20
 * PHPCS provides a number of static token arrays in the {@see \PHP_CodeSniffer\Util\Tokens}
21
 * class.
22
 * Some of these token arrays will not be available in older PHPCS versions.
23
 * Some will not contain the same set of tokens across PHPCS versions.
24
 *
25
 * This class is a compatibility layer to allow for retrieving these token arrays
26
 * with a consistent token content across PHPCS versions.
27
 * The one caveat is that the token constants do need to be available.
28
 *
29
 * Recommended usage:
30
 * Only use the methods in this class when needed. I.e. when your sniff unit tests indicate
31
 * a PHPCS cross-version compatibility issue related to inconsistent token arrays.
32
 *
33
 * All PHPCS token arrays are supported, though only a limited number of them are different
34
 * across PHPCS versions.
35
 *
36
 * The names of the PHPCS native token arrays translate one-on-one to the methods in this class:
37
 * - `PHP_CodeSniffer\Util\Tokens::$emptyTokens` => `PHPCSUtils\BackCompat\BCTokens::emptyTokens()`
38
 * - `PHP_CodeSniffer\Util\Tokens::$operators`   => `PHPCSUtils\BackCompat\BCTokens::operators()`
39
 * - ... etc
40
 *
41
 * The order of the tokens in the arrays may differ between the PHPCS native token arrays and
42
 * the token arrays returned by this class.
43
 *
44
 * @since 1.0.0
45
 *
46
 * @method static array<int|string, int|string> arithmeticTokens()         Tokens that represent arithmetic operators.
47
 * @method static array<int|string, int|string> booleanOperators()         Tokens that perform boolean operations.
48
 * @method static array<int|string, int|string> bracketTokens()            Tokens that represent brackets and parenthesis.
49
 * @method static array<int|string, int|string> castTokens()               Tokens that represent type casting.
50
 * @method static array<int|string, int|string> commentTokens()            Tokens that are comments.
51
 * @method static array<int|string, int|string> comparisonTokens()         Tokens that represent comparison operator.
52
 * @method static array<int|string, int|string> contextSensitiveKeywords() Tokens representing context sensitive keywords
53
 *                                                                         in PHP.
54
 * @method static array<int|string, int|string> emptyTokens()              Tokens that don't represent code.
55
 * @method static array<int|string, int|string> equalityTokens()           Tokens that represent equality comparisons.
56
 * @method static array<int|string, int|string> heredocTokens()            Tokens that make up a heredoc string.
57
 * @method static array<int|string, int|string> includeTokens()            Tokens that include files.
58
 * @method static array<int|string, int|string> magicConstants()           Tokens representing PHP magic constants.
59
 * @method static array<int|string, int|string> methodPrefixes()           Tokens that can prefix a method name.
60
 * @method static array<int|string, int|string> ooScopeTokens()            Tokens that open class and object scopes.
61
 * @method static array<int|string, int|string> operators()                Tokens that perform operations.
62
 * @method static array<int|string, int|string> phpcsCommentTokens()       Tokens that are comments containing PHPCS
63
 *                                                                         instructions.
64
 * @method static array<int|string, int|string> scopeModifiers()           Tokens that represent scope modifiers.
65
 * @method static array<int|string, int|string> stringTokens()             Tokens that represent strings.
66
 *                                                                         Note that `T_STRING`s are NOT represented in this
67
 *                                                                         list as this list is about _text_ strings.
68
 * @method static array<int|string, int|string> textStringTokens()         Tokens that represent text strings.
69
 */
70
final class BCTokens
71
{
72

73
    /**
74
     * Handle calls to (undeclared) methods for token arrays which haven't received any
75
     * changes since PHPCS 3.13.3.
76
     *
77
     * @since 1.0.0
78
     *
79
     * @param string       $name The name of the method which has been called.
80
     * @param array<mixed> $args Any arguments passed to the method.
81
     *                           Unused as none of the methods take arguments.
82
     *
83
     * @return array<int|string, int|string> Token array
84
     *
85
     * @throws \PHPCSUtils\Exceptions\InvalidTokenArray When an invalid token array is requested.
86
     */
87
    public static function __callStatic($name, $args)
160✔
88
    {
89
        if (isset(Tokens::${$name})) {
160✔
90
            return Tokens::${$name};
152✔
91
        }
92

93
        // Unknown token array requested.
94
        throw InvalidTokenArray::create($name);
8✔
95
    }
96

97
    /**
98
     * Tokens that represent assignments.
99
     *
100
     * Retrieve the PHPCS assignments tokens array in a cross-version compatible manner.
101
     *
102
     * Changelog for the PHPCS native array:
103
     * - PHPCS 4.0.0: The JS specific `T_ZSR_EQUAL` token is no longer available and has been removed from the array.
104
     *
105
     * @see \PHP_CodeSniffer\Util\Tokens::$assignmentTokens Original array.
106
     *
107
     * @since 1.0.0
108
     *
109
     * @return array<int|string, int|string> Token array.
110
     */
111
    public static function assignmentTokens()
8✔
112
    {
113
        $tokens = Tokens::$assignmentTokens;
8✔
114

115
        if (\defined('T_ZSR_EQUAL') && isset($tokens[\T_ZSR_EQUAL])) {
8✔
116
            unset($tokens[\T_ZSR_EQUAL]);
4✔
117
        }
2✔
118

119
        return $tokens;
8✔
120
    }
121

122
    /**
123
     * Tokens that open code blocks.
124
     *
125
     * Retrieve the PHPCS block opener tokens array in a cross-version compatible manner.
126
     *
127
     * Changelog for the PHPCS native array:
128
     * - PHPCS 4.0.0: The JS specific `T_OBJECT` token is no longer available and has been removed from the array.
129
     *
130
     * @see \PHP_CodeSniffer\Util\Tokens::$blockOpeners Original array.
131
     *
132
     * @since 1.0.0
133
     *
134
     * @return array<int|string, int|string> Token array.
135
     */
136
    public static function blockOpeners()
8✔
137
    {
138
        $tokens = Tokens::$blockOpeners;
8✔
139

140
        if (\defined('T_OBJECT') && isset($tokens[\T_OBJECT])) {
8✔
141
            unset($tokens[\T_OBJECT]);
4✔
142
        }
2✔
143

144
        return $tokens;
8✔
145
    }
146

147
    /**
148
     * Tokens that represent the names of called functions.
149
     *
150
     * Retrieve the PHPCS function name tokens array in a cross-version compatible manner.
151
     *
152
     * Changelog for the PHPCS native array:
153
     * - Introduced in PHPCS 2.3.3.
154
     * - PHPCS 4.0.0: `T_NAME_QUALIFIED`, `T_NAME_FULLY_QUALIFIED`, `T_NAME_RELATIVE` and `T_ANON_CLASS` added to the array.
155
     *
156
     * @see \PHP_CodeSniffer\Util\Tokens::$functionNameTokens Original array.
157
     *
158
     * @since 1.0.0
159
     *
160
     * @return array<int|string, int|string> Token array.
161
     */
162
    public static function functionNameTokens()
8✔
163
    {
164
        $tokens                = Tokens::$functionNameTokens;
8✔
165
        $tokens               += Collections::nameTokens();
8✔
166
        $tokens[\T_ANON_CLASS] = \T_ANON_CLASS;
8✔
167

168
        return $tokens;
8✔
169
    }
170

171
    /**
172
     * Tokens used for "names", be it namespace, OO, function or constant names.
173
     *
174
     * Retrieve the PHPCS name tokens array in a cross-version compatible manner.
175
     *
176
     * Changelog for the PHPCS native array:
177
     * - Introduced in PHPCS 4.0.0.
178
     *
179
     * @see \PHP_CodeSniffer\Util\Tokens::NAME_TOKENS Original array.
180
     *
181
     * @since 1.1.0
182
     *
183
     * @return array<int|string, int|string> Token array.
184
     */
185
    public static function nameTokens()
8✔
186
    {
187
        return Collections::nameTokens();
8✔
188
    }
189

190
    /**
191
     * Token types that open parentheses.
192
     *
193
     * Retrieve the PHPCS parenthesis openers tokens array in a cross-version compatible manner.
194
     *
195
     * Changelog for the PHPCS native array:
196
     * - Introduced in PHPCS 0.0.5.
197
     * - PHPCS 4.0.0: `T_USE` (for closures), `T_ISSET`, `T_UNSET`, `T_EMPTY`, `T_EVAL` and `T_EXIT` added to the array.
198
     *
199
     * **Important**: While `T_USE`, `T_ISSET`, `T_UNSET`, `T_EMPTY`, `T_EVAL` and `T_EXIT` will be included
200
     * in the return value for this method, the associated parentheses will not have the `'parenthesis_owner'` index
201
     * set until PHPCS 4.0.0.
202
     * Use the {@see \PHPCSUtils\Utils\Parentheses::getOwner()} or {@see \PHPCSUtils\Utils\Parentheses::hasOwner()} methods
203
     * if you need to check for whether any of these tokens are a parentheses owner.
204
     *
205
     * @see \PHP_CodeSniffer\Util\Tokens::$parenthesisOpeners Original array.
206
     * @see \PHPCSUtils\Utils\Parentheses                     Class holding utility methods for
207
     *                                                        working with the `'parenthesis_...'`
208
     *                                                        index keys in a token array.
209
     *
210
     * @since 1.0.0
211
     *
212
     * @return array<int|string, int|string> Token array.
213
     */
214
    public static function parenthesisOpeners()
8✔
215
    {
216
        $tokens           = Tokens::$parenthesisOpeners;
8✔
217
        $tokens[\T_USE]   = \T_USE;
8✔
218
        $tokens[\T_ISSET] = \T_ISSET;
8✔
219
        $tokens[\T_UNSET] = \T_UNSET;
8✔
220
        $tokens[\T_EMPTY] = \T_EMPTY;
8✔
221
        $tokens[\T_EVAL]  = \T_EVAL;
8✔
222
        $tokens[\T_EXIT]  = \T_EXIT;
8✔
223

224
        return $tokens;
8✔
225
    }
226

227
    /**
228
     * Tokens that are allowed to open scopes.
229
     *
230
     * Retrieve the PHPCS scope opener tokens array in a cross-version compatible manner.
231
     *
232
     * Changelog for the PHPCS native array:
233
     * - PHPCS 4.0.0: The JS specific `T_PROPERTY` and `T_OBJECT` tokens are no longer available
234
     *   and have been removed from the array.
235
     *
236
     * @see \PHP_CodeSniffer\Util\Tokens::$scopeOpeners Original array.
237
     *
238
     * @since 1.0.0
239
     *
240
     * @return array<int|string, int|string> Token array.
241
     */
242
    public static function scopeOpeners()
8✔
243
    {
244
        $tokens = Tokens::$scopeOpeners;
8✔
245

246
        if (\defined('T_PROPERTY') && isset($tokens[\T_PROPERTY])) {
8✔
247
            unset($tokens[\T_PROPERTY]);
4✔
248
        }
2✔
249

250
        if (\defined('T_OBJECT') && isset($tokens[\T_OBJECT])) {
8✔
251
            unset($tokens[\T_OBJECT]);
4✔
252
        }
2✔
253

254
        return $tokens;
8✔
255
    }
256
}
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