• 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

95.45
/src/Tokenizer/Transformer/UseTransformer.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\Transformer;
16

17
use PhpCsFixer\Tokenizer\AbstractTransformer;
18
use PhpCsFixer\Tokenizer\CT;
19
use PhpCsFixer\Tokenizer\FCT;
20
use PhpCsFixer\Tokenizer\Token;
21
use PhpCsFixer\Tokenizer\Tokens;
22

23
/**
24
 * Transform T_USE into:
25
 * - CT::T_USE_TRAIT for imports,
26
 * - CT::T_USE_LAMBDA for lambda variable uses.
27
 *
28
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
29
 *
30
 * @internal
31
 */
32
final class UseTransformer extends AbstractTransformer
33
{
34
    private const CLASS_TYPES = [T_TRAIT, FCT::T_ENUM];
35

36
    public function getPriority(): int
37
    {
38
        // Should run after CurlyBraceTransformer and before TypeColonTransformer
39
        return -5;
2✔
40
    }
41

42
    public function getRequiredPhpVersionId(): int
43
    {
44
        return 5_03_00;
3✔
45
    }
46

47
    public function process(Tokens $tokens, Token $token, int $index): void
48
    {
49
        if ($token->isGivenKind(T_USE) && $this->isUseForLambda($tokens, $index)) {
10✔
50
            $tokens[$index] = new Token([CT::T_USE_LAMBDA, $token->getContent()]);
1✔
51

52
            return;
1✔
53
        }
54

55
        // Only search inside class/trait body for `T_USE` for traits.
56
        // Cannot import traits inside interfaces or anywhere else
57

58
        if ($token->isGivenKind(T_CLASS)) {
10✔
59
            if ($tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_DOUBLE_COLON)) {
4✔
UNCOV
60
                return;
×
61
            }
62
        } elseif (!$token->isGivenKind(self::CLASS_TYPES)) {
10✔
63
            return;
10✔
64
        }
65

66
        $index = $tokens->getNextTokenOfKind($index, ['{']);
5✔
67
        $innerLimit = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
5✔
68

69
        while ($index < $innerLimit) {
5✔
70
            $token = $tokens[++$index];
5✔
71

72
            if (!$token->isGivenKind(T_USE)) {
5✔
73
                continue;
5✔
74
            }
75

76
            if ($this->isUseForLambda($tokens, $index)) {
4✔
77
                $tokens[$index] = new Token([CT::T_USE_LAMBDA, $token->getContent()]);
2✔
78
            } else {
79
                $tokens[$index] = new Token([CT::T_USE_TRAIT, $token->getContent()]);
4✔
80
            }
81
        }
82
    }
83

84
    public function getCustomTokens(): array
85
    {
86
        return [CT::T_USE_TRAIT, CT::T_USE_LAMBDA];
10✔
87
    }
88

89
    /**
90
     * Check if token under given index is `use` statement for lambda function.
91
     */
92
    private function isUseForLambda(Tokens $tokens, int $index): bool
93
    {
94
        $nextToken = $tokens[$tokens->getNextMeaningfulToken($index)];
9✔
95

96
        // test `function () use ($foo) {}` case
97
        return $nextToken->equals('(');
9✔
98
    }
99
}
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