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

j-schumann / symfony-addons / 23257254021

18 Mar 2026 05:08PM UTC coverage: 53.674% (-0.6%) from 54.23%
23257254021

push

github

web-flow
upd: RefreshDatabaseTrait: Support SQLServer, fix MySQL/MariaDB (#29)

6 of 29 new or added lines in 4 files covered. (20.69%)

1 existing line in 1 file now uncovered.

504 of 939 relevant lines covered (53.67%)

3.45 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(
×
80
                'max_arguments',
×
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 (
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
135
        if (!$analysisResult['hasNamedArgs'] || $analysisResult['argumentCount'] <= $this->maxArguments) {
×
136
            return false;
×
137
        }
138

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

143
    /**
144
     * @param Tokens<Token>      $tokens
145
     * @param bool|int|list<int> $topLevelCommas
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
150
        $nextIndex = $openParenIndex + 1;
×
151
        if ($nextIndex < $closeParenIndex && $tokens[$nextIndex]->isWhitespace() && str_contains($tokens[$nextIndex]->getContent(), "\n")) {
×
152
            // There's a newline after opening paren, likely already formatted
153
            return true;
×
154
        }
155

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

165
        return false;
×
166
    }
167

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

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

195
        // Detect indentation unit (try to find consistent indentation in the file)
196
        $indentUnit = $this->detectIndentationUnit($tokens);
×
197

198
        return [
×
199
            'base'     => $baseIndent,
×
200
            'unit'     => $indentUnit,
×
201
            'argument' => $baseIndent.$indentUnit,
×
202
        ];
×
203
    }
204

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

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

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

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

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

240
                if ('' !== $indent) {
×
241
                    $indentations[] = $indent;
×
242
                }
243
            }
244
        }
245

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

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

258
        // Count spaces - find the smallest non-zero indentation
259
        $spaceCounts = array_map(strlen(...), $indentations);
×
260
        $spaceCounts = array_filter($spaceCounts, static fn ($count) => $count > 0);
×
261

262
        if ([] === $spaceCounts) {
×
263
            return '    '; // Default to 4 spaces
×
264
        }
265

266
        $minSpaces = min($spaceCounts);
×
267

268
        return str_repeat(' ', $minSpaces);
×
269
    }
270

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

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

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

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

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

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

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

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

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

343
    /**
344
     * @param Tokens<Token>      $tokens
345
     * @param bool|int|list<int> $topLevelCommas
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 · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc