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

keradus / PHP-CS-Fixer / 17252691116

26 Aug 2025 11:09PM UTC coverage: 94.743% (-0.01%) from 94.755%
17252691116

push

github

keradus
chore: apply phpdoc_tag_no_named_arguments

28313 of 29884 relevant lines covered (94.74%)

45.64 hits per line

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

96.55
/src/Fixer/Whitespace/BlankLineBetweenImportGroupsFixer.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\Fixer\Whitespace;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
19
use PhpCsFixer\FixerDefinition\CodeSample;
20
use PhpCsFixer\FixerDefinition\FixerDefinition;
21
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
22
use PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer;
23
use PhpCsFixer\Tokenizer\CT;
24
use PhpCsFixer\Tokenizer\Tokens;
25
use PhpCsFixer\Tokenizer\TokensAnalyzer;
26

27
/**
28
 * @author Sander Verkuil <s.verkuil@pm.me>
29
 *
30
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
31
 */
32
final class BlankLineBetweenImportGroupsFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
33
{
34
    private const IMPORT_TYPE_CLASS = 'class';
35

36
    private const IMPORT_TYPE_CONST = 'const';
37

38
    private const IMPORT_TYPE_FUNCTION = 'function';
39

40
    public function getDefinition(): FixerDefinitionInterface
41
    {
42
        return new FixerDefinition(
3✔
43
            'Putting blank lines between `use` statement groups.',
3✔
44
            [
3✔
45
                new CodeSample(
3✔
46
                    '<?php
3✔
47

48
use function AAC;
49
use const AAB;
50
use AAA;
51
'
3✔
52
                ),
3✔
53
                new CodeSample(
3✔
54
                    '<?php
3✔
55
use const AAAA;
56
use const BBB;
57
use Bar;
58
use AAC;
59
use Acme;
60
use function CCC\AA;
61
use function DDD;
62
'
3✔
63
                ),
3✔
64
                new CodeSample(
3✔
65
                    '<?php
3✔
66
use const BBB;
67
use const AAAA;
68
use Acme;
69
use AAC;
70
use Bar;
71
use function DDD;
72
use function CCC\AA;
73
'
3✔
74
                ),
3✔
75
                new CodeSample(
3✔
76
                    '<?php
3✔
77
use const AAAA;
78
use const BBB;
79
use Acme;
80
use function DDD;
81
use AAC;
82
use function CCC\AA;
83
use Bar;
84
'
3✔
85
                ),
3✔
86
            ]
3✔
87
        );
3✔
88
    }
89

90
    /**
91
     * {@inheritdoc}
92
     *
93
     * Must run after OrderedImportsFixer.
94
     */
95
    public function getPriority(): int
96
    {
97
        return -40;
1✔
98
    }
99

100
    public function isCandidate(Tokens $tokens): bool
101
    {
102
        return $tokens->isTokenKindFound(\T_USE);
14✔
103
    }
104

105
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
106
    {
107
        $tokensAnalyzer = new TokensAnalyzer($tokens);
14✔
108
        $namespacesImports = $tokensAnalyzer->getImportUseIndexes(true);
14✔
109

110
        foreach (array_reverse($namespacesImports) as $uses) {
14✔
111
            $this->walkOverUses($tokens, $uses);
14✔
112
        }
113
    }
114

115
    /**
116
     * @param list<int> $uses
117
     */
118
    private function walkOverUses(Tokens $tokens, array $uses): void
119
    {
120
        $usesCount = \count($uses);
14✔
121

122
        if ($usesCount < 2) {
14✔
123
            return; // nothing to fix
×
124
        }
125

126
        $previousType = null;
14✔
127

128
        for ($i = $usesCount - 1; $i >= 0; --$i) {
14✔
129
            $index = $uses[$i];
14✔
130
            $startIndex = $tokens->getNextMeaningfulToken($index + 1);
14✔
131
            $endIndex = $tokens->getNextTokenOfKind($startIndex, [';', [\T_CLOSE_TAG]]);
14✔
132

133
            if ($tokens[$startIndex]->isGivenKind(CT::T_CONST_IMPORT)) {
14✔
134
                $type = self::IMPORT_TYPE_CONST;
10✔
135
            } elseif ($tokens[$startIndex]->isGivenKind(CT::T_FUNCTION_IMPORT)) {
14✔
136
                $type = self::IMPORT_TYPE_FUNCTION;
10✔
137
            } else {
138
                $type = self::IMPORT_TYPE_CLASS;
14✔
139
            }
140

141
            if (null !== $previousType && $type !== $previousType) {
14✔
142
                $this->ensureLine($tokens, $endIndex + 1);
14✔
143
            }
144

145
            $previousType = $type;
14✔
146
        }
147
    }
148

149
    private function ensureLine(Tokens $tokens, int $index): void
150
    {
151
        static $lineEnding;
14✔
152

153
        if (null === $lineEnding) {
14✔
154
            $lineEnding = $this->whitespacesConfig->getLineEnding();
1✔
155
            $lineEnding .= $lineEnding;
1✔
156
        }
157

158
        $index = $this->getInsertIndex($tokens, $index);
14✔
159
        $indent = WhitespacesAnalyzer::detectIndent($tokens, $index);
14✔
160

161
        $tokens->ensureWhitespaceAtIndex($index, 1, $lineEnding.$indent);
14✔
162
    }
163

164
    private function getInsertIndex(Tokens $tokens, int $index): int
165
    {
166
        $tokensCount = \count($tokens);
14✔
167

168
        for (; $index < $tokensCount - 1; ++$index) {
14✔
169
            if (!$tokens[$index]->isWhitespace() && !$tokens[$index]->isComment()) {
14✔
170
                return $index - 1;
3✔
171
            }
172

173
            $content = $tokens[$index]->getContent();
14✔
174

175
            if (str_contains($content, "\n")) {
14✔
176
                return $index;
14✔
177
            }
178
        }
179

180
        return $index;
×
181
    }
182
}
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