-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SingleLineCommentSpacingFixer.php
110 lines (92 loc) · 3.19 KB
/
SingleLineCommentSpacingFixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\Comment;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
final class SingleLineCommentSpacingFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Single-line comments must have proper spacing.',
[
new CodeSample(
'<?php
//comment 1
#comment 2
/*comment 3*/
'
),
]
);
}
/**
* {@inheritdoc}
*
* Must run after PhpdocToCommentFixer.
*/
public function getPriority(): int
{
return 1;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_COMMENT);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = \count($tokens) - 1; 0 <= $index; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_COMMENT)) {
continue;
}
$content = $token->getContent();
$contentLength = \strlen($content);
if ('/' === $content[0]) {
if ($contentLength < 3) {
continue; // cheap check for "//"
}
if ('*' === $content[1]) { // slash asterisk comment
if ($contentLength < 5 || '*' === $content[2] || str_contains($content, "\n")) {
continue; // cheap check for "/**/", comment that looks like a PHPDoc, or multi line comment
}
$newContent = rtrim(substr($content, 0, -2)).' '.substr($content, -2);
$newContent = $this->fixCommentLeadingSpace($newContent, '/*');
} else { // double slash comment
$newContent = $this->fixCommentLeadingSpace($content, '//');
}
} else { // hash comment
if ($contentLength < 2 || '[' === $content[1]) { // cheap check for "#" or annotation (like) comment
continue;
}
$newContent = $this->fixCommentLeadingSpace($content, '#');
}
if ($newContent !== $content) {
$tokens[$index] = new Token([T_COMMENT, $newContent]);
}
}
}
// fix space between comment open and leading text
private function fixCommentLeadingSpace(string $content, string $prefix): string
{
if (Preg::match(sprintf('@^%s\h+.*$@', preg_quote($prefix, '@')), $content)) {
return $content;
}
$position = \strlen($prefix);
return substr($content, 0, $position).' '.substr($content, $position);
}
}