• 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

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
                        <?php
66
                        class Bar
67
                        {
68
                            private string    $a;
69
                            private bool   $b;
70

71
                            public function __invoke(array   $c) {}
72
                        }
73

74
                        PHP
3✔
75
                ),
3✔
76
                new CodeSample(
3✔
77
                    <<<'PHP'
3✔
78
                        <?php
79
                        class Foo
80
                        {
81
                            public int   $bar;
82

83
                            public function baz(string     $a)
84
                            {
85
                                return fn(bool    $c): string => (string) $c;
86
                            }
87
                        }
88

89
                        PHP,
3✔
90
                    ['elements' => ['function']]
3✔
91
                ),
3✔
92
                new CodeSample(
3✔
93
                    <<<'PHP'
3✔
94
                        <?php
95
                        class Foo
96
                        {
97
                            public int   $bar;
98

99
                            public function baz(string     $a) {}
100
                        }
101

102
                        PHP,
3✔
103
                    ['elements' => ['property']]
3✔
104
                ),
3✔
105
                new VersionSpecificCodeSample(
3✔
106
                    <<<'PHP'
3✔
107
                        <?php
108
                        class Foo
109
                        {
110
                            public  const string   BAR = "";
111
                        }
112

113
                        PHP,
3✔
114
                    new VersionSpecification(8_03_00),
3✔
115
                    ['elements' => ['constant']]
3✔
116
                ),
3✔
117
            ]
3✔
118
        );
3✔
119
    }
120

121
    public function isCandidate(Tokens $tokens): bool
122
    {
123
        return $tokens->isAnyTokenKindsFound([...Token::getClassyTokenKinds(), \T_FN, \T_FUNCTION]);
91✔
124
    }
125

126
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
127
    {
128
        return new FixerConfigurationResolver([
100✔
129
            (new FixerOptionBuilder('elements', 'Structural elements where the spacing after the type declaration should be fixed.'))
100✔
130
                ->setAllowedTypes(['string[]'])
100✔
131
                ->setAllowedValues([new AllowedValueSubset(['function', 'property', 'constant'])])
100✔
132
                ->setDefault(['function', 'property']) // @TODO add 'constant' on next major 4.0
100✔
133
                ->getOption(),
100✔
134
        ]);
100✔
135
    }
136

137
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
138
    {
139
        $functionsAnalyzer = new FunctionsAnalyzer();
89✔
140

141
        foreach (array_reverse($this->getElements($tokens), true) as $index => $type) {
89✔
142
            if ('property' === $type && \in_array('property', $this->configuration['elements'], true)) {
89✔
143
                $this->ensureSingleSpaceAtPropertyTypehint($tokens, $index);
21✔
144

145
                continue;
21✔
146
            }
147

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

151
                continue;
51✔
152
            }
153

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

157
                // implicit continue;
158
            }
159
        }
160
    }
161

162
    /**
163
     * @return array<int, string>
164
     *
165
     * @phpstan-return array<int, 'method'|'property'|'const'>
166
     */
167
    private function getElements(Tokens $tokens): array
168
    {
169
        $tokensAnalyzer = new TokensAnalyzer($tokens);
89✔
170

171
        $elements = array_map(
89✔
172
            static fn (array $element): string => $element['type'],
89✔
173
            array_filter(
89✔
174
                $tokensAnalyzer->getClassyElements(),
89✔
175
                static fn (array $element): bool => \in_array($element['type'], ['method', 'property', 'const'], true)
89✔
176
            )
89✔
177
        );
89✔
178

179
        foreach ($tokens as $index => $token) {
89✔
180
            if (
181
                $token->isGivenKind(\T_FN)
89✔
182
                || ($token->isGivenKind(\T_FUNCTION) && !isset($elements[$index]))
89✔
183
            ) {
184
                $elements[$index] = 'method';
47✔
185
            }
186
        }
187

188
        return $elements;
89✔
189
    }
190

191
    private function ensureSingleSpaceAtFunctionArgumentTypehint(FunctionsAnalyzer $functionsAnalyzer, Tokens $tokens, int $index): void
192
    {
193
        foreach (array_reverse($functionsAnalyzer->getFunctionArguments($tokens, $index)) as $argumentInfo) {
51✔
194
            $argumentType = $argumentInfo->getTypeAnalysis();
50✔
195

196
            if (null === $argumentType) {
50✔
197
                continue;
11✔
198
            }
199

200
            $tokens->ensureWhitespaceAtIndex($argumentType->getEndIndex() + 1, 0, ' ');
43✔
201
        }
202
    }
203

204
    private function ensureSingleSpaceAtPropertyTypehint(Tokens $tokens, int $index): void
205
    {
206
        $propertyIndex = $index;
21✔
207

208
        do {
209
            $index = $tokens->getPrevMeaningfulToken($index);
21✔
210
        } while (!$tokens[$index]->isGivenKind(self::PROPERTY_MODIFIERS));
21✔
211

212
        $propertyType = $this->collectTypeAnalysis($tokens, $index, $propertyIndex);
21✔
213

214
        if (null === $propertyType) {
21✔
215
            return;
3✔
216
        }
217

218
        $tokens->ensureWhitespaceAtIndex($propertyType->getEndIndex() + 1, 0, ' ');
18✔
219
    }
220

221
    private function ensureSingleSpaceAtConstantTypehint(Tokens $tokens, int $index): void
222
    {
223
        $constIndex = $index;
22✔
224
        $equalsIndex = $tokens->getNextTokenOfKind($constIndex, ['=']);
22✔
225

226
        if (null === $equalsIndex) {
22✔
UNCOV
227
            return;
×
228
        }
229

230
        $nameIndex = $tokens->getPrevMeaningfulToken($equalsIndex);
22✔
231

232
        if (!$tokens[$nameIndex]->isGivenKind(\T_STRING)) {
22✔
UNCOV
233
            return;
×
234
        }
235

236
        $typeEndIndex = $tokens->getPrevMeaningfulToken($nameIndex);
22✔
237

238
        if (null === $typeEndIndex || $tokens[$typeEndIndex]->isGivenKind(\T_CONST)) {
22✔
239
            return;
5✔
240
        }
241

242
        $tokens->ensureWhitespaceAtIndex($typeEndIndex + 1, 0, ' ');
17✔
243
    }
244

245
    private function collectTypeAnalysis(Tokens $tokens, int $startIndex, int $endIndex): ?TypeAnalysis
246
    {
247
        $type = '';
21✔
248
        $typeStartIndex = $tokens->getNextMeaningfulToken($startIndex);
21✔
249
        $typeEndIndex = $typeStartIndex;
21✔
250

251
        for ($i = $typeStartIndex; $i < $endIndex; ++$i) {
21✔
252
            if ($tokens[$i]->isWhitespace() || $tokens[$i]->isComment()) {
18✔
253
                continue;
18✔
254
            }
255

256
            $type .= $tokens[$i]->getContent();
18✔
257
            $typeEndIndex = $i;
18✔
258
        }
259

260
        return '' !== $type ? new TypeAnalysis($type, $typeStartIndex, $typeEndIndex) : null;
21✔
261
    }
262
}
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