/
PhpdocOnlyAllowedAnnotationsFixer.php
120 lines (102 loc) · 3.27 KB
/
PhpdocOnlyAllowedAnnotationsFixer.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
<?php
/*
* This file is part of PHP CS Fixer: custom fixers.
*
* (c) 2018-2020 Kuba Werłos
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpCsFixerCustomFixers\Fixer;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
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 PhpdocOnlyAllowedAnnotationsFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
{
/** @var string[] */
private $elements = [];
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Only listed annotations can be in PHPDoc.',
[new CodeSample(
'<?php
/**
* @author John Doe
* @package foo
* @subpackage bar
* @version 1.0
*/
function foo_bar() {}
',
['elements' => ['author', 'version']]
)]
);
}
public function getConfigurationDefinition(): FixerConfigurationResolver
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('elements', 'list of annotations to keep in PHPDoc'))
->setAllowedTypes(['array'])
->setDefault($this->elements)
->getOption(),
]);
}
public function configure(?array $configuration = null): void
{
if (isset($configuration['elements'])) {
/** @var string[] $elements */
$elements = $configuration['elements'];
$this->elements = $elements;
}
}
/**
* Must run before NoEmptyPhpdocFixer.
* Must run after CommentToPhpdocFixer.
*/
public function getPriority(): int
{
return 4;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}
public function isRisky(): bool
{
return false;
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = $tokens->count() - 1; $index > 0; $index--) {
/** @var Token $token */
$token = $tokens[$index];
if (!$token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
$docBlock = new DocBlock($token->getContent());
foreach ($docBlock->getAnnotations() as $annotation) {
if (
Preg::match('/@([a-zA-Z0-9_\\-\\\\]+)/', $annotation->getContent(), $matches) === 1
&& \in_array($matches[1], $this->elements, true)
) {
continue;
}
$annotation->remove();
}
if ($docBlock->getContent() === '') {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
continue;
}
$tokens[$index] = new Token([T_DOC_COMMENT, $docBlock->getContent()]);
}
}
}