• 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

98.26
/src/Fixer/ClassNotation/OrderedInterfacesFixer.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\ClassNotation;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
19
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
20
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
21
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
22
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
23
use PhpCsFixer\FixerDefinition\CodeSample;
24
use PhpCsFixer\FixerDefinition\FixerDefinition;
25
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
26
use PhpCsFixer\Tokenizer\Token;
27
use PhpCsFixer\Tokenizer\Tokens;
28

29
/**
30
 * @phpstan-type _AutogeneratedInputConfiguration array{
31
 *  case_sensitive?: bool,
32
 *  direction?: 'ascend'|'descend',
33
 *  order?: 'alpha'|'length',
34
 * }
35
 * @phpstan-type _AutogeneratedComputedConfiguration array{
36
 *  case_sensitive: bool,
37
 *  direction: 'ascend'|'descend',
38
 *  order: 'alpha'|'length',
39
 * }
40
 *
41
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
42
 *
43
 * @author Dave van der Brugge <dmvdbrugge@gmail.com>
44
 *
45
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
46
 */
47
final class OrderedInterfacesFixer extends AbstractFixer implements ConfigurableFixerInterface
48
{
49
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
50
    use ConfigurableFixerTrait;
51

52
    /** @internal */
53
    public const OPTION_DIRECTION = 'direction';
54

55
    /** @internal */
56
    public const OPTION_ORDER = 'order';
57

58
    /** @internal */
59
    public const DIRECTION_ASCEND = 'ascend';
60

61
    /** @internal */
62
    public const DIRECTION_DESCEND = 'descend';
63

64
    /** @internal */
65
    public const ORDER_ALPHA = 'alpha';
66

67
    /** @internal */
68
    public const ORDER_LENGTH = 'length';
69

70
    /**
71
     * Array of supported directions in configuration.
72
     *
73
     * @var non-empty-list<string>
74
     */
75
    private const SUPPORTED_DIRECTION_OPTIONS = [
76
        self::DIRECTION_ASCEND,
77
        self::DIRECTION_DESCEND,
78
    ];
79

80
    /**
81
     * Array of supported orders in configuration.
82
     *
83
     * @var non-empty-list<string>
84
     */
85
    private const SUPPORTED_ORDER_OPTIONS = [
86
        self::ORDER_ALPHA,
87
        self::ORDER_LENGTH,
88
    ];
89

90
    public function getDefinition(): FixerDefinitionInterface
91
    {
92
        return new FixerDefinition(
3✔
93
            'Orders the interfaces in an `implements` or `interface extends` clause.',
3✔
94
            [
3✔
95
                new CodeSample(
3✔
96
                    "<?php\n\nfinal class ExampleA implements Gamma, Alpha, Beta {}\n\ninterface ExampleB extends Gamma, Alpha, Beta {}\n"
3✔
97
                ),
3✔
98
                new CodeSample(
3✔
99
                    "<?php\n\nfinal class ExampleA implements Gamma, Alpha, Beta {}\n\ninterface ExampleB extends Gamma, Alpha, Beta {}\n",
3✔
100
                    [self::OPTION_DIRECTION => self::DIRECTION_DESCEND]
3✔
101
                ),
3✔
102
                new CodeSample(
3✔
103
                    "<?php\n\nfinal class ExampleA implements MuchLonger, Short, Longer {}\n\ninterface ExampleB extends MuchLonger, Short, Longer {}\n",
3✔
104
                    [self::OPTION_ORDER => self::ORDER_LENGTH]
3✔
105
                ),
3✔
106
                new CodeSample(
3✔
107
                    "<?php\n\nfinal class ExampleA implements MuchLonger, Short, Longer {}\n\ninterface ExampleB extends MuchLonger, Short, Longer {}\n",
3✔
108
                    [
3✔
109
                        self::OPTION_ORDER => self::ORDER_LENGTH,
3✔
110
                        self::OPTION_DIRECTION => self::DIRECTION_DESCEND,
3✔
111
                    ]
3✔
112
                ),
3✔
113
                new CodeSample(
3✔
114
                    "<?php\n\nfinal class ExampleA implements IgnorecaseB, IgNoReCaSeA, IgnoreCaseC {}\n\ninterface ExampleB extends IgnorecaseB, IgNoReCaSeA, IgnoreCaseC {}\n",
3✔
115
                    [
3✔
116
                        self::OPTION_ORDER => self::ORDER_ALPHA,
3✔
117
                    ]
3✔
118
                ),
3✔
119
                new CodeSample(
3✔
120
                    "<?php\n\nfinal class ExampleA implements Casesensitivea, CaseSensitiveA, CasesensitiveA {}\n\ninterface ExampleB extends Casesensitivea, CaseSensitiveA, CasesensitiveA {}\n",
3✔
121
                    [
3✔
122
                        self::OPTION_ORDER => self::ORDER_ALPHA,
3✔
123
                        'case_sensitive' => true,
3✔
124
                    ]
3✔
125
                ),
3✔
126
            ],
3✔
127
        );
3✔
128
    }
129

130
    /**
131
     * {@inheritdoc}
132
     *
133
     * Must run after FullyQualifiedStrictTypesFixer.
134
     */
135
    public function getPriority(): int
136
    {
137
        return 0;
1✔
138
    }
139

140
    public function isCandidate(Tokens $tokens): bool
141
    {
142
        return $tokens->isTokenKindFound(\T_IMPLEMENTS)
27✔
143
            || $tokens->isAllTokenKindsFound([\T_INTERFACE, \T_EXTENDS]);
27✔
144
    }
145

146
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
147
    {
148
        foreach ($tokens as $index => $token) {
27✔
149
            if (!$token->isGivenKind(\T_IMPLEMENTS)) {
27✔
150
                if (!$token->isGivenKind(\T_EXTENDS)) {
27✔
151
                    continue;
27✔
152
                }
153

154
                $nameTokenIndex = $tokens->getPrevMeaningfulToken($index);
2✔
155
                $interfaceTokenIndex = $tokens->getPrevMeaningfulToken($nameTokenIndex);
2✔
156
                $interfaceToken = $tokens[$interfaceTokenIndex];
2✔
157

158
                if (!$interfaceToken->isGivenKind(\T_INTERFACE)) {
2✔
159
                    continue;
×
160
                }
161
            }
162

163
            $implementsStart = $index + 1;
27✔
164
            $implementsEnd = $tokens->getPrevMeaningfulToken($tokens->getNextTokenOfKind($implementsStart, ['{']));
27✔
165

166
            $interfacesTokens = $this->getInterfaces($tokens, $implementsStart, $implementsEnd);
27✔
167

168
            if (1 === \count($interfacesTokens)) {
27✔
169
                continue;
5✔
170
            }
171

172
            $interfaces = [];
22✔
173
            foreach ($interfacesTokens as $interfaceIndex => $interface) {
22✔
174
                $interfaceTokens = Tokens::fromArray($interface);
22✔
175
                $normalized = '';
22✔
176
                $actualInterfaceIndex = $interfaceTokens->getNextMeaningfulToken(-1);
22✔
177

178
                while ($interfaceTokens->offsetExists($actualInterfaceIndex)) {
22✔
179
                    $token = $interfaceTokens[$actualInterfaceIndex];
22✔
180

181
                    if ($token->isComment() || $token->isWhitespace()) {
22✔
182
                        break;
×
183
                    }
184

185
                    $normalized .= str_replace('\\', ' ', $token->getContent());
22✔
186
                    ++$actualInterfaceIndex;
22✔
187
                }
188

189
                $interfaces[$interfaceIndex] = [
22✔
190
                    'tokens' => $interface,
22✔
191
                    'normalized' => $normalized,
22✔
192
                    'originalIndex' => $interfaceIndex,
22✔
193
                ];
22✔
194
            }
195

196
            usort($interfaces, function (array $first, array $second): int {
22✔
197
                $score = self::ORDER_LENGTH === $this->configuration[self::OPTION_ORDER]
22✔
198
                    ? \strlen($first['normalized']) - \strlen($second['normalized'])
7✔
199
                    : (
22✔
200
                        true === $this->configuration['case_sensitive']
16✔
201
                        ? $first['normalized'] <=> $second['normalized']
4✔
202
                        : strcasecmp($first['normalized'], $second['normalized'])
16✔
203
                    );
22✔
204

205
                if (self::DIRECTION_DESCEND === $this->configuration[self::OPTION_DIRECTION]) {
22✔
206
                    $score *= -1;
6✔
207
                }
208

209
                return $score;
22✔
210
            });
22✔
211

212
            $changed = false;
22✔
213

214
            foreach ($interfaces as $interfaceIndex => $interface) {
22✔
215
                if ($interface['originalIndex'] !== $interfaceIndex) {
22✔
216
                    $changed = true;
22✔
217

218
                    break;
22✔
219
                }
220
            }
221

222
            if (!$changed) {
22✔
223
                continue;
21✔
224
            }
225

226
            $newTokens = array_shift($interfaces)['tokens'];
22✔
227

228
            foreach ($interfaces as $interface) {
22✔
229
                array_push($newTokens, new Token(','), ...$interface['tokens']);
22✔
230
            }
231

232
            $tokens->overrideRange($implementsStart, $implementsEnd, $newTokens);
22✔
233
        }
234
    }
235

236
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
237
    {
238
        return new FixerConfigurationResolver([
36✔
239
            (new FixerOptionBuilder(self::OPTION_ORDER, 'How the interfaces should be ordered.'))
36✔
240
                ->setAllowedValues(self::SUPPORTED_ORDER_OPTIONS)
36✔
241
                ->setDefault(self::ORDER_ALPHA)
36✔
242
                ->getOption(),
36✔
243
            (new FixerOptionBuilder(self::OPTION_DIRECTION, 'Which direction the interfaces should be ordered.'))
36✔
244
                ->setAllowedValues(self::SUPPORTED_DIRECTION_OPTIONS)
36✔
245
                ->setDefault(self::DIRECTION_ASCEND)
36✔
246
                ->getOption(),
36✔
247
            (new FixerOptionBuilder('case_sensitive', 'Whether the sorting should be case sensitive.'))
36✔
248
                ->setAllowedTypes(['bool'])
36✔
249
                ->setDefault(false)
36✔
250
                ->getOption(),
36✔
251
        ]);
36✔
252
    }
253

254
    /**
255
     * @return array<int, list<Token>>
256
     */
257
    private function getInterfaces(Tokens $tokens, int $implementsStart, int $implementsEnd): array
258
    {
259
        $interfaces = [];
27✔
260
        $interfaceIndex = 0;
27✔
261

262
        for ($i = $implementsStart; $i <= $implementsEnd; ++$i) {
27✔
263
            if ($tokens[$i]->equals(',')) {
27✔
264
                ++$interfaceIndex;
22✔
265
                $interfaces[$interfaceIndex] = [];
22✔
266

267
                continue;
22✔
268
            }
269

270
            $interfaces[$interfaceIndex][] = $tokens[$i];
27✔
271
        }
272

273
        return $interfaces;
27✔
274
    }
275
}
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