forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpdocNoUselessInheritdocFixer.php
151 lines (127 loc) · 5.15 KB
/
PhpdocNoUselessInheritdocFixer.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
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
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* 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\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* Remove inheritdoc tags from classy that does not inherit.
*/
final class PhpdocNoUselessInheritdocFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
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}
*
* Must run before NoEmptyPhpdocFixer, NoTrailingWhitespaceInCommentFixer, PhpdocAlignFixer.
* Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
*/
public function getPriority(): int
{
return 6;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT) && $tokens->isAnyTokenKindsFound([T_CLASS, T_INTERFACE]);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
// 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);
}
}
}
private function fixClassy(Tokens $tokens, int $index): int
{
// 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;
}
private function fixClassyInside(Tokens $tokens, int $classOpenIndex, int $classEndIndex, bool $fixThisLevel): void
{
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);
}
}
}
private function fixClassyOutside(Tokens $tokens, int $classIndex): void
{
$previousIndex = $tokens->getPrevNonWhitespace($classIndex);
if ($tokens[$previousIndex]->isGivenKind(T_DOC_COMMENT)) {
$this->fixToken($tokens, $previousIndex);
}
}
private function fixToken(Tokens $tokens, int $tokenIndex): void
{
$count = 0;
$content = Preg::replaceCallback(
'#(\h*(?:@{*|{*\h*@)\h*inheritdoc\h*)([^}]*)((?:}*)\h*)#i',
static fn (array $matches): string => ' '.$matches[2],
$tokens[$tokenIndex]->getContent(),
-1,
$count
);
if ($count > 0) {
$tokens[$tokenIndex] = new Token([T_DOC_COMMENT, $content]);
}
}
private function isExtendingOrImplementing(Tokens $tokens, int $classIndex, int $classOpenIndex): bool
{
for ($index = $classIndex; $index < $classOpenIndex; ++$index) {
if ($tokens[$index]->isGivenKind([T_EXTENDS, T_IMPLEMENTS])) {
return true;
}
}
return false;
}
private function isUsingTrait(Tokens $tokens, int $classIndex, int $classOpenIndex, int $classCloseIndex): bool
{
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;
}
}