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

keradus / PHP-CS-Fixer / 16018263876

02 Jul 2025 06:58AM UTC coverage: 94.846% (-0.002%) from 94.848%
16018263876

push

github

keradus
debug2

28193 of 29725 relevant lines covered (94.85%)

45.34 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
 * @author Antonio J. García Lagar <aj@garcialagar.es>
36
 *
37
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
38
 *
39
 * @phpstan-type _AutogeneratedInputConfiguration array{
40
 *  comment_type?: 'PHPDoc'|'comment',
41
 *  header: string,
42
 *  location?: 'after_declare_strict'|'after_open',
43
 *  separate?: 'both'|'bottom'|'none'|'top',
44
 *  validator?: null|string,
45
 * }
46
 * @phpstan-type _AutogeneratedComputedConfiguration array{
47
 *  comment_type: 'PHPDoc'|'comment',
48
 *  header: string,
49
 *  location: 'after_declare_strict'|'after_open',
50
 *  separate: 'both'|'bottom'|'none'|'top',
51
 *  validator: null|string,
52
 * }
53
 */
54
final class HeaderCommentFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
55
{
56
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
57
    use ConfigurableFixerTrait;
58

59
    /**
60
     * @internal
61
     */
62
    public const HEADER_PHPDOC = 'PHPDoc';
63

64
    /**
65
     * @internal
66
     */
67
    public const HEADER_COMMENT = 'comment';
68

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

78
namespace A\B;
79

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

90
namespace A\B;
91

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

105
namespace A\B;
106

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

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

138
/*
139
 * Comment is not wanted here.
140
 */
141

142
namespace A\B;
143

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

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

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

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

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

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

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

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

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

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

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

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

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

219
                $this->insertHeader($tokens, $headerAsComment, $headerNewIndex);
29✔
220

221
                continue;
29✔
222
            }
223

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

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

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

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

241
                continue;
12✔
242
            }
243

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

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

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

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

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

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

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

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

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

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

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

325
        return $comment.' */';
41✔
326
    }
327

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

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

336
        $next = $index + 1;
41✔
337

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

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

347
            ++$next;
9✔
348
        }
349

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

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

361
        return null;
3✔
362
    }
363

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

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

375
        $index = $tokens->getNextMeaningfulToken($openTagIndex);
41✔
376

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

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

385
        $next = $tokens->getNextMeaningfulToken($index);
15✔
386

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

391
        $next = $tokens->getNextMeaningfulToken($next);
15✔
392

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

397
        $next = $tokens->getNextMeaningfulToken($next);
14✔
398

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

403
        $next = $tokens->getNextMeaningfulToken($next);
14✔
404

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

409
        $next = $tokens->getNextMeaningfulToken($next);
14✔
410

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

415
        $next = $tokens->getNextMeaningfulToken($next);
14✔
416

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

421
        return $next + 1;
13✔
422
    }
423

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

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

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

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

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

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

464
        $regex = '/\h$/';
40✔
465

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

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

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

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

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

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

488
                continue;
40✔
489
            }
490

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

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

500
        return substr_count($whitespace, "\n");
40✔
501
    }
502

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

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

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

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

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

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

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

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

530
        $tokens->clearTokenAndMergeSurroundingWhitespace($index);
13✔
531
    }
532

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