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

keradus / PHP-CS-Fixer / 17252691116

26 Aug 2025 11:09PM UTC coverage: 94.743% (-0.01%) from 94.755%
17252691116

push

github

keradus
chore: apply phpdoc_tag_no_named_arguments

28313 of 29884 relevant lines covered (94.74%)

45.64 hits per line

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

94.37
/src/Fixer/ClassNotation/ClassAttributesSeparationFixer.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\ClassNotation;
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\CodeSample;
25
use PhpCsFixer\FixerDefinition\FixerDefinition;
26
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
27
use PhpCsFixer\FixerDefinition\VersionSpecification;
28
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
29
use PhpCsFixer\Preg;
30
use PhpCsFixer\Tokenizer\CT;
31
use PhpCsFixer\Tokenizer\FCT;
32
use PhpCsFixer\Tokenizer\Token;
33
use PhpCsFixer\Tokenizer\Tokens;
34
use PhpCsFixer\Tokenizer\TokensAnalyzer;
35
use PhpCsFixer\Utils;
36
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
37

38
/**
39
 * Make sure there is one blank line above and below class elements.
40
 *
41
 * The exception is when an element is the first or last item in a 'classy'.
42
 *
43
 * @phpstan-type _Class array{
44
 *      index: int,
45
 *      open: int,
46
 *      close: int,
47
 *      elements: non-empty-list<_Element>
48
 *  }
49
 * @phpstan-type _Element array{token: Token, type: string, index: int, start: int, end: int}
50
 * @phpstan-type _AutogeneratedInputConfiguration array{
51
 *  elements?: array<string, string>,
52
 * }
53
 * @phpstan-type _AutogeneratedComputedConfiguration array{
54
 *  elements: array<string, string>,
55
 * }
56
 *
57
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
58
 *
59
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
60
 */
61
final class ClassAttributesSeparationFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
62
{
63
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
64
    use ConfigurableFixerTrait;
65

66
    /**
67
     * @internal
68
     */
69
    public const SPACING_NONE = 'none';
70

71
    /**
72
     * @internal
73
     */
74
    public const SPACING_ONE = 'one';
75

76
    private const SPACING_ONLY_IF_META = 'only_if_meta';
77
    private const MODIFIER_TYPES = [\T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_ABSTRACT, \T_FINAL, \T_STATIC, \T_STRING, \T_NS_SEPARATOR, \T_VAR, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT, CT::T_TYPE_ALTERNATION, CT::T_TYPE_INTERSECTION, CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_OPEN, CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_CLOSE, FCT::T_READONLY, FCT::T_PRIVATE_SET, FCT::T_PROTECTED_SET, FCT::T_PUBLIC_SET];
78

79
    /**
80
     * @var array<string, string>
81
     */
82
    private array $classElementTypes = [];
83

84
    public function getDefinition(): FixerDefinitionInterface
85
    {
86
        return new FixerDefinition(
3✔
87
            'Class, trait and interface elements must be separated with one or none blank line.',
3✔
88
            [
3✔
89
                new CodeSample(
3✔
90
                    '<?php
3✔
91
final class Sample
92
{
93
    protected function foo()
94
    {
95
    }
96
    protected function bar()
97
    {
98
    }
99

100

101
}
102
'
3✔
103
                ),
3✔
104
                new CodeSample(
3✔
105
                    '<?php
3✔
106
class Sample
107
{private $a; // foo
108
    /** second in a hour */
109
    private $b;
110
}
111
',
3✔
112
                    ['elements' => ['property' => self::SPACING_ONE]]
3✔
113
                ),
3✔
114
                new CodeSample(
3✔
115
                    '<?php
3✔
116
class Sample
117
{
118
    const A = 1;
119
    /** seconds in some hours */
120
    const B = 3600;
121
}
122
',
3✔
123
                    ['elements' => ['const' => self::SPACING_ONE]]
3✔
124
                ),
3✔
125
                new CodeSample(
3✔
126
                    '<?php
3✔
127
class Sample
128
{
129
    /** @var int */
130
    const SECOND = 1;
131
    /** @var int */
132
    const MINUTE = 60;
133

134
    const HOUR = 3600;
135

136
    const DAY = 86400;
137
}
138
',
3✔
139
                    ['elements' => ['const' => self::SPACING_ONLY_IF_META]]
3✔
140
                ),
3✔
141
                new VersionSpecificCodeSample(
3✔
142
                    '<?php
3✔
143
class Sample
144
{
145
    public $a;
146
    #[SetUp]
147
    public $b;
148
    /** @var string */
149
    public $c;
150
    /** @internal */
151
    #[Assert\String()]
152
    public $d;
153

154
    public $e;
155
}
156
',
3✔
157
                    new VersionSpecification(8_00_00),
3✔
158
                    ['elements' => ['property' => self::SPACING_ONLY_IF_META]]
3✔
159
                ),
3✔
160
            ]
3✔
161
        );
3✔
162
    }
163

164
    /**
165
     * {@inheritdoc}
166
     *
167
     * Must run before BracesFixer, IndentationTypeFixer, NoExtraBlankLinesFixer, StatementIndentationFixer.
168
     * Must run after OrderedClassElementsFixer, PhpUnitDataProviderMethodOrderFixer, SingleClassElementPerStatementFixer, VisibilityRequiredFixer.
169
     */
170
    public function getPriority(): int
171
    {
172
        return 55;
1✔
173
    }
174

175
    public function isCandidate(Tokens $tokens): bool
176
    {
177
        return $tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds());
95✔
178
    }
179

180
    protected function configurePostNormalisation(): void
181
    {
182
        $this->classElementTypes = []; // reset previous configuration
114✔
183

184
        foreach ($this->configuration['elements'] as $elementType => $spacing) {
114✔
185
            $this->classElementTypes[$elementType] = $spacing;
114✔
186
        }
187
    }
188

189
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
190
    {
191
        foreach ($this->getElementsByClass($tokens) as $class) {
95✔
192
            $elements = $class['elements'];
93✔
193

194
            if (0 === \count($elements)) {
93✔
195
                continue;
×
196
            }
197

198
            if (isset($this->classElementTypes[$elements[0]['type']])) {
93✔
199
                $this->fixSpaceBelowClassElement($tokens, $class);
89✔
200
            }
201

202
            foreach ($elements as $index => $element) {
93✔
203
                if (isset($this->classElementTypes[$element['type']])) {
93✔
204
                    $this->fixSpaceAboveClassElement($tokens, $class, $index);
92✔
205
                }
206
            }
207
        }
208
    }
209

210
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
211
    {
212
        return new FixerConfigurationResolver([
114✔
213
            (new FixerOptionBuilder('elements', 'Dictionary of `const|method|property|trait_import|case` => `none|one|only_if_meta` values.'))
114✔
214
                ->setAllowedTypes(['array<string, string>'])
114✔
215
                ->setAllowedValues([static function (array $option): bool {
114✔
216
                    foreach ($option as $type => $spacing) {
114✔
217
                        $supportedTypes = ['const', 'method', 'property', 'trait_import', 'case'];
114✔
218

219
                        if (!\in_array($type, $supportedTypes, true)) {
114✔
220
                            throw new InvalidOptionsException(
2✔
221
                                \sprintf(
2✔
222
                                    'Unexpected element type, expected any of %s, got "%s".',
2✔
223
                                    Utils::naturalLanguageJoin($supportedTypes),
2✔
224
                                    \gettype($type).'#'.$type
2✔
225
                                )
2✔
226
                            );
2✔
227
                        }
228

229
                        $supportedSpacings = [self::SPACING_NONE, self::SPACING_ONE, self::SPACING_ONLY_IF_META];
114✔
230

231
                        if (!\in_array($spacing, $supportedSpacings, true)) {
114✔
232
                            throw new InvalidOptionsException(
1✔
233
                                \sprintf(
1✔
234
                                    'Unexpected spacing for element type "%s", expected any of %s, got "%s".',
1✔
235
                                    $spacing,
1✔
236
                                    Utils::naturalLanguageJoin($supportedSpacings),
1✔
237
                                    \is_object($spacing) ? \get_class($spacing) : (null === $spacing ? 'null' : \gettype($spacing).'#'.$spacing)
1✔
238
                                )
1✔
239
                            );
1✔
240
                        }
241
                    }
242

243
                    return true;
114✔
244
                }])
114✔
245
                ->setDefault([
114✔
246
                    'const' => self::SPACING_ONE,
114✔
247
                    'method' => self::SPACING_ONE,
114✔
248
                    'property' => self::SPACING_ONE,
114✔
249
                    'trait_import' => self::SPACING_NONE,
114✔
250
                    'case' => self::SPACING_NONE,
114✔
251
                ])
114✔
252
                ->getOption(),
114✔
253
        ]);
114✔
254
    }
255

256
    /**
257
     * Fix spacing above an element of a class, interface or trait.
258
     *
259
     * Deals with comments, PHPDocs and spaces above the element with respect to the position of the
260
     * element within the class, interface or trait.
261
     *
262
     * @param _Class $class
263
     */
264
    private function fixSpaceAboveClassElement(Tokens $tokens, array $class, int $elementIndex): void
265
    {
266
        \assert(isset($class['elements'][$elementIndex]));
92✔
267
        $element = $class['elements'][$elementIndex];
92✔
268
        $elementAboveEnd = isset($class['elements'][$elementIndex + 1]) ? $class['elements'][$elementIndex + 1]['end'] : 0;
92✔
269
        $nonWhiteAbove = $tokens->getPrevNonWhitespace($element['start']);
92✔
270

271
        // element is directly after class open brace
272
        if ($nonWhiteAbove === $class['open']) {
92✔
273
            $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], 1);
65✔
274

275
            return;
65✔
276
        }
277

278
        // deal with comments above an element
279
        if ($tokens[$nonWhiteAbove]->isGivenKind(\T_COMMENT)) {
81✔
280
            // check if the comment belongs to the previous element
281
            if ($elementAboveEnd === $nonWhiteAbove) {
25✔
282
                $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], $this->determineRequiredLineCount($tokens, $class, $elementIndex));
8✔
283

284
                return;
8✔
285
            }
286

287
            // more than one line break, always bring it back to 2 line breaks between the element start and what is above it
288
            if ($tokens[$nonWhiteAbove + 1]->isWhitespace() && substr_count($tokens[$nonWhiteAbove + 1]->getContent(), "\n") > 1) {
19✔
289
                $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], 2);
8✔
290

291
                return;
8✔
292
            }
293

294
            // there are 2 cases:
295
            if (
296
                1 === $element['start'] - $nonWhiteAbove
13✔
297
                || $tokens[$nonWhiteAbove - 1]->isWhitespace() && substr_count($tokens[$nonWhiteAbove - 1]->getContent(), "\n") > 0
13✔
298
                || $tokens[$nonWhiteAbove + 1]->isWhitespace() && substr_count($tokens[$nonWhiteAbove + 1]->getContent(), "\n") > 0
13✔
299
            ) {
300
                // 1. The comment is meant for the element (although not a PHPDoc),
301
                //    make sure there is one line break between the element and the comment...
302
                $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], 1);
13✔
303
                //    ... and make sure there is blank line above the comment (with the exception when it is directly after a class opening)
304
                $nonWhiteAbove = $this->findCommentBlockStart($tokens, $nonWhiteAbove, $elementAboveEnd);
13✔
305
                $nonWhiteAboveComment = $tokens->getPrevNonWhitespace($nonWhiteAbove);
13✔
306

307
                if ($nonWhiteAboveComment === $class['open']) {
13✔
308
                    if ($tokens[$nonWhiteAboveComment - 1]->isWhitespace() && substr_count($tokens[$nonWhiteAboveComment - 1]->getContent(), "\n") > 0) {
10✔
309
                        $this->correctLineBreaks($tokens, $nonWhiteAboveComment, $nonWhiteAbove, 1);
7✔
310
                    }
311
                } else {
312
                    $this->correctLineBreaks($tokens, $nonWhiteAboveComment, $nonWhiteAbove, 2);
3✔
313
                }
314
            } else {
315
                // 2. The comment belongs to the code above the element,
316
                //    make sure there is a blank line above the element (i.e. 2 line breaks)
317
                $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], 2);
×
318
            }
319

320
            return;
13✔
321
        }
322

323
        // deal with element with a PHPDoc/attribute above it
324
        if ($tokens[$nonWhiteAbove]->isGivenKind([\T_DOC_COMMENT, CT::T_ATTRIBUTE_CLOSE])) {
64✔
325
            // there should be one linebreak between the element and the attribute above it
326
            $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], 1);
24✔
327

328
            // make sure there is blank line above the comment (with the exception when it is directly after a class opening)
329
            $nonWhiteAbove = $this->findCommentBlockStart($tokens, $nonWhiteAbove, $elementAboveEnd);
24✔
330
            $nonWhiteAboveComment = $tokens->getPrevNonWhitespace($nonWhiteAbove);
24✔
331

332
            $this->correctLineBreaks($tokens, $nonWhiteAboveComment, $nonWhiteAbove, $nonWhiteAboveComment === $class['open'] ? 1 : 2);
24✔
333

334
            return;
24✔
335
        }
336

337
        $this->correctLineBreaks($tokens, $nonWhiteAbove, $element['start'], $this->determineRequiredLineCount($tokens, $class, $elementIndex));
54✔
338
    }
339

340
    /**
341
     * @param _Class $class
342
     */
343
    private function determineRequiredLineCount(Tokens $tokens, array $class, int $elementIndex): int
344
    {
345
        \assert(isset($class['elements'][$elementIndex]));
55✔
346
        $type = $class['elements'][$elementIndex]['type'];
55✔
347
        $spacing = $this->classElementTypes[$type];
55✔
348

349
        if (self::SPACING_ONE === $spacing) {
55✔
350
            return 2;
40✔
351
        }
352

353
        if (self::SPACING_NONE === $spacing) {
21✔
354
            if (!isset($class['elements'][$elementIndex + 1])) {
14✔
355
                return 1;
×
356
            }
357

358
            $aboveElement = $class['elements'][$elementIndex + 1];
14✔
359

360
            if ($aboveElement['type'] !== $type) {
14✔
361
                return 2;
2✔
362
            }
363

364
            $aboveElementDocCandidateIndex = $tokens->getPrevNonWhitespace($aboveElement['start']);
14✔
365

366
            return $tokens[$aboveElementDocCandidateIndex]->isGivenKind([\T_DOC_COMMENT, CT::T_ATTRIBUTE_CLOSE]) ? 2 : 1;
14✔
367
        }
368

369
        if (self::SPACING_ONLY_IF_META === $spacing) {
7✔
370
            $aboveElementDocCandidateIndex = $tokens->getPrevNonWhitespace($class['elements'][$elementIndex]['start']);
7✔
371

372
            return $tokens[$aboveElementDocCandidateIndex]->isGivenKind([\T_DOC_COMMENT, CT::T_ATTRIBUTE_CLOSE]) ? 2 : 1;
7✔
373
        }
374

375
        throw new \RuntimeException(\sprintf('Unknown spacing "%s".', $spacing));
×
376
    }
377

378
    /**
379
     * @param _Class $class
380
     */
381
    private function fixSpaceBelowClassElement(Tokens $tokens, array $class): void
382
    {
383
        $element = $class['elements'][0];
89✔
384

385
        // if this is last element fix; fix to the class end `}` here if appropriate
386
        if ($class['close'] === $tokens->getNextNonWhitespace($element['end'])) {
89✔
387
            $this->correctLineBreaks($tokens, $element['end'], $class['close'], 1);
88✔
388
        }
389
    }
390

391
    private function correctLineBreaks(Tokens $tokens, int $startIndex, int $endIndex, int $reqLineCount): void
392
    {
393
        $lineEnding = $this->whitespacesConfig->getLineEnding();
92✔
394

395
        ++$startIndex;
92✔
396
        $numbOfWhiteTokens = $endIndex - $startIndex;
92✔
397

398
        if (0 === $numbOfWhiteTokens) {
92✔
399
            $tokens->insertAt($startIndex, new Token([\T_WHITESPACE, str_repeat($lineEnding, $reqLineCount)]));
17✔
400

401
            return;
17✔
402
        }
403

404
        $lineBreakCount = $this->getLineBreakCount($tokens, $startIndex, $endIndex);
92✔
405

406
        if ($reqLineCount === $lineBreakCount) {
92✔
407
            return;
92✔
408
        }
409

410
        if ($lineBreakCount < $reqLineCount) {
69✔
411
            $tokens[$startIndex] = new Token([
50✔
412
                \T_WHITESPACE,
50✔
413
                str_repeat($lineEnding, $reqLineCount - $lineBreakCount).$tokens[$startIndex]->getContent(),
50✔
414
            ]);
50✔
415

416
            return;
50✔
417
        }
418

419
        // $lineCount = > $reqLineCount : check the one Token case first since this one will be true most of the time
420
        if (1 === $numbOfWhiteTokens) {
39✔
421
            $tokens[$startIndex] = new Token([
39✔
422
                \T_WHITESPACE,
39✔
423
                Preg::replace('/\r\n|\n/', '', $tokens[$startIndex]->getContent(), $lineBreakCount - $reqLineCount),
39✔
424
            ]);
39✔
425

426
            return;
39✔
427
        }
428

429
        // $numbOfWhiteTokens = > 1
430
        $toReplaceCount = $lineBreakCount - $reqLineCount;
×
431

432
        for ($i = $startIndex; $i < $endIndex && $toReplaceCount > 0; ++$i) {
×
433
            $tokenLineCount = substr_count($tokens[$i]->getContent(), "\n");
×
434

435
            if ($tokenLineCount > 0) {
×
436
                $tokens[$i] = new Token([
×
437
                    \T_WHITESPACE,
×
438
                    Preg::replace('/\r\n|\n/', '', $tokens[$i]->getContent(), min($toReplaceCount, $tokenLineCount)),
×
439
                ]);
×
440
                $toReplaceCount -= $tokenLineCount;
×
441
            }
442
        }
443
    }
444

445
    private function getLineBreakCount(Tokens $tokens, int $startIndex, int $endIndex): int
446
    {
447
        $lineCount = 0;
100✔
448

449
        for ($i = $startIndex; $i < $endIndex; ++$i) {
100✔
450
            $lineCount += substr_count($tokens[$i]->getContent(), "\n");
100✔
451
        }
452

453
        return $lineCount;
100✔
454
    }
455

456
    private function findCommentBlockStart(Tokens $tokens, int $start, int $elementAboveEnd): int
457
    {
458
        for ($i = $start; $i > $elementAboveEnd; --$i) {
44✔
459
            if ($tokens[$i]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
44✔
460
                $start = $i = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ATTRIBUTE, $i);
7✔
461

462
                continue;
7✔
463
            }
464

465
            if ($tokens[$i]->isComment()) {
44✔
466
                $start = $i;
39✔
467

468
                continue;
39✔
469
            }
470

471
            if (!$tokens[$i]->isWhitespace() || $this->getLineBreakCount($tokens, $i, $i + 1) > 1) {
44✔
472
                break;
40✔
473
            }
474
        }
475

476
        return $start;
44✔
477
    }
478

479
    /**
480
     * @TODO Introduce proper DTO instead of an array
481
     *
482
     * @return \Generator<_Class>
483
     */
484
    private function getElementsByClass(Tokens $tokens): \Generator
485
    {
486
        $tokensAnalyzer = new TokensAnalyzer($tokens);
95✔
487
        $class = null;
95✔
488

489
        foreach (array_reverse($tokensAnalyzer->getClassyElements(), true) as $index => $element) {
95✔
490
            $element['index'] = $index;
93✔
491

492
            if (null === $class || $element['classIndex'] !== $class['index']) {
93✔
493
                if (null !== $class) {
93✔
494
                    yield $class;
4✔
495
                }
496

497
                $classIndex = $element['classIndex'];
93✔
498
                $classOpen = $tokens->getNextTokenOfKind($classIndex, ['{']);
93✔
499
                $classEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classOpen);
93✔
500
                $class = [
93✔
501
                    'index' => $element['classIndex'],
93✔
502
                    'open' => $classOpen,
93✔
503
                    'close' => $classEnd,
93✔
504
                    'elements' => [],
93✔
505
                ];
93✔
506
            }
507

508
            unset($element['classIndex']);
93✔
509
            $element['start'] = $this->getFirstTokenIndexOfClassElement($tokens, $class['open'], $index);
93✔
510
            $element['end'] = $this->getLastTokenIndexOfClassElement($tokens, $class['index'], $index, $element['type'], $tokensAnalyzer);
93✔
511

512
            $class['elements'][] = $element; // reset the key by design
93✔
513
        }
514

515
        if (null !== $class) {
95✔
516
            yield $class;
93✔
517
        }
518
    }
519

520
    /**
521
     * including trailing single line comments if belonging to the class element.
522
     */
523
    private function getFirstTokenIndexOfClassElement(Tokens $tokens, int $classOpen, int $elementIndex): int
524
    {
525
        $firstElementAttributeIndex = $elementIndex;
93✔
526

527
        do {
528
            $nonWhiteAbove = $tokens->getPrevMeaningfulToken($firstElementAttributeIndex);
93✔
529

530
            if (null !== $nonWhiteAbove && $tokens[$nonWhiteAbove]->isGivenKind(self::MODIFIER_TYPES)) {
93✔
531
                $firstElementAttributeIndex = $nonWhiteAbove;
77✔
532
            } else {
533
                break;
93✔
534
            }
535
        } while ($firstElementAttributeIndex > $classOpen);
77✔
536

537
        return $firstElementAttributeIndex;
93✔
538
    }
539

540
    /**
541
     * including trailing single line comments if belonging to the class element.
542
     */
543
    private function getLastTokenIndexOfClassElement(Tokens $tokens, int $classIndex, int $elementIndex, string $elementType, TokensAnalyzer $tokensAnalyzer): int
544
    {
545
        // find last token of the element
546
        if ('method' === $elementType && !$tokens[$classIndex]->isGivenKind(\T_INTERFACE)) {
93✔
547
            $attributes = $tokensAnalyzer->getMethodAttributes($elementIndex);
53✔
548

549
            if (true === $attributes['abstract']) {
53✔
550
                $elementEndIndex = $tokens->getNextTokenOfKind($elementIndex, [';']);
5✔
551
            } else {
552
                $elementEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $tokens->getNextTokenOfKind($elementIndex, ['{']));
51✔
553
            }
554
        } elseif ('trait_import' === $elementType) {
63✔
555
            $elementEndIndex = $elementIndex;
13✔
556

557
            do {
558
                $elementEndIndex = $tokens->getNextMeaningfulToken($elementEndIndex);
13✔
559
            } while ($tokens[$elementEndIndex]->isGivenKind([\T_STRING, \T_NS_SEPARATOR]) || $tokens[$elementEndIndex]->equals(','));
13✔
560

561
            if (!$tokens[$elementEndIndex]->equals(';')) {
13✔
562
                $elementEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $tokens->getNextTokenOfKind($elementIndex, ['{']));
4✔
563
            }
564
        } else { // 'const', 'property', enum-'case', or 'method' of an interface
565
            $elementEndIndex = $tokens->getNextTokenOfKind($elementIndex, [';', '{']);
52✔
566
        }
567

568
        $singleLineElement = true;
93✔
569

570
        for ($i = $elementIndex + 1; $i < $elementEndIndex; ++$i) {
93✔
571
            if (str_contains($tokens[$i]->getContent(), "\n")) {
73✔
572
                $singleLineElement = false;
31✔
573

574
                break;
31✔
575
            }
576
        }
577

578
        if ($singleLineElement) {
93✔
579
            while (true) {
77✔
580
                $nextToken = $tokens[$elementEndIndex + 1];
77✔
581

582
                if (($nextToken->isComment() || $nextToken->isWhitespace()) && !str_contains($nextToken->getContent(), "\n")) {
77✔
583
                    ++$elementEndIndex;
14✔
584
                } else {
585
                    break;
77✔
586
                }
587
            }
588

589
            if ($tokens[$elementEndIndex]->isWhitespace()) {
77✔
590
                $elementEndIndex = $tokens->getPrevNonWhitespace($elementEndIndex);
2✔
591
            }
592
        }
593

594
        return $elementEndIndex;
93✔
595
    }
596
}
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