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

keradus / PHP-CS-Fixer / 15295226534

28 May 2025 08:23AM UTC coverage: 94.849% (-0.01%) from 94.859%
15295226534

push

github

keradus
DX: introduce `FCT` class for tokens not present in the lowest supported PHP version (#8706)

Co-authored-by: Dariusz Rumiński <dariusz.ruminski@gmail.com>

186 of 192 new or added lines in 52 files covered. (96.88%)

307 existing lines in 29 files now uncovered.

28099 of 29625 relevant lines covered (94.85%)

45.33 hits per line

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

92.95
/src/Tokenizer/Token.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Tokenizer;
16

17
use PhpCsFixer\Utils;
18

19
/**
20
 * Representation of single token.
21
 * As a token prototype you should understand a single element generated by token_get_all.
22
 *
23
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
24
 *
25
 * @readonly
26
 */
27
final class Token
28
{
29
    /**
30
     * Content of token prototype.
31
     */
32
    private string $content;
33

34
    /**
35
     * ID of token prototype, if available.
36
     */
37
    private ?int $id;
38

39
    /**
40
     * If token prototype is an array.
41
     */
42
    private bool $isArray;
43

44
    /**
45
     * @param array{int, string}|string $token token prototype
46
     */
47
    public function __construct($token)
48
    {
49
        if (\is_array($token)) {
41✔
50
            if (!\is_int($token[0])) {
33✔
51
                throw new \InvalidArgumentException(\sprintf(
3✔
52
                    'Id must be an int, got "%s".',
3✔
53
                    get_debug_type($token[0])
3✔
54
                ));
3✔
55
            }
56

57
            if (!\is_string($token[1])) {
30✔
58
                throw new \InvalidArgumentException(\sprintf(
3✔
59
                    'Content must be a string, got "%s".',
3✔
60
                    get_debug_type($token[1])
3✔
61
                ));
3✔
62
            }
63

64
            if ('' === $token[1]) {
27✔
65
                throw new \InvalidArgumentException('Cannot set empty content for id-based Token.');
1✔
66
            }
67

68
            $this->isArray = true;
26✔
69
            $this->id = $token[0];
26✔
70
            $this->content = $token[1];
26✔
71
        } elseif (\is_string($token)) {
12✔
72
            $this->isArray = false;
6✔
73
            $this->id = null;
6✔
74
            $this->content = $token;
6✔
75
        } else {
76
            throw new \InvalidArgumentException(\sprintf('Cannot recognize input value as valid Token prototype, got "%s".', get_debug_type($token)));
6✔
77
        }
78
    }
79

80
    /**
81
     * @return list<int>
82
     */
83
    public static function getCastTokenKinds(): array
84
    {
85
        static $castTokens = [T_ARRAY_CAST, T_BOOL_CAST, T_DOUBLE_CAST, T_INT_CAST, T_OBJECT_CAST, T_STRING_CAST, T_UNSET_CAST];
9✔
86

87
        return $castTokens;
9✔
88
    }
89

90
    /**
91
     * Get classy tokens kinds: T_ENUM, T_CLASS, T_INTERFACE and T_TRAIT.
92
     *
93
     * @return list<int>
94
     */
95
    public static function getClassyTokenKinds(): array
96
    {
97
        static $classTokens = [T_CLASS, T_TRAIT, T_INTERFACE, FCT::T_ENUM];
6✔
98

99
        return $classTokens;
6✔
100
    }
101

102
    /**
103
     * Get object operator tokens kinds: T_OBJECT_OPERATOR and (if available) T_NULLSAFE_OBJECT_OPERATOR.
104
     *
105
     * @return list<int>
106
     */
107
    public static function getObjectOperatorKinds(): array
108
    {
109
        static $objectOperators = [T_OBJECT_OPERATOR, FCT::T_NULLSAFE_OBJECT_OPERATOR];
6✔
110

111
        return $objectOperators;
6✔
112
    }
113

114
    /**
115
     * Check if token is equals to given one.
116
     *
117
     * If tokens are arrays, then only keys defined in parameter token are checked.
118
     *
119
     * @param array{0: int, 1?: string}|string|Token $other         token or it's prototype
120
     * @param bool                                   $caseSensitive perform a case sensitive comparison
121
     */
122
    public function equals($other, bool $caseSensitive = true): bool
123
    {
124
        if ('&' === $other) {
39✔
125
            return '&' === $this->content && (null === $this->id || $this->isGivenKind([FCT::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, FCT::T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG]));
3✔
126
        }
127
        if (null === $this->id && '&' === $this->content) {
36✔
128
            return $other instanceof self && '&' === $other->content && (null === $other->id || $other->isGivenKind([FCT::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, FCT::T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG]));
2✔
129
        }
130

131
        if ($other instanceof self) {
34✔
132
            // Inlined getPrototype() on this very hot path.
133
            // We access the private properties of $other directly to save function call overhead.
134
            // This is only possible because $other is of the same class as `self`.
135
            if (!$other->isArray) {
11✔
136
                $otherPrototype = $other->content;
6✔
137
            } else {
138
                $otherPrototype = [
10✔
139
                    $other->id,
10✔
140
                    $other->content,
10✔
141
                ];
10✔
142
            }
143
        } else {
144
            $otherPrototype = $other;
27✔
145
        }
146

147
        if ($this->isArray !== \is_array($otherPrototype)) {
34✔
148
            return false;
8✔
149
        }
150

151
        if (!$this->isArray) {
31✔
152
            return $this->content === $otherPrototype;
4✔
153
        }
154

155
        if ($this->id !== $otherPrototype[0]) {
27✔
156
            return false;
12✔
157
        }
158

159
        if (isset($otherPrototype[1])) {
20✔
160
            if ($caseSensitive) {
17✔
161
                if ($this->content !== $otherPrototype[1]) {
11✔
162
                    return false;
5✔
163
                }
164
            } elseif (0 !== strcasecmp($this->content, $otherPrototype[1])) {
6✔
165
                return false;
2✔
166
            }
167
        }
168

169
        // detect unknown keys
170
        unset($otherPrototype[0], $otherPrototype[1]);
15✔
171

172
        return [] === $otherPrototype;
15✔
173
    }
174

175
    /**
176
     * Check if token is equals to one of given.
177
     *
178
     * @param list<array{0: int, 1?: string}|string|Token> $others        array of tokens or token prototypes
179
     * @param bool                                         $caseSensitive perform a case sensitive comparison
180
     */
181
    public function equalsAny(array $others, bool $caseSensitive = true): bool
182
    {
183
        foreach ($others as $other) {
9✔
184
            if ($this->equals($other, $caseSensitive)) {
8✔
185
                return true;
4✔
186
            }
187
        }
188

189
        return false;
6✔
190
    }
191

192
    /**
193
     * A helper method used to find out whether a certain input token has to be case-sensitively matched.
194
     *
195
     * @param array<int, bool>|bool $caseSensitive global case sensitiveness or an array of booleans, whose keys should match
196
     *                                             the ones used in $sequence. If any is missing, the default case-sensitive
197
     *                                             comparison is used
198
     * @param int                   $key           the key of the token that has to be looked up
199
     *
200
     * @deprecated
201
     */
202
    public static function isKeyCaseSensitive($caseSensitive, int $key): bool
203
    {
204
        Utils::triggerDeprecation(new \InvalidArgumentException(\sprintf(
12✔
205
            'Method "%s" is deprecated and will be removed in the next major version.',
12✔
206
            __METHOD__
12✔
207
        )));
12✔
208

209
        if (\is_array($caseSensitive)) {
12✔
210
            return $caseSensitive[$key] ?? true;
9✔
211
        }
212

213
        return $caseSensitive;
3✔
214
    }
215

216
    /**
217
     * @return array{int, non-empty-string}|string
218
     */
219
    public function getPrototype()
220
    {
221
        if (!$this->isArray) {
1✔
222
            return $this->content;
1✔
223
        }
224

225
        \assert('' !== $this->content);
1✔
226

227
        return [
1✔
228
            $this->id,
1✔
229
            $this->content,
1✔
230
        ];
1✔
231
    }
232

233
    /**
234
     * Get token's content.
235
     *
236
     * It shall be used only for getting the content of token, not for checking it against excepted value.
237
     */
238
    public function getContent(): string
239
    {
240
        return $this->content;
2✔
241
    }
242

243
    /**
244
     * Get token's id.
245
     *
246
     * It shall be used only for getting the internal id of token, not for checking it against excepted value.
247
     */
248
    public function getId(): ?int
249
    {
250
        return $this->id;
2✔
251
    }
252

253
    /**
254
     * Get token's name.
255
     *
256
     * It shall be used only for getting the name of token, not for checking it against excepted value.
257
     *
258
     * @return null|non-empty-string token name
259
     */
260
    public function getName(): ?string
261
    {
262
        if (null === $this->id) {
6✔
263
            return null;
4✔
264
        }
265

266
        return self::getNameForId($this->id);
2✔
267
    }
268

269
    /**
270
     * Get token's name.
271
     *
272
     * It shall be used only for getting the name of token, not for checking it against excepted value.
273
     *
274
     * @return null|non-empty-string token name
275
     */
276
    public static function getNameForId(int $id): ?string
277
    {
278
        if (CT::has($id)) {
5✔
279
            return CT::getName($id);
1✔
280
        }
281

282
        $name = token_name($id);
4✔
283

284
        return 'UNKNOWN' === $name ? null : $name;
4✔
285
    }
286

287
    /**
288
     * Generate array containing all keywords that exists in PHP version in use.
289
     *
290
     * @return list<int>
291
     */
292
    public static function getKeywords(): array
293
    {
294
        static $keywords = null;
1✔
295

296
        if (null === $keywords) {
1✔
297
            $keywords = self::getTokenKindsForNames(['T_ABSTRACT', 'T_ARRAY', 'T_AS', 'T_BREAK', 'T_CALLABLE', 'T_CASE',
1✔
298
                'T_CATCH', 'T_CLASS', 'T_CLONE', 'T_CONST', 'T_CONTINUE', 'T_DECLARE', 'T_DEFAULT', 'T_DO',
1✔
299
                'T_ECHO', 'T_ELSE', 'T_ELSEIF', 'T_EMPTY', 'T_ENDDECLARE', 'T_ENDFOR', 'T_ENDFOREACH',
1✔
300
                'T_ENDIF', 'T_ENDSWITCH', 'T_ENDWHILE', 'T_EVAL', 'T_EXIT', 'T_EXTENDS', 'T_FINAL',
1✔
301
                'T_FINALLY', 'T_FN', 'T_FOR', 'T_FOREACH', 'T_FUNCTION', 'T_GLOBAL', 'T_GOTO', 'T_HALT_COMPILER',
1✔
302
                'T_IF', 'T_IMPLEMENTS', 'T_INCLUDE', 'T_INCLUDE_ONCE', 'T_INSTANCEOF', 'T_INSTEADOF',
1✔
303
                'T_INTERFACE', 'T_ISSET', 'T_LIST', 'T_LOGICAL_AND', 'T_LOGICAL_OR', 'T_LOGICAL_XOR',
1✔
304
                'T_NAMESPACE', 'T_NEW', 'T_PRINT', 'T_PRIVATE', 'T_PROTECTED', 'T_PUBLIC', 'T_REQUIRE',
1✔
305
                'T_REQUIRE_ONCE', 'T_RETURN', 'T_STATIC', 'T_SWITCH', 'T_THROW', 'T_TRAIT', 'T_TRY',
1✔
306
                'T_UNSET', 'T_USE', 'T_VAR', 'T_WHILE', 'T_YIELD', 'T_YIELD_FROM',
1✔
307
            ]) + [
1✔
308
                CT::T_ARRAY_TYPEHINT => CT::T_ARRAY_TYPEHINT,
1✔
309
                CT::T_CLASS_CONSTANT => CT::T_CLASS_CONSTANT,
1✔
310
                CT::T_CONST_IMPORT => CT::T_CONST_IMPORT,
1✔
311
                CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
1✔
312
                CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
1✔
313
                CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
1✔
314
                CT::T_FUNCTION_IMPORT => CT::T_FUNCTION_IMPORT,
1✔
315
                CT::T_NAMESPACE_OPERATOR => CT::T_NAMESPACE_OPERATOR,
1✔
316
                CT::T_USE_LAMBDA => CT::T_USE_LAMBDA,
1✔
317
                CT::T_USE_TRAIT => CT::T_USE_TRAIT,
1✔
318
                FCT::T_ENUM => FCT::T_ENUM,
1✔
319
                FCT::T_MATCH => FCT::T_MATCH,
1✔
320
                FCT::T_PRIVATE_SET => FCT::T_PRIVATE_SET,
1✔
321
                FCT::T_PROTECTED_SET => FCT::T_PROTECTED_SET,
1✔
322
                FCT::T_PUBLIC_SET => FCT::T_PUBLIC_SET,
1✔
323
                FCT::T_READONLY => FCT::T_READONLY,
1✔
324
            ];
1✔
325
        }
326

327
        return $keywords;
1✔
328
    }
329

330
    /**
331
     * Generate array containing all predefined constants that exists in PHP version in use.
332
     *
333
     * @see https://php.net/manual/en/language.constants.predefined.php
334
     *
335
     * @return array<int, int>
336
     */
337
    public static function getMagicConstants(): array
338
    {
339
        static $magicConstants = null;
11✔
340

341
        if (null === $magicConstants) {
11✔
342
            $magicConstants = self::getTokenKindsForNames(['T_CLASS_C', 'T_DIR', 'T_FILE', 'T_FUNC_C', 'T_LINE', 'T_METHOD_C', 'T_NS_C', 'T_TRAIT_C']);
1✔
343
        }
344

345
        return $magicConstants;
11✔
346
    }
347

348
    /**
349
     * Check if token prototype is an array.
350
     *
351
     * @return bool is array
352
     *
353
     * @phpstan-assert-if-true !=null $this->getId()
354
     * @phpstan-assert-if-true !='' $this->getContent()
355
     */
356
    public function isArray(): bool
357
    {
358
        return $this->isArray;
3✔
359
    }
360

361
    /**
362
     * Check if token is one of type cast tokens.
363
     *
364
     * @phpstan-assert-if-true !='' $this->getContent()
365
     */
366
    public function isCast(): bool
367
    {
368
        return $this->isGivenKind(self::getCastTokenKinds());
9✔
369
    }
370

371
    /**
372
     * Check if token is one of classy tokens: T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM.
373
     *
374
     * @phpstan-assert-if-true !='' $this->getContent()
375
     */
376
    public function isClassy(): bool
377
    {
378
        return $this->isGivenKind(self::getClassyTokenKinds());
6✔
379
    }
380

381
    /**
382
     * Check if token is one of comment tokens: T_COMMENT or T_DOC_COMMENT.
383
     *
384
     * @phpstan-assert-if-true !='' $this->getContent()
385
     */
386
    public function isComment(): bool
387
    {
388
        static $commentTokens = [T_COMMENT, T_DOC_COMMENT];
5✔
389

390
        return $this->isGivenKind($commentTokens);
5✔
391
    }
392

393
    /**
394
     * Check if token is one of object operator tokens: T_OBJECT_OPERATOR or T_NULLSAFE_OBJECT_OPERATOR.
395
     *
396
     * @phpstan-assert-if-true !='' $this->getContent()
397
     */
398
    public function isObjectOperator(): bool
399
    {
400
        return $this->isGivenKind(self::getObjectOperatorKinds());
6✔
401
    }
402

403
    /**
404
     * Check if token is one of given kind.
405
     *
406
     * @param int|list<int> $possibleKind kind or array of kinds
407
     *
408
     * @phpstan-assert-if-true !='' $this->getContent()
409
     */
410
    public function isGivenKind($possibleKind): bool
411
    {
412
        return $this->isArray && (\is_array($possibleKind) ? \in_array($this->id, $possibleKind, true) : $this->id === $possibleKind);
37✔
413
    }
414

415
    /**
416
     * Check if token is a keyword.
417
     *
418
     * @phpstan-assert-if-true !='' $this->getContent()
419
     */
420
    public function isKeyword(): bool
421
    {
422
        $keywords = self::getKeywords();
1✔
423

424
        return $this->isArray && isset($keywords[$this->id]);
1✔
425
    }
426

427
    /**
428
     * Check if token is a native PHP constant: true, false or null.
429
     *
430
     * @phpstan-assert-if-true !='' $this->getContent()
431
     */
432
    public function isNativeConstant(): bool
433
    {
434
        static $nativeConstantStrings = ['true', 'false', 'null'];
7✔
435

436
        return $this->isArray && \in_array(strtolower($this->content), $nativeConstantStrings, true);
7✔
437
    }
438

439
    /**
440
     * Returns if the token is of a Magic constants type.
441
     *
442
     * @phpstan-assert-if-true !='' $this->getContent()
443
     *
444
     * @see https://php.net/manual/en/language.constants.predefined.php
445
     */
446
    public function isMagicConstant(): bool
447
    {
448
        $magicConstants = self::getMagicConstants();
11✔
449

450
        return $this->isArray && isset($magicConstants[$this->id]);
11✔
451
    }
452

453
    /**
454
     * Check if token is whitespace.
455
     *
456
     * @param null|string $whitespaces whitespace characters, default is " \t\n\r\0\x0B"
457
     */
458
    public function isWhitespace(?string $whitespaces = " \t\n\r\0\x0B"): bool
459
    {
460
        if (null === $whitespaces) {
10✔
461
            $whitespaces = " \t\n\r\0\x0B";
8✔
462
        }
463

464
        if ($this->isArray && !$this->isGivenKind(T_WHITESPACE)) {
10✔
465
            return false;
1✔
466
        }
467

468
        return '' === trim($this->content, $whitespaces);
9✔
469
    }
470

471
    /**
472
     * @return array{
473
     *     id: int|null,
474
     *     name: non-empty-string|null,
475
     *     content: string,
476
     *     isArray: bool,
477
     *     changed: bool,
478
     * }
479
     */
480
    public function toArray(): array
481
    {
482
        return [
3✔
483
            'id' => $this->id,
3✔
484
            'name' => $this->getName(),
3✔
485
            'content' => $this->content,
3✔
486
            'isArray' => $this->isArray,
3✔
487
            'changed' => false, // @TODO v4: remove index
3✔
488
        ];
3✔
489
    }
490

491
    /**
492
     * @return non-empty-string
493
     */
494
    public function toJson(): string
495
    {
496
        $jsonResult = json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
×
497

498
        if (JSON_ERROR_NONE !== json_last_error()) {
×
UNCOV
499
            $jsonResult = json_encode(
×
UNCOV
500
                [
×
UNCOV
501
                    'errorDescription' => 'Cannot encode Tokens to JSON.',
×
UNCOV
502
                    'rawErrorMessage' => json_last_error_msg(),
×
UNCOV
503
                ],
×
UNCOV
504
                JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK
×
UNCOV
505
            );
×
506
        }
507

UNCOV
508
        \assert(false !== $jsonResult);
×
509

UNCOV
510
        return $jsonResult;
×
511
    }
512

513
    /**
514
     * @param list<string> $tokenNames
515
     *
516
     * @return array<int, int>
517
     */
518
    private static function getTokenKindsForNames(array $tokenNames): array
519
    {
520
        $keywords = [];
2✔
521
        foreach ($tokenNames as $keywordName) {
2✔
522
            $keyword = \constant($keywordName);
2✔
523
            $keywords[$keyword] = $keyword;
2✔
524
        }
525

526
        return $keywords;
2✔
527
    }
528
}
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

© 2026 Coveralls, Inc