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

keradus / PHP-CS-Fixer / 16253708939

13 Jul 2025 09:37PM UTC coverage: 94.807% (+0.001%) from 94.806%
16253708939

push

github

web-flow
Merge branch 'master' into native_constant_invocation__CS

2259 of 2334 new or added lines in 338 files covered. (96.79%)

7 existing lines in 5 files now uncovered.

28260 of 29808 relevant lines covered (94.81%)

45.39 hits per line

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

84.38
/src/Tokenizer/Processor/ImportProcessor.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\Processor;
16

17
use PhpCsFixer\Tokenizer\CT;
18
use PhpCsFixer\Tokenizer\Token;
19
use PhpCsFixer\Tokenizer\Tokens;
20
use PhpCsFixer\WhitespacesFixerConfig;
21

22
/**
23
 * @author Greg Korba <greg@codito.dev>
24
 *
25
 * @readonly
26
 */
27
final class ImportProcessor
28
{
29
    private WhitespacesFixerConfig $whitespacesConfig;
30

31
    public function __construct(WhitespacesFixerConfig $whitespacesConfig)
32
    {
33
        $this->whitespacesConfig = $whitespacesConfig;
2✔
34
    }
35

36
    /**
37
     * @param array{
38
     *     const?: array<int|string, class-string>,
39
     *     class?: array<int|string, class-string>,
40
     *     function?: array<int|string, class-string>
41
     * } $imports
42
     */
43
    public function insertImports(Tokens $tokens, array $imports, int $atIndex): void
44
    {
45
        $lineEnding = $this->whitespacesConfig->getLineEnding();
2✔
46

47
        if (!$tokens[$atIndex]->isWhitespace() || !str_contains($tokens[$atIndex]->getContent(), "\n")) {
2✔
NEW
48
            $tokens->insertAt($atIndex, new Token([\T_WHITESPACE, $lineEnding]));
×
49
        }
50

51
        foreach ($imports as $type => $typeImports) {
2✔
52
            sort($typeImports);
2✔
53

54
            $items = [];
2✔
55

56
            foreach ($typeImports as $name) {
2✔
57
                $items = array_merge($items, [
2✔
58
                    new Token([\T_WHITESPACE, $lineEnding]),
2✔
59
                    new Token([\T_USE, 'use']),
2✔
60
                    new Token([\T_WHITESPACE, ' ']),
2✔
61
                ]);
2✔
62

63
                if ('const' === $type) {
2✔
64
                    $items[] = new Token([CT::T_CONST_IMPORT, 'const']);
×
NEW
65
                    $items[] = new Token([\T_WHITESPACE, ' ']);
×
66
                } elseif ('function' === $type) {
2✔
67
                    $items[] = new Token([CT::T_FUNCTION_IMPORT, 'function']);
×
NEW
68
                    $items[] = new Token([\T_WHITESPACE, ' ']);
×
69
                }
70

71
                $items = array_merge($items, self::tokenizeName($name));
2✔
72
                $items[] = new Token(';');
2✔
73
            }
74

75
            $tokens->insertAt($atIndex, $items);
2✔
76
        }
77
    }
78

79
    /**
80
     * @param class-string $name
81
     *
82
     * @return list<Token>
83
     */
84
    public static function tokenizeName(string $name): array
85
    {
86
        $parts = explode('\\', $name);
9✔
87
        $newTokens = [];
9✔
88

89
        if ('' === $parts[0]) {
9✔
90
            $newTokens[] = new Token([\T_NS_SEPARATOR, '\\']);
4✔
91
            array_shift($parts);
4✔
92
        }
93

94
        foreach ($parts as $part) {
9✔
95
            $newTokens[] = new Token([\T_STRING, $part]);
9✔
96
            $newTokens[] = new Token([\T_NS_SEPARATOR, '\\']);
9✔
97
        }
98

99
        array_pop($newTokens);
9✔
100

101
        return $newTokens;
9✔
102
    }
103
}
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