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

keradus / PHP-CS-Fixer / 19958239208

05 Dec 2025 09:13AM UTC coverage: 93.181% (-1.0%) from 94.158%
19958239208

push

github

keradus
chore: .php-cs-fixer.dist.php - remove no longer needed rule, 'expectedDeprecation' annotation does not exist for long time

28928 of 31045 relevant lines covered (93.18%)

44.49 hits per line

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

80.56
/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, non-empty-string>,
41
     *     class?: array<int|string, non-empty-string>,
42
     *     function?: array<int|string, non-empty-string>
43
     * } $imports
44
     */
45
    public function insertImports(Tokens $tokens, array $imports, int $atIndex): void
46
    {
47
        $lineEnding = $this->whitespacesConfig->getLineEnding();
2✔
48

49
        $prevIndex = $tokens->getPrevMeaningfulToken($atIndex);
2✔
50
        if (null !== $prevIndex && $tokens[$prevIndex]->isGivenKind(\T_OPEN_TAG_WITH_ECHO)) {
2✔
51
            $tokens->insertAt($prevIndex, Tokens::fromCode("<?php\n?>"));
×
52
            $atIndex = $prevIndex + 1;
×
53
        }
54

55
        if (!$tokens[$atIndex]->isWhitespace() || !str_contains($tokens[$atIndex]->getContent(), "\n")) {
2✔
56
            $tokens->insertAt($atIndex, new Token([\T_WHITESPACE, $lineEnding]));
×
57
        }
58

59
        foreach ($imports as $type => $typeImports) {
2✔
60
            sort($typeImports);
2✔
61

62
            $items = [];
2✔
63

64
            foreach ($typeImports as $name) {
2✔
65
                $items = array_merge($items, [
2✔
66
                    new Token([\T_WHITESPACE, $lineEnding]),
2✔
67
                    new Token([\T_USE, 'use']),
2✔
68
                    new Token([\T_WHITESPACE, ' ']),
2✔
69
                ]);
2✔
70

71
                if ('const' === $type) {
2✔
72
                    $items[] = new Token([CT::T_CONST_IMPORT, 'const']);
×
73
                    $items[] = new Token([\T_WHITESPACE, ' ']);
×
74
                } elseif ('function' === $type) {
2✔
75
                    $items[] = new Token([CT::T_FUNCTION_IMPORT, 'function']);
×
76
                    $items[] = new Token([\T_WHITESPACE, ' ']);
×
77
                }
78

79
                $items = array_merge($items, self::tokenizeName($name));
2✔
80
                $items[] = new Token(';');
2✔
81
            }
82

83
            $tokens->insertAt($atIndex, $items);
2✔
84
        }
85
    }
86

87
    /**
88
     * @param non-empty-string $name
89
     *
90
     * @return list<Token>
91
     */
92
    public static function tokenizeName(string $name): array
93
    {
94
        $parts = explode('\\', $name);
9✔
95
        $newTokens = [];
9✔
96

97
        if ('' === $parts[0]) {
9✔
98
            $newTokens[] = new Token([\T_NS_SEPARATOR, '\\']);
4✔
99
            array_shift($parts);
4✔
100
        }
101

102
        foreach ($parts as $part) {
9✔
103
            $newTokens[] = new Token([\T_STRING, $part]);
9✔
104
            $newTokens[] = new Token([\T_NS_SEPARATOR, '\\']);
9✔
105
        }
106

107
        array_pop($newTokens);
9✔
108

109
        return $newTokens;
9✔
110
    }
111
}
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