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

j-schumann / symfony-addons / 15652790256

14 Jun 2025 02:02PM UTC coverage: 54.65% (-0.6%) from 55.22%
15652790256

push

github

j-schumann
fix: CS

0 of 18 new or added lines in 1 file covered. (0.0%)

3 existing lines in 1 file now uncovered.

476 of 871 relevant lines covered (54.65%)

3.48 hits per line

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

0.0
/src/PhpCsFixer/WrapNamedMethodArgumentsFixer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Vrok\SymfonyAddons\PhpCsFixer;
6

7
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
8
use PhpCsFixer\Fixer\FixerInterface;
9
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
10
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
11
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
12
use PhpCsFixer\FixerDefinition\CodeSample;
13
use PhpCsFixer\FixerDefinition\FixerDefinition;
14
use PhpCsFixer\Tokenizer\Token;
15
use PhpCsFixer\Tokenizer\Tokens;
16

17
final class WrapNamedMethodArgumentsFixer implements FixerInterface, ConfigurableFixerInterface
18
{
19
    private const int DEFAULT_MAX_ARGUMENTS = 3;
20
    private const array NESTING_OPEN_TOKENS = ['(', '[', '{'];
21
    private const array NESTING_CLOSE_TOKENS = [')', ']', '}'];
22

23
    private int $maxArguments = self::DEFAULT_MAX_ARGUMENTS;
24

25
    public function getDefinition(): FixerDefinition
26
    {
27
        return new FixerDefinition(
×
28
            'Wrap method arguments to separate lines when they are named and exceed the maximum argument count (default 3).',
×
29
            [
×
30
                new CodeSample(
×
31
                    '<?php
×
32
$this->method(arg1: $value1, arg2: $value2, arg3: $value3);
33
// will be changed to:
34
$this->method(
35
    arg1: $value1,
36
    arg2: $value2,
37
    arg3: $value3
38
);
39

40
// will stay unchanged:
41
$this->method(arg1: $value1, arg2: $value2);',
×
42
                    ['max_arguments' => 2]
×
43
                ),
×
44
            ]
×
45
        );
×
46
    }
47

48
    public function getName(): string
49
    {
50
        return 'VrokSymfonyAddons/wrap_named_method_arguments';
×
51
    }
52

53
    public function getPriority(): int
54
    {
55
        return 100;
×
56
    }
57

58
    public function supports(\SplFileInfo $file): bool
59
    {
60
        return true;
×
61
    }
62

63
    /**
64
     * @param Tokens<Token> $tokens
65
     */
66
    public function isCandidate(Tokens $tokens): bool
67
    {
68
        return $tokens->isTokenKindFound(T_STRING);
×
69
    }
70

71
    public function isRisky(): bool
72
    {
73
        return false;
×
74
    }
75

76
    public function getConfigurationDefinition(): FixerConfigurationResolverInterface
77
    {
78
        return new FixerConfigurationResolver([
×
NEW
79
            (new FixerOptionBuilder(
×
NEW
80
                'max_arguments',
×
NEW
81
                'Maximum number of arguments before formatting is applied.'
×
NEW
82
            ))
×
83
                ->setAllowedTypes(['int'])
×
84
                ->setDefault(self::DEFAULT_MAX_ARGUMENTS)
×
85
                ->getOption(),
×
86
        ]);
×
87
    }
88

89
    public function configure(array $configuration): void
90
    {
91
        $this->maxArguments = $configuration['max_arguments'] ?? self::DEFAULT_MAX_ARGUMENTS;
×
92
    }
93

94
    /**
95
     * @param Tokens<Token> $tokens
96
     */
97
    public function fix(\SplFileInfo $file, Tokens $tokens): void
98
    {
99
        for ($i = 0, $tokenCount = $tokens->count(); $i < $tokenCount; ++$i) {
×
100
            if (!$tokens[$i]->isGivenKind(T_STRING)) {
×
101
                continue;
×
102
            }
103

104
            $openParenIndex = $tokens->getNextMeaningfulToken($i);
×
105
            if (
NEW
106
                null === $openParenIndex
×
NEW
107
                || !$tokens[$openParenIndex]->equals('(')
×
108
            ) {
UNCOV
109
                continue;
×
110
            }
111

NEW
112
            $closeParenIndex = $tokens->findBlockEnd(
×
NEW
113
                Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
×
NEW
114
                $openParenIndex
×
NEW
115
            );
×
116

117
            if ($this->shouldFormatMethodCall($tokens, $openParenIndex, $closeParenIndex)) {
×
118
                $indentation = $this->detectIndentation($tokens, $i);
×
119
                $this->formatMethodCall($tokens, $openParenIndex, $closeParenIndex, $indentation);
×
120
            }
121
        }
122
    }
123

124
    /**
125
     * @param Tokens<Token> $tokens
126
     */
127
    private function shouldFormatMethodCall(
128
        Tokens $tokens,
129
        int $openParenIndex,
130
        int $closeParenIndex,
131
    ): bool {
UNCOV
132
        $analysisResult = $this->analyzeArguments($tokens, $openParenIndex, $closeParenIndex);
×
133

134
        return $analysisResult['hasNamedArgs'] && $analysisResult['argumentCount'] > $this->maxArguments;
×
135
    }
136

137
    /**
138
     * @param Tokens<Token> $tokens
139
     */
140
    private function detectIndentation(Tokens $tokens, int $functionNameIndex): array
141
    {
142
        // Find the start of the line containing the function call
143
        $lineStartIndex = $functionNameIndex;
×
144
        while ($lineStartIndex > 0) {
×
145
            $prevIndex = $lineStartIndex - 1;
×
146
            if (
NEW
147
                $tokens[$prevIndex]->isWhitespace()
×
NEW
148
                && str_contains($tokens[$prevIndex]->getContent(), "\n")
×
149
            ) {
UNCOV
150
                break;
×
151
            }
152
            --$lineStartIndex;
×
153
        }
154

155
        // Detect current line indentation
156
        $baseIndent = '';
×
157
        if ($lineStartIndex > 0 && $tokens[$lineStartIndex]->isWhitespace()) {
×
158
            $whitespace = $tokens[$lineStartIndex]->getContent();
×
159
            $lines = explode("\n", $whitespace);
×
160
            $baseIndent = end($lines); // Get indentation after the last newline
×
161
        }
162

163
        // Detect indentation unit (try to find consistent indentation in the file)
164
        $indentUnit = $this->detectIndentationUnit($tokens);
×
165

166
        return [
×
167
            'base'     => $baseIndent,
×
168
            'unit'     => $indentUnit,
×
169
            'argument' => $baseIndent.$indentUnit,
×
170
        ];
×
171
    }
172

173
    /**
174
     * @param Tokens<Token> $tokens
175
     */
176
    private function detectIndentationUnit(Tokens $tokens): string
177
    {
178
        $indentations = [];
×
179

180
        // Sample some whitespace tokens to detect indentation pattern
181
        for ($i = 0, $count = min(100, $tokens->count()); $i < $count; ++$i) {
×
182
            if (!$tokens[$i]->isWhitespace()) {
×
183
                continue;
×
184
            }
185

186
            $content = $tokens[$i]->getContent();
×
NEW
187
            if (!str_contains($content, "\n")) {
×
188
                continue;
×
189
            }
190

191
            $lines = explode("\n", $content);
×
192
            foreach ($lines as $line) {
×
193
                if ('' === $line) {
×
194
                    continue;
×
195
                }
196

197
                // Count leading spaces/tabs
198
                $indent = '';
×
NEW
199
                $len = \strlen($line);
×
NEW
200
                for ($j = 0; $j < $len; ++$j) {
×
201
                    if (' ' === $line[$j] || "\t" === $line[$j]) {
×
202
                        $indent .= $line[$j];
×
203
                    } else {
204
                        break;
×
205
                    }
206
                }
207

208
                if ('' !== $indent) {
×
209
                    $indentations[] = $indent;
×
210
                }
211
            }
212
        }
213

214
        // Analyze indentations to find the unit
NEW
215
        if ([] === $indentations) {
×
216
            return '    '; // Default to 4 spaces
×
217
        }
218

219
        // Check if using tabs
220
        foreach ($indentations as $indent) {
×
221
            if (str_contains($indent, "\t")) {
×
222
                return "\t";
×
223
            }
224
        }
225

226
        // Count spaces - find the smallest non-zero indentation
227
        $spaceCounts = array_map('strlen', $indentations);
×
228
        $spaceCounts = array_filter($spaceCounts, static fn ($count) => $count > 0);
×
229

NEW
230
        if ([] === $spaceCounts) {
×
231
            return '    '; // Default to 4 spaces
×
232
        }
233

234
        $minSpaces = min($spaceCounts);
×
235

236
        return str_repeat(' ', $minSpaces);
×
237
    }
238

239
    /**
240
     * @param Tokens<Token> $tokens
241
     */
242
    private function analyzeArguments(
243
        Tokens $tokens,
244
        int $openParenIndex,
245
        int $closeParenIndex,
246
    ): array {
247
        $topLevelCommas = [];
×
248
        $nestingLevel = 0;
×
249
        $hasContent = false;
×
250
        $hasNamedArgs = false;
×
251

252
        for ($i = $openParenIndex + 1; $i < $closeParenIndex; ++$i) {
×
253
            $token = $tokens[$i];
×
254
            $content = $token->getContent();
×
255

256
            if (\in_array($content, self::NESTING_OPEN_TOKENS, true)) {
×
257
                ++$nestingLevel;
×
258
            } elseif (\in_array($content, self::NESTING_CLOSE_TOKENS, true)) {
×
259
                --$nestingLevel;
×
260
            } elseif (',' === $content && 0 === $nestingLevel) {
×
261
                $topLevelCommas[] = $i;
×
262
            } elseif (':' === $content) {
×
263
                $hasNamedArgs = true;
×
264
            }
265

266
            if (!$token->isWhitespace()) {
×
267
                $hasContent = true;
×
268
            }
269
        }
270

271
        return [
×
272
            'argumentCount'  => $hasContent ? \count($topLevelCommas) + 1 : 0,
×
273
            'hasNamedArgs'   => $hasNamedArgs,
×
274
            'topLevelCommas' => $topLevelCommas,
×
275
        ];
×
276
    }
277

278
    /**
279
     * @param Tokens<Token> $tokens
280
     */
281
    private function formatMethodCall(Tokens $tokens, int $openParenIndex, int $closeParenIndex, array $indentation): void
282
    {
283
        $analysisResult = $this->analyzeArguments($tokens, $openParenIndex, $closeParenIndex);
×
284
        $topLevelCommas = $analysisResult['topLevelCommas'];
×
285

286
        // Work backwards to avoid index shifts
287
        $this->addNewlineBeforeClosingParenthesis($tokens, $closeParenIndex, $indentation['base']);
×
288
        $this->addNewlinesAfterCommas($tokens, $topLevelCommas, $indentation['argument']);
×
289
        $this->addNewlineAfterOpeningParenthesis($tokens, $openParenIndex, $indentation['argument']);
×
290
    }
291

292
    /**
293
     * @param Tokens<Token> $tokens
294
     */
295
    private function addNewlineBeforeClosingParenthesis(Tokens $tokens, int $closeParenIndex, string $baseIndent): void
296
    {
297
        $prevIndex = $tokens->getPrevMeaningfulToken($closeParenIndex);
×
298
        if (null === $prevIndex) {
×
299
            return;
×
300
        }
301

302
        if ($prevIndex + 1 === $closeParenIndex) {
×
303
            $tokens->insertAt($closeParenIndex, new Token([T_WHITESPACE, "\n".$baseIndent]));
×
304
        } else {
305
            $this->replaceWhitespaceWithNewline($tokens, $prevIndex + 1, $baseIndent);
×
306
        }
307
    }
308

309
    /**
310
     * @param Tokens<Token> $tokens
311
     */
312
    private function addNewlinesAfterCommas(Tokens $tokens, array $topLevelCommas, string $argumentIndent): void
313
    {
314
        foreach (array_reverse($topLevelCommas) as $commaIndex) {
×
315
            $nextTokenIndex = $commaIndex + 1;
×
316
            if ($nextTokenIndex < \count($tokens) && $tokens[$nextTokenIndex]->isWhitespace()) {
×
317
                $tokens[$nextTokenIndex] = new Token([T_WHITESPACE, "\n".$argumentIndent]);
×
318
            } else {
319
                $tokens->insertAt($commaIndex + 1, new Token([T_WHITESPACE, "\n".$argumentIndent]));
×
320
            }
321
        }
322
    }
323

324
    /**
325
     * @param Tokens<Token> $tokens
326
     */
327
    private function addNewlineAfterOpeningParenthesis(Tokens $tokens, int $openParenIndex, string $argumentIndent): void
328
    {
329
        $nextTokenIndex = $openParenIndex + 1;
×
330
        if ($nextTokenIndex < \count($tokens) && $tokens[$nextTokenIndex]->isWhitespace()) {
×
331
            $tokens[$nextTokenIndex] = new Token([T_WHITESPACE, "\n".$argumentIndent]);
×
332
        } else {
333
            $tokens->insertAt($openParenIndex + 1, new Token([T_WHITESPACE, "\n".$argumentIndent]));
×
334
        }
335
    }
336

337
    /**
338
     * @param Tokens<Token> $tokens
339
     */
340
    private function replaceWhitespaceWithNewline(Tokens $tokens, int $whitespaceIndex, string $indent): void
341
    {
342
        if ($tokens[$whitespaceIndex]->isWhitespace()) {
×
343
            $content = $tokens[$whitespaceIndex]->getContent();
×
NEW
344
            if (!str_contains($content, "\n")) {
×
345
                $tokens[$whitespaceIndex] = new Token([T_WHITESPACE, "\n".$indent]);
×
346
            }
347
        }
348
    }
349
}
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