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

NexusPHP / cs-config / 14655342589

23 Feb 2025 03:16PM UTC coverage: 95.644%. Remained the same
14655342589

push

github

paulbalandan
Update build badge

1076 of 1125 relevant lines covered (95.64%)

63.51 hits per line

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

96.55
/src/Fixer/Comment/NoCodeSeparatorCommentFixer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Nexus CS Config.
7
 *
8
 * (c) 2020 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Nexus\CsConfig\Fixer\Comment;
15

16
use Nexus\CsConfig\Fixer\AbstractCustomFixer;
17
use PhpCsFixer\FixerDefinition\CodeSample;
18
use PhpCsFixer\FixerDefinition\FixerDefinition;
19
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
20
use PhpCsFixer\Tokenizer\Token;
21
use PhpCsFixer\Tokenizer\Tokens;
22

23
/**
24
 * Removes code separator comments except when used as section boundary.
25
 */
26
final class NoCodeSeparatorCommentFixer extends AbstractCustomFixer
27
{
28
    public function getDefinition(): FixerDefinitionInterface
29
    {
30
        return new FixerDefinition(
12✔
31
            'There should not be any code separator comments.',
12✔
32
            [new CodeSample(
12✔
33
                <<<'EOF'
12✔
34
                    <?php
35

36
                    $code = 'a';
37

38
                    //------------------------
39

40
                    $arr = [];
41

42
                    EOF,
12✔
43
            )],
12✔
44
        );
12✔
45
    }
46

47
    public function isCandidate(Tokens $tokens): bool
48
    {
49
        return $tokens->isTokenKindFound(T_COMMENT);
16✔
50
    }
51

52
    /**
53
     * {@inheritDoc}
54
     *
55
     * Must run before NoEmptyCommentFixer, SpaceAfterCommentStartFixer
56
     */
57
    public function getPriority(): int
58
    {
59
        return 2;
×
60
    }
61

62
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
63
    {
64
        for ($index = 1, $count = $tokens->count(); $index < $count; ++$index) {
16✔
65
            /** @var Token $token */
66
            $token = $tokens[$index];
16✔
67

68
            if (! $token->isGivenKind(T_COMMENT)) {
16✔
69
                continue;
16✔
70
            }
71

72
            if (! $this->isCodeSeparatorComment($token->getContent())) {
16✔
73
                continue;
8✔
74
            }
75

76
            if ($this->isCommentBlockBoundary($tokens, $index)) {
12✔
77
                continue;
4✔
78
            }
79

80
            $tokens->removeLeadingWhitespace($index);
12✔
81
            $tokens->clearTokenAndMergeSurroundingWhitespace($index);
12✔
82
        }
83
    }
84

85
    /**
86
     * Checks if the recurring code separator comment is part of a comment
87
     * boundary that serves as a logical division between sections of code.
88
     *
89
     * ```
90
     * //================================== <-- this is used as a boundary
91
     * // SECTION
92
     * //================================== <-- this is used as a boundary
93
     *
94
     * //================================== <-- this is NOT a boundary
95
     *
96
     * ```
97
     */
98
    private function isCommentBlockBoundary(Tokens $tokens, int $index): bool
99
    {
100
        $prevIndex = $tokens->getPrevNonWhitespace($index);
12✔
101
        $nextIndex = $tokens->getNextNonWhitespace($index);
12✔
102

103
        /** @var Token $prevToken */
104
        $prevToken = $tokens[$prevIndex];
12✔
105
        $prevTokenIsRegularComment = $prevToken->isGivenKind(T_COMMENT)
12✔
106
            && ! $this->isCodeSeparatorComment($prevToken->getContent());
12✔
107

108
        /** @var Token $nextToken */
109
        $nextToken = $tokens[$nextIndex];
12✔
110
        $nextTokenIsRegularComment = $nextToken->isGivenKind(T_COMMENT)
12✔
111
            && ! $this->isCodeSeparatorComment($nextToken->getContent());
12✔
112

113
        return $prevTokenIsRegularComment || $nextTokenIsRegularComment;
12✔
114
    }
115

116
    private function isCodeSeparatorComment(string $comment): bool
117
    {
118
        return preg_match('/^\/\/\s*[-|=]+$/', $comment) === 1;
16✔
119
    }
120
}
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