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

keradus / PHP-CS-Fixer / 17253322895

26 Aug 2025 11:52PM UTC coverage: 94.753% (+0.008%) from 94.745%
17253322895

push

github

keradus
add to git-blame-ignore-revs

28316 of 29884 relevant lines covered (94.75%)

45.64 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
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
28
 */
29
final class ImportProcessor
30
{
31
    private WhitespacesFixerConfig $whitespacesConfig;
32

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

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

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

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

56
            $items = [];
2✔
57

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

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

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

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

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

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

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

101
        array_pop($newTokens);
9✔
102

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