Newer
Older
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
* 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\Phpdoc;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* Remove inheritdoc tags from classy that does not inherit.
*
* @author SpacePossum
*/
final class PhpdocNoUselessInheritdocFixer extends AbstractFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition()
{
return new FixerDefinition(
'Classy that does not inherit must not have `@inheritdoc` tags.',
[
new CodeSample("<?php\n/** {@inheritdoc} */\nclass Sample\n{\n}\n"),
new CodeSample("<?php\nclass Sample\n{\n /**\n * @inheritdoc\n */\n public function Test()\n {\n }\n}\n"),
]
);
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
// Should run before NoEmptyPhpdocFixer, NoTrailingWhitespaceInCommentFixer
// and after PhpdocToCommentFixer.
return 6;
}
/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_DOC_COMMENT) && $tokens->isAnyTokenKindsFound([T_CLASS, T_INTERFACE]);
}
/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
// min. offset 4 as minimal candidate is @: <?php\n/** @inheritdoc */class min{}
for ($index = 1, $count = \count($tokens) - 4; $index < $count; ++$index) {
if ($tokens[$index]->isGivenKind([T_CLASS, T_INTERFACE])) {
$index = $this->fixClassy($tokens, $index);
}
}
}
/**
* @param Tokens $tokens
* @param int $index
*
* @return int
*/
private function fixClassy(Tokens $tokens, $index)
{
// figure out where the classy starts
$classOpenIndex = $tokens->getNextTokenOfKind($index, ['{']);
// figure out where the classy ends
$classEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classOpenIndex);
// is classy extending or implementing some interface
$extendingOrImplementing = $this->isExtendingOrImplementing($tokens, $index, $classOpenIndex);
if (!$extendingOrImplementing) {
// PHPDoc of classy should not have inherit tag even when using traits as Traits cannot provide this information
$this->fixClassyOutside($tokens, $index);
}
// figure out if the classy uses a trait
if (!$extendingOrImplementing && $this->isUsingTrait($tokens, $index, $classOpenIndex, $classEndIndex)) {
$extendingOrImplementing = true;
}
$this->fixClassyInside($tokens, $classOpenIndex, $classEndIndex, !$extendingOrImplementing);
return $classEndIndex;
}
/**
* @param Tokens $tokens
* @param int $classOpenIndex
* @param int $classEndIndex
* @param bool $fixThisLevel
*/
private function fixClassyInside(Tokens $tokens, $classOpenIndex, $classEndIndex, $fixThisLevel)
{
for ($i = $classOpenIndex; $i < $classEndIndex; ++$i) {
if ($tokens[$i]->isGivenKind(T_CLASS)) {
$i = $this->fixClassy($tokens, $i);
} elseif ($fixThisLevel && $tokens[$i]->isGivenKind(T_DOC_COMMENT)) {
$this->fixToken($tokens, $i);
}
}
}
/**
* @param Tokens $tokens
* @param int $classIndex
*/
private function fixClassyOutside(Tokens $tokens, $classIndex)
{
$previousIndex = $tokens->getPrevNonWhitespace($classIndex);
if ($tokens[$previousIndex]->isGivenKind(T_DOC_COMMENT)) {
$this->fixToken($tokens, $previousIndex);
}
}
/**
* @param Tokens $tokens
* @param int $tokenIndex
*/
private function fixToken(Tokens $tokens, $tokenIndex)
{
$count = 0;
$content = Preg::replaceCallback(
'#(\h*(?:@{*|{*\h*@)\h*inheritdoc\h*)([^}]*)((?:}*)\h*)#i',
static function ($matches) {
return ' '.$matches[2];
},
$tokens[$tokenIndex]->getContent(),
-1,
$count
);
if ($count) {
$tokens[$tokenIndex] = new Token([T_DOC_COMMENT, $content]);
}
}
/**
* @param Tokens $tokens
* @param int $classIndex
* @param int $classOpenIndex
*
* @return bool
*/
private function isExtendingOrImplementing(Tokens $tokens, $classIndex, $classOpenIndex)
{
for ($index = $classIndex; $index < $classOpenIndex; ++$index) {
if ($tokens[$index]->isGivenKind([T_EXTENDS, T_IMPLEMENTS])) {
return true;
}
}
return false;
}
/**
* @param Tokens $tokens
* @param int $classIndex
* @param int $classOpenIndex
* @param int $classCloseIndex
*
* @return bool
*/
private function isUsingTrait(Tokens $tokens, $classIndex, $classOpenIndex, $classCloseIndex)
{
if ($tokens[$classIndex]->isGivenKind(T_INTERFACE)) {
// cannot use Trait inside an interface
return false;
}
$useIndex = $tokens->getNextTokenOfKind($classOpenIndex, [[CT::T_USE_TRAIT]]);
return null !== $useIndex && $useIndex < $classCloseIndex;
}
}