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

j-schumann / symfony-addons / 15653521406

14 Jun 2025 03:31PM UTC coverage: 53.664% (-0.9%) from 54.525%
15653521406

push

github

j-schumann
fix: WrapNamedMethodArgumentsFixer

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

476 of 887 relevant lines covered (53.66%)

3.42 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([
×
79
            (new FixerOptionBuilder(
×
80
                'max_arguments',
×
81
                'Maximum number of arguments before formatting is applied.'
×
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 (
106
                null === $openParenIndex
×
107
                || !$tokens[$openParenIndex]->equals('(')
×
108
            ) {
109
                continue;
×
110
            }
111

112
            $closeParenIndex = $tokens->findBlockEnd(
×
113
                Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
×
114
                $openParenIndex
×
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 {
132
        $analysisResult = $this->analyzeArguments($tokens, $openParenIndex, $closeParenIndex);
×
133

134
        // Only format if we have named args and exceed the threshold
NEW
135
        if (!$analysisResult['hasNamedArgs'] || $analysisResult['argumentCount'] <= $this->maxArguments) {
×
NEW
136
            return false;
×
137
        }
138

139
        // Check if arguments are already on separate lines
NEW
140
        if ($this->areArgumentsAlreadyFormatted($tokens, $openParenIndex, $closeParenIndex, $analysisResult['topLevelCommas'])) {
×
NEW
141
            return false;
×
142
        }
143

NEW
144
        return true;
×
145
    }
146

147
    private function areArgumentsAlreadyFormatted(Tokens $tokens, int $openParenIndex, int $closeParenIndex, array $topLevelCommas): bool
148
    {
149
        // Check if there's a newline after the opening parenthesis
NEW
150
        $nextIndex = $openParenIndex + 1;
×
NEW
151
        if ($nextIndex < $closeParenIndex && $tokens[$nextIndex]->isWhitespace()) {
×
NEW
152
            if (false !== strpos($tokens[$nextIndex]->getContent(), "\n")) {
×
153
                // There's a newline after opening paren, likely already formatted
NEW
154
                return true;
×
155
            }
156
        }
157

158
        // Check if there are newlines after commas
NEW
159
        foreach ($topLevelCommas as $commaIndex) {
×
NEW
160
            $nextIndex = $commaIndex + 1;
×
NEW
161
            if ($nextIndex < $closeParenIndex && $tokens[$nextIndex]->isWhitespace()) {
×
NEW
162
                if (false !== strpos($tokens[$nextIndex]->getContent(), "\n")) {
×
163
                    // Found newline after comma, likely already formatted
NEW
164
                    return true;
×
165
                }
166
            }
167
        }
168

NEW
169
        return false;
×
170
    }
171

172
    /**
173
     * @param Tokens<Token> $tokens
174
     */
175
    private function detectIndentation(Tokens $tokens, int $functionNameIndex): array
176
    {
177
        // Find the start of the line containing the function call
178
        $lineStartIndex = $functionNameIndex;
×
179
        while ($lineStartIndex > 0) {
×
180
            $prevIndex = $lineStartIndex - 1;
×
181
            if (
182
                $tokens[$prevIndex]->isWhitespace()
×
183
                && str_contains($tokens[$prevIndex]->getContent(), "\n")
×
184
            ) {
185
                break;
×
186
            }
187
            --$lineStartIndex;
×
188
        }
189

190
        // Detect current line indentation
191
        $baseIndent = '';
×
192
        if ($lineStartIndex > 0 && $tokens[$lineStartIndex]->isWhitespace()) {
×
193
            $whitespace = $tokens[$lineStartIndex]->getContent();
×
194
            $lines = explode("\n", $whitespace);
×
195
            $baseIndent = end($lines); // Get indentation after the last newline
×
196
        }
197

198
        // Detect indentation unit (try to find consistent indentation in the file)
199
        $indentUnit = $this->detectIndentationUnit($tokens);
×
200

201
        return [
×
202
            'base'     => $baseIndent,
×
203
            'unit'     => $indentUnit,
×
204
            'argument' => $baseIndent.$indentUnit,
×
205
        ];
×
206
    }
207

208
    /**
209
     * @param Tokens<Token> $tokens
210
     */
211
    private function detectIndentationUnit(Tokens $tokens): string
212
    {
213
        $indentations = [];
×
214

215
        // Sample some whitespace tokens to detect indentation pattern
216
        for ($i = 0, $count = min(100, $tokens->count()); $i < $count; ++$i) {
×
217
            if (!$tokens[$i]->isWhitespace()) {
×
218
                continue;
×
219
            }
220

221
            $content = $tokens[$i]->getContent();
×
222
            if (!str_contains($content, "\n")) {
×
223
                continue;
×
224
            }
225

226
            $lines = explode("\n", $content);
×
227
            foreach ($lines as $line) {
×
228
                if ('' === $line) {
×
229
                    continue;
×
230
                }
231

232
                // Count leading spaces/tabs
233
                $indent = '';
×
234
                $len = \strlen($line);
×
235
                for ($j = 0; $j < $len; ++$j) {
×
236
                    if (' ' === $line[$j] || "\t" === $line[$j]) {
×
237
                        $indent .= $line[$j];
×
238
                    } else {
239
                        break;
×
240
                    }
241
                }
242

243
                if ('' !== $indent) {
×
244
                    $indentations[] = $indent;
×
245
                }
246
            }
247
        }
248

249
        // Analyze indentations to find the unit
250
        if ([] === $indentations) {
×
251
            return '    '; // Default to 4 spaces
×
252
        }
253

254
        // Check if using tabs
255
        foreach ($indentations as $indent) {
×
256
            if (str_contains($indent, "\t")) {
×
257
                return "\t";
×
258
            }
259
        }
260

261
        // Count spaces - find the smallest non-zero indentation
262
        $spaceCounts = array_map('strlen', $indentations);
×
263
        $spaceCounts = array_filter($spaceCounts, static fn ($count) => $count > 0);
×
264

265
        if ([] === $spaceCounts) {
×
266
            return '    '; // Default to 4 spaces
×
267
        }
268

269
        $minSpaces = min($spaceCounts);
×
270

271
        return str_repeat(' ', $minSpaces);
×
272
    }
273

274
    /**
275
     * @param Tokens<Token> $tokens
276
     */
277
    private function analyzeArguments(
278
        Tokens $tokens,
279
        int $openParenIndex,
280
        int $closeParenIndex,
281
    ): array {
282
        $topLevelCommas = [];
×
283
        $nestingLevel = 0;
×
284
        $hasContent = false;
×
285
        $hasNamedArgs = false;
×
286

287
        for ($i = $openParenIndex + 1; $i < $closeParenIndex; ++$i) {
×
288
            $token = $tokens[$i];
×
289
            $content = $token->getContent();
×
290

291
            if (\in_array($content, self::NESTING_OPEN_TOKENS, true)) {
×
292
                ++$nestingLevel;
×
293
            } elseif (\in_array($content, self::NESTING_CLOSE_TOKENS, true)) {
×
294
                --$nestingLevel;
×
295
            } elseif (',' === $content && 0 === $nestingLevel) {
×
296
                $topLevelCommas[] = $i;
×
297
            } elseif (':' === $content) {
×
298
                $hasNamedArgs = true;
×
299
            }
300

301
            if (!$token->isWhitespace()) {
×
302
                $hasContent = true;
×
303
            }
304
        }
305

306
        return [
×
307
            'argumentCount'  => $hasContent ? \count($topLevelCommas) + 1 : 0,
×
308
            'hasNamedArgs'   => $hasNamedArgs,
×
309
            'topLevelCommas' => $topLevelCommas,
×
310
        ];
×
311
    }
312

313
    /**
314
     * @param Tokens<Token> $tokens
315
     */
316
    private function formatMethodCall(Tokens $tokens, int $openParenIndex, int $closeParenIndex, array $indentation): void
317
    {
318
        $analysisResult = $this->analyzeArguments($tokens, $openParenIndex, $closeParenIndex);
×
319
        $topLevelCommas = $analysisResult['topLevelCommas'];
×
320

321
        // Work backwards to avoid index shifts
322
        $this->addNewlineBeforeClosingParenthesis($tokens, $closeParenIndex, $indentation['base']);
×
323
        $this->addNewlinesAfterCommas($tokens, $topLevelCommas, $indentation['argument']);
×
324
        $this->addNewlineAfterOpeningParenthesis($tokens, $openParenIndex, $indentation['argument']);
×
325
    }
326

327
    /**
328
     * @param Tokens<Token> $tokens
329
     */
330
    private function addNewlineBeforeClosingParenthesis(Tokens $tokens, int $closeParenIndex, string $baseIndent): void
331
    {
332
        $prevIndex = $tokens->getPrevMeaningfulToken($closeParenIndex);
×
333
        if (null === $prevIndex) {
×
334
            return;
×
335
        }
336

337
        if ($prevIndex + 1 === $closeParenIndex) {
×
338
            $tokens->insertAt($closeParenIndex, new Token([T_WHITESPACE, "\n".$baseIndent]));
×
339
        } else {
340
            $this->replaceWhitespaceWithNewline($tokens, $prevIndex + 1, $baseIndent);
×
341
        }
342
    }
343

344
    /**
345
     * @param Tokens<Token> $tokens
346
     */
347
    private function addNewlinesAfterCommas(Tokens $tokens, array $topLevelCommas, string $argumentIndent): void
348
    {
349
        foreach (array_reverse($topLevelCommas) as $commaIndex) {
×
350
            $nextTokenIndex = $commaIndex + 1;
×
351
            if ($nextTokenIndex < \count($tokens) && $tokens[$nextTokenIndex]->isWhitespace()) {
×
352
                $tokens[$nextTokenIndex] = new Token([T_WHITESPACE, "\n".$argumentIndent]);
×
353
            } else {
354
                $tokens->insertAt($commaIndex + 1, new Token([T_WHITESPACE, "\n".$argumentIndent]));
×
355
            }
356
        }
357
    }
358

359
    /**
360
     * @param Tokens<Token> $tokens
361
     */
362
    private function addNewlineAfterOpeningParenthesis(Tokens $tokens, int $openParenIndex, string $argumentIndent): void
363
    {
364
        $nextTokenIndex = $openParenIndex + 1;
×
365
        if ($nextTokenIndex < \count($tokens) && $tokens[$nextTokenIndex]->isWhitespace()) {
×
366
            $tokens[$nextTokenIndex] = new Token([T_WHITESPACE, "\n".$argumentIndent]);
×
367
        } else {
368
            $tokens->insertAt($openParenIndex + 1, new Token([T_WHITESPACE, "\n".$argumentIndent]));
×
369
        }
370
    }
371

372
    /**
373
     * @param Tokens<Token> $tokens
374
     */
375
    private function replaceWhitespaceWithNewline(Tokens $tokens, int $whitespaceIndex, string $indent): void
376
    {
377
        if ($tokens[$whitespaceIndex]->isWhitespace()) {
×
378
            $content = $tokens[$whitespaceIndex]->getContent();
×
379
            if (!str_contains($content, "\n")) {
×
380
                $tokens[$whitespaceIndex] = new Token([T_WHITESPACE, "\n".$indent]);
×
381
            }
382
        }
383
    }
384
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc