• 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

97.73
/src/Fixer/Whitespace/TypeDeclarationSpacesFixer.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\ConfigurableFixerInterface;
19
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
20
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
21
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
22
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
23
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
24
use PhpCsFixer\FixerDefinition\CodeSample;
25
use PhpCsFixer\FixerDefinition\FixerDefinition;
26
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
27
use PhpCsFixer\FixerDefinition\VersionSpecification;
28
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
29
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
30
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
31
use PhpCsFixer\Tokenizer\FCT;
32
use PhpCsFixer\Tokenizer\Token;
33
use PhpCsFixer\Tokenizer\Tokens;
34
use PhpCsFixer\Tokenizer\TokensAnalyzer;
35

36
/**
37
 * @phpstan-type _AutogeneratedInputConfiguration array{
38
 *  elements?: list<'constant'|'function'|'property'>,
39
 * }
40
 * @phpstan-type _AutogeneratedComputedConfiguration array{
41
 *  elements: list<'constant'|'function'|'property'>,
42
 * }
43
 *
44
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
45
 *
46
 * @author Dariusz RumiƄski <dariusz.ruminski@gmail.com>
47
 * @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
48
 *
49
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
50
 */
51
final class TypeDeclarationSpacesFixer extends AbstractFixer implements ConfigurableFixerInterface
52
{
53
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
54
    use ConfigurableFixerTrait;
55

56
    private const PROPERTY_MODIFIERS = [\T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_STATIC, \T_VAR, \T_FINAL, FCT::T_READONLY, FCT::T_PUBLIC_SET, FCT::T_PROTECTED_SET, FCT::T_PRIVATE_SET];
57

58
    public function getDefinition(): FixerDefinitionInterface
59
    {
60
        return new FixerDefinition(
3✔
61
            'Ensure single space between a variable and its type declaration in function arguments and properties.',
3✔
62
            [
3✔
63
                new CodeSample(
3✔
64
                    '<?php
3✔
65
class Bar
66
{
67
    private string    $a;
68
    private bool   $b;
69

70
    public function __invoke(array   $c) {}
71
}
72
'
3✔
73
                ),
3✔
74
                new CodeSample(
3✔
75
                    '<?php
3✔
76
class Foo
77
{
78
    public int   $bar;
79

80
    public function baz(string     $a)
81
    {
82
        return fn(bool    $c): string => (string) $c;
83
    }
84
}
85
',
3✔
86
                    ['elements' => ['function']]
3✔
87
                ),
3✔
88
                new CodeSample(
3✔
89
                    '<?php
3✔
90
class Foo
91
{
92
    public int   $bar;
93

94
    public function baz(string     $a) {}
95
}
96
',
3✔
97
                    ['elements' => ['property']]
3✔
98
                ),
3✔
99
                new VersionSpecificCodeSample(
3✔
100
                    '<?php
3✔
101
class Foo
102
{
103
    public  const string   BAR = "";
104
}
105
',
3✔
106
                    new VersionSpecification(8_03_00),
3✔
107
                    ['elements' => ['constant']]
3✔
108
                ),
3✔
109
            ]
3✔
110
        );
3✔
111
    }
112

113
    public function isCandidate(Tokens $tokens): bool
114
    {
115
        return $tokens->isAnyTokenKindsFound([...Token::getClassyTokenKinds(), \T_FN, \T_FUNCTION]);
90✔
116
    }
117

118
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
119
    {
120
        return new FixerConfigurationResolver([
99✔
121
            (new FixerOptionBuilder('elements', 'Structural elements where the spacing after the type declaration should be fixed.'))
99✔
122
                ->setAllowedTypes(['string[]'])
99✔
123
                ->setAllowedValues([new AllowedValueSubset(['function', 'property', 'constant'])])
99✔
124
                ->setDefault(['function', 'property']) // @TODO add 'constant' on next major 4.0
99✔
125
                ->getOption(),
99✔
126
        ]);
99✔
127
    }
128

129
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
130
    {
131
        $functionsAnalyzer = new FunctionsAnalyzer();
88✔
132

133
        foreach (array_reverse($this->getElements($tokens), true) as $index => $type) {
88✔
134
            if ('property' === $type && \in_array('property', $this->configuration['elements'], true)) {
88✔
135
                $this->ensureSingleSpaceAtPropertyTypehint($tokens, $index);
20✔
136

137
                continue;
20✔
138
            }
139

140
            if ('method' === $type && \in_array('function', $this->configuration['elements'], true)) {
71✔
141
                $this->ensureSingleSpaceAtFunctionArgumentTypehint($functionsAnalyzer, $tokens, $index);
51✔
142

143
                continue;
51✔
144
            }
145

146
            if ('const' === $type && \in_array('constant', $this->configuration['elements'], true)) {
22✔
147
                $this->ensureSingleSpaceAtConstantTypehint($tokens, $index);
22✔
148

149
                // implicit continue;
150
            }
151
        }
152
    }
153

154
    /**
155
     * @return array<int, string>
156
     *
157
     * @phpstan-return array<int, 'method'|'property'|'const'>
158
     */
159
    private function getElements(Tokens $tokens): array
160
    {
161
        $tokensAnalyzer = new TokensAnalyzer($tokens);
88✔
162

163
        $elements = array_map(
88✔
164
            static fn (array $element): string => $element['type'],
88✔
165
            array_filter(
88✔
166
                $tokensAnalyzer->getClassyElements(),
88✔
167
                static fn (array $element): bool => \in_array($element['type'], ['method', 'property', 'const'], true)
88✔
168
            )
88✔
169
        );
88✔
170

171
        foreach ($tokens as $index => $token) {
88✔
172
            if (
173
                $token->isGivenKind(\T_FN)
88✔
174
                || ($token->isGivenKind(\T_FUNCTION) && !isset($elements[$index]))
88✔
175
            ) {
176
                $elements[$index] = 'method';
47✔
177
            }
178
        }
179

180
        return $elements;
88✔
181
    }
182

183
    private function ensureSingleSpaceAtFunctionArgumentTypehint(FunctionsAnalyzer $functionsAnalyzer, Tokens $tokens, int $index): void
184
    {
185
        foreach (array_reverse($functionsAnalyzer->getFunctionArguments($tokens, $index)) as $argumentInfo) {
51✔
186
            $argumentType = $argumentInfo->getTypeAnalysis();
50✔
187

188
            if (null === $argumentType) {
50✔
189
                continue;
11✔
190
            }
191

192
            $tokens->ensureWhitespaceAtIndex($argumentType->getEndIndex() + 1, 0, ' ');
43✔
193
        }
194
    }
195

196
    private function ensureSingleSpaceAtPropertyTypehint(Tokens $tokens, int $index): void
197
    {
198
        $propertyIndex = $index;
20✔
199

200
        do {
201
            $index = $tokens->getPrevMeaningfulToken($index);
20✔
202
        } while (!$tokens[$index]->isGivenKind(self::PROPERTY_MODIFIERS));
20✔
203

204
        $propertyType = $this->collectTypeAnalysis($tokens, $index, $propertyIndex);
20✔
205

206
        if (null === $propertyType) {
20✔
207
            return;
3✔
208
        }
209

210
        $tokens->ensureWhitespaceAtIndex($propertyType->getEndIndex() + 1, 0, ' ');
17✔
211
    }
212

213
    private function ensureSingleSpaceAtConstantTypehint(Tokens $tokens, int $index): void
214
    {
215
        $constIndex = $index;
22✔
216
        $equalsIndex = $tokens->getNextTokenOfKind($constIndex, ['=']);
22✔
217

218
        if (null === $equalsIndex) {
22✔
219
            return;
×
220
        }
221

222
        $nameIndex = $tokens->getPrevMeaningfulToken($equalsIndex);
22✔
223

224
        if (!$tokens[$nameIndex]->isGivenKind(\T_STRING)) {
22✔
225
            return;
×
226
        }
227

228
        $typeEndIndex = $tokens->getPrevMeaningfulToken($nameIndex);
22✔
229

230
        if (null === $typeEndIndex || $tokens[$typeEndIndex]->isGivenKind(\T_CONST)) {
22✔
231
            return;
5✔
232
        }
233

234
        $tokens->ensureWhitespaceAtIndex($typeEndIndex + 1, 0, ' ');
17✔
235
    }
236

237
    private function collectTypeAnalysis(Tokens $tokens, int $startIndex, int $endIndex): ?TypeAnalysis
238
    {
239
        $type = '';
20✔
240
        $typeStartIndex = $tokens->getNextMeaningfulToken($startIndex);
20✔
241
        $typeEndIndex = $typeStartIndex;
20✔
242

243
        for ($i = $typeStartIndex; $i < $endIndex; ++$i) {
20✔
244
            if ($tokens[$i]->isWhitespace() || $tokens[$i]->isComment()) {
17✔
245
                continue;
17✔
246
            }
247

248
            $type .= $tokens[$i]->getContent();
17✔
249
            $typeEndIndex = $i;
17✔
250
        }
251

252
        return '' !== $type ? new TypeAnalysis($type, $typeStartIndex, $typeEndIndex) : null;
20✔
253
    }
254
}
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