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

keradus / PHP-CS-Fixer / 17279562118

27 Aug 2025 09:47PM UTC coverage: 94.693%. Remained the same
17279562118

push

github

keradus
CS

28316 of 29903 relevant lines covered (94.69%)

45.61 hits per line

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

97.86
/src/Fixer/Comment/HeaderCommentFixer.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\Comment;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
19
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
20
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
21
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
22
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
23
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
24
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
25
use PhpCsFixer\FixerDefinition\CodeSample;
26
use PhpCsFixer\FixerDefinition\FixerDefinition;
27
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
28
use PhpCsFixer\Preg;
29
use PhpCsFixer\PregException;
30
use PhpCsFixer\Tokenizer\Token;
31
use PhpCsFixer\Tokenizer\Tokens;
32
use Symfony\Component\OptionsResolver\Options;
33

34
/**
35
 * @phpstan-type _AutogeneratedInputConfiguration array{
36
 *  comment_type?: 'PHPDoc'|'comment',
37
 *  header: string,
38
 *  location?: 'after_declare_strict'|'after_open',
39
 *  separate?: 'both'|'bottom'|'none'|'top',
40
 *  validator?: null|string,
41
 * }
42
 * @phpstan-type _AutogeneratedComputedConfiguration array{
43
 *  comment_type: 'PHPDoc'|'comment',
44
 *  header: string,
45
 *  location: 'after_declare_strict'|'after_open',
46
 *  separate: 'both'|'bottom'|'none'|'top',
47
 *  validator: null|string,
48
 * }
49
 *
50
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
51
 *
52
 * @author Antonio J. García Lagar <aj@garcialagar.es>
53
 *
54
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
55
 */
56
final class HeaderCommentFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
57
{
58
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
59
    use ConfigurableFixerTrait;
60

61
    /**
62
     * @internal
63
     */
64
    public const HEADER_PHPDOC = 'PHPDoc';
65

66
    /**
67
     * @internal
68
     */
69
    public const HEADER_COMMENT = 'comment';
70

71
    public function getDefinition(): FixerDefinitionInterface
72
    {
73
        return new FixerDefinition(
3✔
74
            'Add, replace or remove header comment.',
3✔
75
            [
3✔
76
                new CodeSample(
3✔
77
                    '<?php
3✔
78
declare(strict_types=1);
79

80
namespace A\B;
81

82
echo 1;
83
',
3✔
84
                    [
3✔
85
                        'header' => 'Made with love.',
3✔
86
                    ]
3✔
87
                ),
3✔
88
                new CodeSample(
3✔
89
                    '<?php
3✔
90
declare(strict_types=1);
91

92
namespace A\B;
93

94
echo 1;
95
',
3✔
96
                    [
3✔
97
                        'header' => 'Made with love.',
3✔
98
                        'comment_type' => self::HEADER_PHPDOC,
3✔
99
                        'location' => 'after_open',
3✔
100
                        'separate' => 'bottom',
3✔
101
                    ]
3✔
102
                ),
3✔
103
                new CodeSample(
3✔
104
                    '<?php
3✔
105
declare(strict_types=1);
106

107
namespace A\B;
108

109
echo 1;
110
',
3✔
111
                    [
3✔
112
                        'header' => 'Made with love.',
3✔
113
                        'comment_type' => self::HEADER_COMMENT,
3✔
114
                        'location' => 'after_declare_strict',
3✔
115
                    ]
3✔
116
                ),
3✔
117
                new CodeSample(
3✔
118
                    '<?php
3✔
119
declare(strict_types=1);
120
/*
121
 * Made with love.
122
 *
123
 * Extra content.
124
 */
125
namespace A\B;
126

127
echo 1;
128
',
3✔
129
                    [
3✔
130
                        'header' => 'Made with love.',
3✔
131
                        'validator' => '/Made with love(?P<EXTRA>.*)??/s',
3✔
132
                        'comment_type' => self::HEADER_COMMENT,
3✔
133
                        'location' => 'after_declare_strict',
3✔
134
                    ]
3✔
135
                ),
3✔
136
                new CodeSample(
3✔
137
                    '<?php
3✔
138
declare(strict_types=1);
139

140
/*
141
 * Comment is not wanted here.
142
 */
143

144
namespace A\B;
145

146
echo 1;
147
',
3✔
148
                    [
3✔
149
                        'header' => '',
3✔
150
                    ]
3✔
151
                ),
3✔
152
            ]
3✔
153
        );
3✔
154
    }
155

156
    public function isCandidate(Tokens $tokens): bool
157
    {
158
        return $tokens->isMonolithicPhp() && !$tokens->isTokenKindFound(\T_OPEN_TAG_WITH_ECHO);
52✔
159
    }
160

161
    /**
162
     * {@inheritdoc}
163
     *
164
     * Must run before BlankLinesBeforeNamespaceFixer, SingleBlankLineBeforeNamespaceFixer, SingleLineCommentStyleFixer.
165
     * Must run after DeclareStrictTypesFixer, NoBlankLinesAfterPhpdocFixer.
166
     */
167
    public function getPriority(): int
168
    {
169
        // When this fixer is configured with ["separate" => "bottom", "comment_type" => "PHPDoc"]
170
        // and the target file has no namespace or declare() construct,
171
        // the fixed header comment gets trimmed by NoBlankLinesAfterPhpdocFixer if we run before it.
172
        return -30;
1✔
173
    }
174

175
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
176
    {
177
        $headerAsComment = $this->getHeaderAsComment();
41✔
178
        $location = $this->configuration['location'];
41✔
179
        $locationIndices = [];
41✔
180

181
        foreach (['after_open', 'after_declare_strict'] as $possibleLocation) {
41✔
182
            $locationIndex = $this->findHeaderCommentInsertionIndex($tokens, $possibleLocation);
41✔
183

184
            if (!isset($locationIndices[$locationIndex]) || $possibleLocation === $location) {
41✔
185
                $locationIndices[$locationIndex] = $possibleLocation;
41✔
186
            }
187
        }
188

189
        // pre-run to find existing comment, if dynamic content is allowed
190
        if (null !== $this->configuration['validator']) {
41✔
191
            foreach ($locationIndices as $possibleLocation) {
4✔
192
                // figure out where the comment should be placed
193
                $headerNewIndex = $this->findHeaderCommentInsertionIndex($tokens, $possibleLocation);
4✔
194

195
                // check if there is already a comment
196
                $headerCurrentIndex = $this->findHeaderCommentCurrentIndex($tokens, $headerAsComment, $headerNewIndex - 1);
4✔
197

198
                if (null === $headerCurrentIndex) {
4✔
199
                    continue;
3✔
200
                }
201
                $currentHeaderComment = $tokens[$headerCurrentIndex]->getContent();
4✔
202

203
                if ($this->doesTokenFulfillValidator($tokens[$headerCurrentIndex])) {
4✔
204
                    $headerAsComment = $currentHeaderComment;
4✔
205
                }
206
            }
207
        }
208

209
        foreach ($locationIndices as $possibleLocation) {
41✔
210
            // figure out where the comment should be placed
211
            $headerNewIndex = $this->findHeaderCommentInsertionIndex($tokens, $possibleLocation);
41✔
212

213
            // check if there is already a comment
214
            $headerCurrentIndex = $this->findHeaderCommentCurrentIndex($tokens, $headerAsComment, $headerNewIndex - 1);
41✔
215

216
            if (null === $headerCurrentIndex) {
41✔
217
                if ('' === $this->configuration['header'] || $possibleLocation !== $location) {
31✔
218
                    continue;
11✔
219
                }
220

221
                $this->insertHeader($tokens, $headerAsComment, $headerNewIndex);
29✔
222

223
                continue;
29✔
224
            }
225

226
            $currentHeaderComment = $tokens[$headerCurrentIndex]->getContent();
41✔
227
            $sameComment = $headerAsComment === $currentHeaderComment;
41✔
228
            $expectedLocation = $possibleLocation === $location;
41✔
229

230
            if (!$sameComment || !$expectedLocation) {
41✔
231
                if ($expectedLocation xor $sameComment) {
14✔
232
                    $this->removeHeader($tokens, $headerCurrentIndex);
13✔
233
                }
234

235
                if ('' === $this->configuration['header']) {
14✔
236
                    continue;
2✔
237
                }
238

239
                if ($possibleLocation === $location) {
12✔
240
                    $this->insertHeader($tokens, $headerAsComment, $headerNewIndex);
8✔
241
                }
242

243
                continue;
12✔
244
            }
245

246
            $this->fixWhiteSpaceAroundHeader($tokens, $headerCurrentIndex);
40✔
247
        }
248
    }
249

250
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
251
    {
252
        $fixerName = $this->getName();
70✔
253

254
        return new FixerConfigurationResolver([
70✔
255
            (new FixerOptionBuilder('header', 'Proper header content.'))
70✔
256
                ->setAllowedTypes(['string'])
70✔
257
                ->setNormalizer(static function (Options $options, string $value) use ($fixerName): string {
70✔
258
                    if ('' === trim($value)) {
53✔
259
                        return '';
10✔
260
                    }
261

262
                    if (str_contains($value, '*/')) {
44✔
263
                        throw new InvalidFixerConfigurationException($fixerName, 'Cannot use \'*/\' in header.');
1✔
264
                    }
265

266
                    return $value;
43✔
267
                })
70✔
268
                ->getOption(),
70✔
269
            (new FixerOptionBuilder('validator', 'RegEx validator for header content.'))
70✔
270
                ->setAllowedTypes(['string', 'null'])
70✔
271
                ->setNormalizer(static function (Options $options, ?string $value) use ($fixerName): ?string {
70✔
272
                    if (null !== $value) {
55✔
273
                        try {
274
                            Preg::match($value, '');
5✔
275
                        } catch (PregException $exception) {
1✔
276
                            throw new InvalidFixerConfigurationException($fixerName, 'Provided RegEx is not valid.');
1✔
277
                        }
278
                    }
279

280
                    return $value;
54✔
281
                })
70✔
282
                ->setDefault(null)
70✔
283
                ->getOption(),
70✔
284
            (new FixerOptionBuilder('comment_type', 'Comment syntax type.'))
70✔
285
                ->setAllowedValues([self::HEADER_PHPDOC, self::HEADER_COMMENT])
70✔
286
                ->setDefault(self::HEADER_COMMENT)
70✔
287
                ->getOption(),
70✔
288
            (new FixerOptionBuilder('location', 'The location of the inserted header.'))
70✔
289
                ->setAllowedValues(['after_open', 'after_declare_strict'])
70✔
290
                ->setDefault('after_declare_strict')
70✔
291
                ->getOption(),
70✔
292
            (new FixerOptionBuilder('separate', 'Whether the header should be separated from the file content with a new line.'))
70✔
293
                ->setAllowedValues(['both', 'top', 'bottom', 'none'])
70✔
294
                ->setDefault('both')
70✔
295
                ->getOption(),
70✔
296
        ]);
70✔
297
    }
298

299
    private function doesTokenFulfillValidator(Token $token): bool
300
    {
301
        if (null === $this->configuration['validator']) {
4✔
302
            throw new \LogicException(\sprintf("Cannot call '%s' method while missing config:validator.", __METHOD__));
×
303
        }
304
        $currentHeaderComment = $token->getContent();
4✔
305

306
        $lines = implode("\n", array_map(
4✔
307
            static fn (string $line): string => ' *' === $line ? '' : (str_starts_with($line, ' * ') ? substr($line, 3) : $line),
4✔
308
            \array_slice(explode("\n", $currentHeaderComment), 1, -1),
4✔
309
        ));
4✔
310

311
        return Preg::match($this->configuration['validator'], $lines);
4✔
312
    }
313

314
    /**
315
     * Enclose the given text in a comment block.
316
     */
317
    private function getHeaderAsComment(): string
318
    {
319
        $lineEnding = $this->whitespacesConfig->getLineEnding();
41✔
320
        $comment = (self::HEADER_COMMENT === $this->configuration['comment_type'] ? '/*' : '/**').$lineEnding;
41✔
321
        $lines = explode("\n", str_replace("\r", '', $this->configuration['header']));
41✔
322

323
        foreach ($lines as $line) {
41✔
324
            $comment .= rtrim(' * '.$line).$lineEnding;
41✔
325
        }
326

327
        return $comment.' */';
41✔
328
    }
329

330
    private function findHeaderCommentCurrentIndex(Tokens $tokens, string $headerAsComment, int $headerNewIndex): ?int
331
    {
332
        $index = $tokens->getNextNonWhitespace($headerNewIndex);
41✔
333

334
        if (null === $index || !$tokens[$index]->isComment()) {
41✔
335
            return null;
28✔
336
        }
337

338
        $next = $index + 1;
41✔
339

340
        if (!isset($tokens[$next]) || \in_array($this->configuration['separate'], ['top', 'none'], true) || !$tokens[$index]->isGivenKind(\T_DOC_COMMENT)) {
41✔
341
            return $index;
30✔
342
        }
343

344
        if ($tokens[$next]->isWhitespace()) {
15✔
345
            if (!Preg::match('/^\h*\R\h*$/D', $tokens[$next]->getContent())) {
15✔
346
                return $index;
9✔
347
            }
348

349
            ++$next;
9✔
350
        }
351

352
        if (!isset($tokens[$next]) || !$tokens[$next]->isClassy() && !$tokens[$next]->isGivenKind(\T_FUNCTION)) {
9✔
353
            return $index;
4✔
354
        }
355

356
        if (
357
            $headerAsComment === $tokens[$index]->getContent()
5✔
358
            || (null !== $this->configuration['validator'] && $this->doesTokenFulfillValidator($tokens[$index]))
5✔
359
        ) {
360
            return $index;
2✔
361
        }
362

363
        return null;
3✔
364
    }
365

366
    /**
367
     * Find the index where the header comment must be inserted.
368
     */
369
    private function findHeaderCommentInsertionIndex(Tokens $tokens, string $location): int
370
    {
371
        $openTagIndex = $tokens[0]->isGivenKind(\T_INLINE_HTML) ? 1 : 0;
41✔
372

373
        if ('after_open' === $location) {
41✔
374
            return $openTagIndex + 1;
41✔
375
        }
376

377
        $index = $tokens->getNextMeaningfulToken($openTagIndex);
41✔
378

379
        if (null === $index) {
41✔
380
            return $openTagIndex + 1; // file without meaningful tokens but an open tag, comment should always be placed directly after the open tag
3✔
381
        }
382

383
        if (!$tokens[$index]->isGivenKind(\T_DECLARE)) {
38✔
384
            return $openTagIndex + 1;
23✔
385
        }
386

387
        $next = $tokens->getNextMeaningfulToken($index);
15✔
388

389
        if (null === $next || !$tokens[$next]->equals('(')) {
15✔
390
            return $openTagIndex + 1;
×
391
        }
392

393
        $next = $tokens->getNextMeaningfulToken($next);
15✔
394

395
        if (null === $next || !$tokens[$next]->equals([\T_STRING, 'strict_types'], false)) {
15✔
396
            return $openTagIndex + 1;
1✔
397
        }
398

399
        $next = $tokens->getNextMeaningfulToken($next);
14✔
400

401
        if (null === $next || !$tokens[$next]->equals('=')) {
14✔
402
            return $openTagIndex + 1;
×
403
        }
404

405
        $next = $tokens->getNextMeaningfulToken($next);
14✔
406

407
        if (null === $next || !$tokens[$next]->isGivenKind(\T_LNUMBER)) {
14✔
408
            return $openTagIndex + 1;
×
409
        }
410

411
        $next = $tokens->getNextMeaningfulToken($next);
14✔
412

413
        if (null === $next || !$tokens[$next]->equals(')')) {
14✔
414
            return $openTagIndex + 1;
×
415
        }
416

417
        $next = $tokens->getNextMeaningfulToken($next);
14✔
418

419
        if (null === $next || !$tokens[$next]->equals(';')) { // don't insert after close tag
14✔
420
            return $openTagIndex + 1;
1✔
421
        }
422

423
        return $next + 1;
13✔
424
    }
425

426
    private function fixWhiteSpaceAroundHeader(Tokens $tokens, int $headerIndex): void
427
    {
428
        $lineEnding = $this->whitespacesConfig->getLineEnding();
40✔
429

430
        // fix lines after header comment
431
        if (
432
            ('both' === $this->configuration['separate'] || 'bottom' === $this->configuration['separate'])
40✔
433
            && null !== $tokens->getNextMeaningfulToken($headerIndex)
40✔
434
        ) {
435
            $expectedLineCount = 2;
33✔
436
        } else {
437
            $expectedLineCount = 1;
7✔
438
        }
439

440
        if ($headerIndex === \count($tokens) - 1) {
40✔
441
            $tokens->insertAt($headerIndex + 1, new Token([\T_WHITESPACE, str_repeat($lineEnding, $expectedLineCount)]));
1✔
442
        } else {
443
            $lineBreakCount = $this->getLineBreakCount($tokens, $headerIndex, 1);
40✔
444

445
            if ($lineBreakCount < $expectedLineCount) {
40✔
446
                $missing = str_repeat($lineEnding, $expectedLineCount - $lineBreakCount);
31✔
447

448
                if ($tokens[$headerIndex + 1]->isWhitespace()) {
31✔
449
                    $tokens[$headerIndex + 1] = new Token([\T_WHITESPACE, $missing.$tokens[$headerIndex + 1]->getContent()]);
15✔
450
                } else {
451
                    $tokens->insertAt($headerIndex + 1, new Token([\T_WHITESPACE, $missing]));
17✔
452
                }
453
            } elseif ($lineBreakCount > $expectedLineCount && $tokens[$headerIndex + 1]->isWhitespace()) {
40✔
454
                $newLinesToRemove = $lineBreakCount - $expectedLineCount;
2✔
455
                $tokens[$headerIndex + 1] = new Token([
2✔
456
                    \T_WHITESPACE,
2✔
457
                    Preg::replace("/^\\R{{$newLinesToRemove}}/", '', $tokens[$headerIndex + 1]->getContent()),
2✔
458
                ]);
2✔
459
            }
460
        }
461

462
        // fix lines before header comment
463
        $expectedLineCount = 'both' === $this->configuration['separate'] || 'top' === $this->configuration['separate'] ? 2 : 1;
40✔
464
        $prev = $tokens->getPrevNonWhitespace($headerIndex);
40✔
465

466
        $regex = '/\h$/';
40✔
467

468
        if ($tokens[$prev]->isGivenKind(\T_OPEN_TAG) && Preg::match($regex, $tokens[$prev]->getContent())) {
40✔
469
            $tokens[$prev] = new Token([\T_OPEN_TAG, Preg::replace($regex, $lineEnding, $tokens[$prev]->getContent())]);
1✔
470
        }
471

472
        $lineBreakCount = $this->getLineBreakCount($tokens, $headerIndex, -1);
40✔
473

474
        if ($lineBreakCount < $expectedLineCount) {
40✔
475
            // because of the way the insert index was determined for header comment there cannot be an empty token here
476
            $tokens->insertAt($headerIndex, new Token([\T_WHITESPACE, str_repeat($lineEnding, $expectedLineCount - $lineBreakCount)]));
34✔
477
        }
478
    }
479

480
    private function getLineBreakCount(Tokens $tokens, int $index, int $direction): int
481
    {
482
        $whitespace = '';
40✔
483

484
        for ($index += $direction; isset($tokens[$index]); $index += $direction) {
40✔
485
            $token = $tokens[$index];
40✔
486

487
            if ($token->isWhitespace()) {
40✔
488
                $whitespace .= $token->getContent();
40✔
489

490
                continue;
40✔
491
            }
492

493
            if (-1 === $direction && $token->isGivenKind(\T_OPEN_TAG)) {
40✔
494
                $whitespace .= $token->getContent();
35✔
495
            }
496

497
            if ('' !== $token->getContent()) {
40✔
498
                break;
40✔
499
            }
500
        }
501

502
        return substr_count($whitespace, "\n");
40✔
503
    }
504

505
    private function removeHeader(Tokens $tokens, int $index): void
506
    {
507
        $prevIndex = $index - 1;
13✔
508
        $prevToken = $tokens[$prevIndex];
13✔
509
        $newlineRemoved = false;
13✔
510

511
        if ($prevToken->isWhitespace()) {
13✔
512
            $content = $prevToken->getContent();
11✔
513

514
            if (Preg::match('/\R/', $content)) {
11✔
515
                $newlineRemoved = true;
9✔
516
            }
517

518
            $content = Preg::replace('/\R?\h*$/', '', $content);
11✔
519

520
            $tokens->ensureWhitespaceAtIndex($prevIndex, 0, $content);
11✔
521
        }
522

523
        $nextIndex = $index + 1;
13✔
524
        $nextToken = $tokens[$nextIndex] ?? null;
13✔
525

526
        if (!$newlineRemoved && null !== $nextToken && $nextToken->isWhitespace()) {
13✔
527
            $content = Preg::replace('/^\R/', '', $nextToken->getContent());
4✔
528

529
            $tokens->ensureWhitespaceAtIndex($nextIndex, 0, $content);
4✔
530
        }
531

532
        $tokens->clearTokenAndMergeSurroundingWhitespace($index);
13✔
533
    }
534

535
    private function insertHeader(Tokens $tokens, string $headerAsComment, int $index): void
536
    {
537
        $tokens->insertAt($index, new Token([self::HEADER_COMMENT === $this->configuration['comment_type'] ? \T_COMMENT : \T_DOC_COMMENT, $headerAsComment]));
37✔
538
        $this->fixWhiteSpaceAroundHeader($tokens, $index);
37✔
539
    }
540
}
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