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

PHP-CS-Fixer / PHP-CS-Fixer / 14626845176

23 Apr 2025 07:50PM UTC coverage: 94.896%. Remained the same
14626845176

push

github

web-flow
feat: introduce `MultilinePromotedPropertiesFixer` (#8595)

79 of 80 new or added lines in 2 files covered. (98.75%)

3 existing lines in 1 file now uncovered.

28464 of 29995 relevant lines covered (94.9%)

43.09 hits per line

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

98.73
/src/Fixer/FunctionNotation/MultilinePromotedPropertiesFixer.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\FunctionNotation;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
19
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
20
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
21
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
22
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
23
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
24
use PhpCsFixer\FixerDefinition\FixerDefinition;
25
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
26
use PhpCsFixer\FixerDefinition\VersionSpecification;
27
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
28
use PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer;
29
use PhpCsFixer\Tokenizer\CT;
30
use PhpCsFixer\Tokenizer\Tokens;
31
use PhpCsFixer\Tokenizer\TokensAnalyzer;
32
use PhpCsFixerCustomFixers\Analyzer\Analysis\ConstructorAnalysis;
33

34
/**
35
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
36
 *
37
 * @phpstan-type _InputConfig array{keep_blank_lines?: bool, minimum_number_of_parameters?: int}
38
 * @phpstan-type _Config array{keep_blank_lines: bool, minimum_number_of_parameters: int}
39
 * @phpstan-type _AutogeneratedInputConfiguration array{
40
 *  keep_blank_lines?: bool,
41
 *  minimum_number_of_parameters?: int,
42
 * }
43
 * @phpstan-type _AutogeneratedComputedConfiguration array{
44
 *  keep_blank_lines: bool,
45
 *  minimum_number_of_parameters: int,
46
 * }
47
 * @phpstan-type _ConstructorAnalysis array{
48
 *   index?: int,
49
 *   parameter_names: list<string>,
50
 *   promotable_parameters: array<int, string>,
51
 *   constructor_index?: int,
52
 *  }
53
 */
54
final class MultilinePromotedPropertiesFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
55
{
56
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
57
    use ConfigurableFixerTrait;
58

59
    public function getDefinition(): FixerDefinitionInterface
60
    {
61
        return new FixerDefinition(
3✔
62
            'Promoted properties must be on separate lines.',
3✔
63
            [
3✔
64
                new VersionSpecificCodeSample(
3✔
65
                    <<<'PHP'
3✔
66
                        <?php
67
                        class Foo {
68
                            public function __construct(private array $a, private bool $b, private int $i) {}
69
                        }
70

71
                        PHP,
3✔
72
                    new VersionSpecification(80_000),
3✔
73
                ),
3✔
74
                new VersionSpecificCodeSample(
3✔
75
                    <<<'PHP'
3✔
76
                        <?php
77
                        class Foo {
78
                            public function __construct(private array $a, private bool $b, private int $i) {}
79
                        }
80
                        class Bar {
81
                            public function __construct(private array $x) {}
82
                        }
83

84
                        PHP,
3✔
85
                    new VersionSpecification(80_000),
3✔
86
                    ['minimum_number_of_parameters' => 3]
3✔
87
                ),
3✔
88
            ],
3✔
89
        );
3✔
90
    }
91

92
    /**
93
     * {@inheritdoc}
94
     *
95
     * Must run before TrailingCommaInMultilineFixer.
96
     */
97
    public function getPriority(): int
98
    {
99
        return 1;
1✔
100
    }
101

102
    public function isCandidate(Tokens $tokens): bool
103
    {
104
        return $tokens->isAnyTokenKindsFound([
26✔
105
            CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
26✔
106
            CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
26✔
107
            CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
26✔
108
        ]);
26✔
109
    }
110

111
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
112
    {
113
        return new FixerConfigurationResolver([
35✔
114
            (new FixerOptionBuilder('keep_blank_lines', 'Whether to keep blank lines between properties.'))
35✔
115
                ->setAllowedTypes(['bool'])
35✔
116
                ->setDefault(false)
35✔
117
                ->getOption(),
35✔
118
            (new FixerOptionBuilder('minimum_number_of_parameters', 'Minimum number of parameters in the constructor to fix.'))
35✔
119
                ->setAllowedTypes(['int'])
35✔
120
                ->setDefault(1)
35✔
121
                ->getOption(),
35✔
122
        ]);
35✔
123
    }
124

125
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
126
    {
127
        $tokensAnalyzer = new TokensAnalyzer($tokens);
21✔
128
        $methodArgumentSpaceFixer = new MethodArgumentSpaceFixer();
21✔
129

130
        foreach ($tokensAnalyzer->getClassyElements() as $index => $element) {
21✔
131
            if ('method' !== $element['type']) {
21✔
NEW
132
                continue;
×
133
            }
134

135
            $openParenthesisIndex = $tokens->getNextTokenOfKind($index, ['(']);
21✔
136
            $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);
21✔
137

138
            if (!$this->shouldBeFixed($tokens, $openParenthesisIndex, $closeParenthesisIndex)) {
21✔
139
                continue;
6✔
140
            }
141

142
            $this->fixParameters($tokens, $openParenthesisIndex, $closeParenthesisIndex);
17✔
143
        }
144
    }
145

146
    private function shouldBeFixed(Tokens $tokens, int $openParenthesisIndex, int $closeParenthesisIndex): bool
147
    {
148
        $promotedParameterFound = false;
21✔
149
        $minimumNumberOfParameters = 0;
21✔
150
        for ($index = $openParenthesisIndex + 1; $index < $closeParenthesisIndex; ++$index) {
21✔
151
            if ($tokens[$index]->isGivenKind(T_VARIABLE)) {
21✔
152
                ++$minimumNumberOfParameters;
21✔
153
            }
154
            if (
155
                $tokens[$index]->isGivenKind([
21✔
156
                    CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
21✔
157
                    CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
21✔
158
                    CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
21✔
159
                ])
21✔
160
            ) {
161
                $promotedParameterFound = true;
21✔
162
            }
163
        }
164

165
        return $promotedParameterFound && $minimumNumberOfParameters >= $this->configuration['minimum_number_of_parameters'];
21✔
166
    }
167

168
    private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeParenthesis): void
169
    {
170
        $indent = WhitespacesAnalyzer::detectIndent($tokens, $openParenthesis);
17✔
171

172
        $tokens->ensureWhitespaceAtIndex(
17✔
173
            $closeParenthesis - 1,
17✔
174
            1,
17✔
175
            $this->whitespacesConfig->getLineEnding().$indent,
17✔
176
        );
17✔
177

178
        $index = $tokens->getPrevMeaningfulToken($closeParenthesis);
17✔
179
        \assert(\is_int($index));
17✔
180

181
        while ($index > $openParenthesis) {
17✔
182
            $index = $tokens->getPrevMeaningfulToken($index);
17✔
183
            \assert(\is_int($index));
17✔
184

185
            $blockType = Tokens::detectBlockType($tokens[$index]);
17✔
186
            if (null !== $blockType && !$blockType['isStart']) {
17✔
187
                $index = $tokens->findBlockStart($blockType['type'], $index);
2✔
188

189
                continue;
2✔
190
            }
191

192
            if (!$tokens[$index]->equalsAny(['(', ','])) {
17✔
193
                continue;
17✔
194
            }
195

196
            $this->fixParameter($tokens, $index + 1, $indent);
17✔
197
        }
198
    }
199

200
    private function fixParameter(Tokens $tokens, int $index, string $indent): void
201
    {
202
        if ($this->configuration['keep_blank_lines'] && $tokens[$index]->isWhitespace() && str_contains($tokens[$index]->getContent(), "\n")) {
17✔
203
            return;
1✔
204
        }
205

206
        $tokens->ensureWhitespaceAtIndex(
17✔
207
            $index,
17✔
208
            0,
17✔
209
            $this->whitespacesConfig->getLineEnding().$indent.$this->whitespacesConfig->getIndent(),
17✔
210
        );
17✔
211
    }
212
}
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