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

keradus / PHP-CS-Fixer / 17319949156

29 Aug 2025 09:20AM UTC coverage: 94.696% (-0.05%) from 94.744%
17319949156

push

github

keradus
CS

28333 of 29920 relevant lines covered (94.7%)

45.63 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
/**
62
 * @param string|null $bar
63
 */
64
'
3✔
65
                ),
3✔
66
                new CodeSample(
3✔
67
                    '<?php
3✔
68
/**
69
 * @param null|string $bar
70
 */
71
',
3✔
72
                    ['null_adjustment' => 'always_last']
3✔
73
                ),
3✔
74
                new CodeSample(
3✔
75
                    '<?php
3✔
76
/**
77
 * @param null|string|int|\Foo $bar
78
 */
79
',
3✔
80
                    ['sort_algorithm' => 'alpha']
3✔
81
                ),
3✔
82
                new CodeSample(
3✔
83
                    '<?php
3✔
84
/**
85
 * @param null|string|int|\Foo $bar
86
 */
87
',
3✔
88
                    [
3✔
89
                        'sort_algorithm' => 'alpha',
3✔
90
                        'null_adjustment' => 'always_last',
3✔
91
                    ]
3✔
92
                ),
3✔
93
                new CodeSample(
3✔
94
                    '<?php
3✔
95
/**
96
 * @param null|string|int|\Foo $bar
97
 */
98
',
3✔
99
                    [
3✔
100
                        'sort_algorithm' => 'alpha',
3✔
101
                        'null_adjustment' => 'none',
3✔
102
                    ]
3✔
103
                ),
3✔
104
                new CodeSample(
3✔
105
                    '<?php
3✔
106
/**
107
 * @param Aaa|AA $bar
108
 */
109
',
3✔
110
                    ['case_sensitive' => true]
3✔
111
                ),
3✔
112
            ]
3✔
113
        );
3✔
114
    }
115

116
    /**
117
     * {@inheritdoc}
118
     *
119
     * Must run before PhpdocAlignFixer.
120
     * Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocArrayTypeFixer, PhpdocIndentFixer, PhpdocListTypeFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
121
     */
122
    public function getPriority(): int
123
    {
124
        return 0;
1✔
125
    }
126

127
    public function isCandidate(Tokens $tokens): bool
128
    {
129
        return $tokens->isTokenKindFound(\T_DOC_COMMENT);
157✔
130
    }
131

132
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
133
    {
134
        return new FixerConfigurationResolver([
166✔
135
            (new FixerOptionBuilder('sort_algorithm', 'The sorting algorithm to apply.'))
166✔
136
                ->setAllowedValues(['alpha', 'none'])
166✔
137
                ->setDefault('alpha')
166✔
138
                ->getOption(),
166✔
139
            (new FixerOptionBuilder('null_adjustment', 'Forces the position of `null` (overrides `sort_algorithm`).'))
166✔
140
                ->setAllowedValues(['always_first', 'always_last', 'none'])
166✔
141
                ->setDefault('always_first')
166✔
142
                ->getOption(),
166✔
143
            (new FixerOptionBuilder('case_sensitive', 'Whether the sorting should be case sensitive.'))
166✔
144
                ->setAllowedTypes(['bool'])
166✔
145
                ->setDefault(false)
166✔
146
                ->getOption(),
166✔
147
        ]);
166✔
148
    }
149

150
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
151
    {
152
        foreach ($tokens as $index => $token) {
157✔
153
            if (!$token->isGivenKind(\T_DOC_COMMENT)) {
157✔
154
                continue;
157✔
155
            }
156

157
            $doc = new DocBlock($token->getContent());
157✔
158
            $annotations = $doc->getAnnotationsOfType(Annotation::TAGS_WITH_TYPES);
157✔
159

160
            if (0 === \count($annotations)) {
157✔
161
                continue;
×
162
            }
163

164
            foreach ($annotations as $annotation) {
157✔
165
                // fix main types
166
                if (null !== $annotation->getTypeExpression()) {
157✔
167
                    $annotation->setTypes(
157✔
168
                        $this->sortTypes(
157✔
169
                            $annotation->getTypeExpression()
157✔
170
                        )
157✔
171
                    );
157✔
172
                }
173

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

179
                    return implode('|', $this->sortTypes($typeExpression));
6✔
180
                }, $line->getContent()));
157✔
181
            }
182

183
            $tokens[$index] = new Token([\T_DOC_COMMENT, $doc->getContent()]);
157✔
184
        }
185
    }
186

187
    /**
188
     * @return list<string>
189
     */
190
    private function sortTypes(TypeExpression $typeExpression): array
191
    {
192
        $normalizeType = static fn (string $type): string => Preg::replace('/^\(*\??\\\?/', '', $type);
157✔
193

194
        $sortedTypeExpression = $typeExpression->sortTypes(
157✔
195
            function (TypeExpression $a, TypeExpression $b) use ($normalizeType): int {
157✔
196
                $a = $normalizeType($a->toString());
147✔
197
                $b = $normalizeType($b->toString());
147✔
198
                $lowerCaseA = strtolower($a);
147✔
199
                $lowerCaseB = strtolower($b);
147✔
200

201
                if ('none' !== $this->configuration['null_adjustment']) {
147✔
202
                    if ('null' === $lowerCaseA && 'null' !== $lowerCaseB) {
109✔
203
                        return 'always_last' === $this->configuration['null_adjustment'] ? 1 : -1;
68✔
204
                    }
205
                    if ('null' !== $lowerCaseA && 'null' === $lowerCaseB) {
104✔
206
                        return 'always_last' === $this->configuration['null_adjustment'] ? -1 : 1;
58✔
207
                    }
208
                }
209

210
                if ('alpha' === $this->configuration['sort_algorithm']) {
103✔
211
                    return true === $this->configuration['case_sensitive'] ? $a <=> $b : strcasecmp($a, $b);
82✔
212
                }
213

214
                return 0;
21✔
215
            }
157✔
216
        );
157✔
217

218
        return $sortedTypeExpression->getTypes();
157✔
219
    }
220
}
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