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

keradus / PHP-CS-Fixer / 17678835382

12 Sep 2025 03:24PM UTC coverage: 94.69% (-0.06%) from 94.75%
17678835382

push

github

keradus
fix typo

1 of 1 new or added line in 1 file covered. (100.0%)

1042 existing lines in 177 files now uncovered.

28424 of 30018 relevant lines covered (94.69%)

45.5 hits per line

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

98.94
/src/Fixer/Phpdoc/PhpdocTypesOrderFixer.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\Phpdoc;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\DocBlock\Annotation;
19
use PhpCsFixer\DocBlock\DocBlock;
20
use PhpCsFixer\DocBlock\TypeExpression;
21
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
22
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
23
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
24
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
25
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
26
use PhpCsFixer\FixerDefinition\CodeSample;
27
use PhpCsFixer\FixerDefinition\FixerDefinition;
28
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
29
use PhpCsFixer\Preg;
30
use PhpCsFixer\Tokenizer\Token;
31
use PhpCsFixer\Tokenizer\Tokens;
32

33
/**
34
 * @phpstan-type _AutogeneratedInputConfiguration array{
35
 *  case_sensitive?: bool,
36
 *  null_adjustment?: 'always_first'|'always_last'|'none',
37
 *  sort_algorithm?: 'alpha'|'none',
38
 * }
39
 * @phpstan-type _AutogeneratedComputedConfiguration array{
40
 *  case_sensitive: bool,
41
 *  null_adjustment: 'always_first'|'always_last'|'none',
42
 *  sort_algorithm: 'alpha'|'none',
43
 * }
44
 *
45
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
46
 *
47
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
48
 */
49
final class PhpdocTypesOrderFixer extends AbstractFixer implements ConfigurableFixerInterface
50
{
51
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
52
    use ConfigurableFixerTrait;
53

54
    public function getDefinition(): FixerDefinitionInterface
55
    {
56
        return new FixerDefinition(
3✔
57
            'Sorts PHPDoc types.',
3✔
58
            [
3✔
59
                new CodeSample(
3✔
60
                    <<<'PHP'
3✔
61
                        <?php
62
                        /**
63
                         * @param string|null $bar
64
                         */
65

66
                        PHP
3✔
67
                ),
3✔
68
                new CodeSample(
3✔
69
                    <<<'PHP'
3✔
70
                        <?php
71
                        /**
72
                         * @param null|string $bar
73
                         */
74

75
                        PHP,
3✔
76
                    ['null_adjustment' => 'always_last']
3✔
77
                ),
3✔
78
                new CodeSample(
3✔
79
                    <<<'PHP'
3✔
80
                        <?php
81
                        /**
82
                         * @param null|string|int|\Foo $bar
83
                         */
84

85
                        PHP,
3✔
86
                    ['sort_algorithm' => 'alpha']
3✔
87
                ),
3✔
88
                new CodeSample(
3✔
89
                    <<<'PHP'
3✔
90
                        <?php
91
                        /**
92
                         * @param null|string|int|\Foo $bar
93
                         */
94

95
                        PHP,
3✔
96
                    [
3✔
97
                        'sort_algorithm' => 'alpha',
3✔
98
                        'null_adjustment' => 'always_last',
3✔
99
                    ]
3✔
100
                ),
3✔
101
                new CodeSample(
3✔
102
                    <<<'PHP'
3✔
103
                        <?php
104
                        /**
105
                         * @param null|string|int|\Foo $bar
106
                         */
107

108
                        PHP,
3✔
109
                    [
3✔
110
                        'sort_algorithm' => 'alpha',
3✔
111
                        'null_adjustment' => 'none',
3✔
112
                    ]
3✔
113
                ),
3✔
114
                new CodeSample(
3✔
115
                    <<<'PHP'
3✔
116
                        <?php
117
                        /**
118
                         * @param Aaa|AA $bar
119
                         */
120

121
                        PHP,
3✔
122
                    ['case_sensitive' => true]
3✔
123
                ),
3✔
124
            ]
3✔
125
        );
3✔
126
    }
127

128
    /**
129
     * {@inheritdoc}
130
     *
131
     * Must run before PhpdocAlignFixer.
132
     * Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocArrayTypeFixer, PhpdocIndentFixer, PhpdocListTypeFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
133
     */
134
    public function getPriority(): int
135
    {
136
        return 0;
1✔
137
    }
138

139
    public function isCandidate(Tokens $tokens): bool
140
    {
141
        return $tokens->isTokenKindFound(\T_DOC_COMMENT);
157✔
142
    }
143

144
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
145
    {
146
        return new FixerConfigurationResolver([
166✔
147
            (new FixerOptionBuilder('sort_algorithm', 'The sorting algorithm to apply.'))
166✔
148
                ->setAllowedValues(['alpha', 'none'])
166✔
149
                ->setDefault('alpha')
166✔
150
                ->getOption(),
166✔
151
            (new FixerOptionBuilder('null_adjustment', 'Forces the position of `null` (overrides `sort_algorithm`).'))
166✔
152
                ->setAllowedValues(['always_first', 'always_last', 'none'])
166✔
153
                ->setDefault('always_first')
166✔
154
                ->getOption(),
166✔
155
            (new FixerOptionBuilder('case_sensitive', 'Whether the sorting should be case sensitive.'))
166✔
156
                ->setAllowedTypes(['bool'])
166✔
157
                ->setDefault(false)
166✔
158
                ->getOption(),
166✔
159
        ]);
166✔
160
    }
161

162
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
163
    {
164
        foreach ($tokens as $index => $token) {
157✔
165
            if (!$token->isGivenKind(\T_DOC_COMMENT)) {
157✔
166
                continue;
157✔
167
            }
168

169
            $doc = new DocBlock($token->getContent());
157✔
170
            $annotations = $doc->getAnnotationsOfType(Annotation::TAGS_WITH_TYPES);
157✔
171

172
            if (0 === \count($annotations)) {
157✔
UNCOV
173
                continue;
×
174
            }
175

176
            foreach ($annotations as $annotation) {
157✔
177
                // fix main types
178
                if (null !== $annotation->getTypeExpression()) {
157✔
179
                    $annotation->setTypes(
157✔
180
                        $this->sortTypes(
157✔
181
                            $annotation->getTypeExpression()
157✔
182
                        )
157✔
183
                    );
157✔
184
                }
185

186
                // fix @method parameters types
187
                $line = $doc->getLine($annotation->getStart());
157✔
188
                $line->setContent(Preg::replaceCallback('/\*\h*@method\h+'.TypeExpression::REGEX_TYPES.'\h+\K(?&callable)/', function (array $matches) {
157✔
189
                    $typeExpression = new TypeExpression($matches[0], null, []);
6✔
190

191
                    return implode('|', $this->sortTypes($typeExpression));
6✔
192
                }, $line->getContent()));
157✔
193
            }
194

195
            $tokens[$index] = new Token([\T_DOC_COMMENT, $doc->getContent()]);
157✔
196
        }
197
    }
198

199
    /**
200
     * @return list<string>
201
     */
202
    private function sortTypes(TypeExpression $typeExpression): array
203
    {
204
        $normalizeType = static fn (string $type): string => Preg::replace('/^\(*\??\\\?/', '', $type);
157✔
205

206
        $sortedTypeExpression = $typeExpression->sortTypes(
157✔
207
            function (TypeExpression $a, TypeExpression $b) use ($normalizeType): int {
157✔
208
                $a = $normalizeType($a->toString());
147✔
209
                $b = $normalizeType($b->toString());
147✔
210
                $lowerCaseA = strtolower($a);
147✔
211
                $lowerCaseB = strtolower($b);
147✔
212

213
                if ('none' !== $this->configuration['null_adjustment']) {
147✔
214
                    if ('null' === $lowerCaseA && 'null' !== $lowerCaseB) {
109✔
215
                        return 'always_last' === $this->configuration['null_adjustment'] ? 1 : -1;
68✔
216
                    }
217
                    if ('null' !== $lowerCaseA && 'null' === $lowerCaseB) {
104✔
218
                        return 'always_last' === $this->configuration['null_adjustment'] ? -1 : 1;
58✔
219
                    }
220
                }
221

222
                if ('alpha' === $this->configuration['sort_algorithm']) {
103✔
223
                    return true === $this->configuration['case_sensitive'] ? $a <=> $b : strcasecmp($a, $b);
82✔
224
                }
225

226
                return 0;
21✔
227
            }
157✔
228
        );
157✔
229

230
        return $sortedTypeExpression->getTypes();
157✔
231
    }
232
}
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