1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\DocTags; |
6
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\Helper\DocTagHelper; |
8
|
|
|
use BestIt\Sniffs\AbstractSniff; |
9
|
|
|
use BestIt\Sniffs\DocPosProviderTrait; |
10
|
|
|
use function in_array; |
11
|
|
|
use function substr; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Sniff to disallow the given tags. |
15
|
|
|
* |
16
|
|
|
* @author blange <[email protected]> |
17
|
|
|
* @package BestIt\Sniffs\DocTags |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractDisallowedTagsSniff extends AbstractSniff |
20
|
|
|
{ |
21
|
|
|
use DocPosProviderTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* You MUST not give one of the disallowed tags in your doc comment. |
25
|
|
|
*/ |
26
|
|
|
public const CODE_TAG_NOT_ALLOWED = 'TagNotAllowed'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Message that comment tag is not allowed. |
30
|
|
|
*/ |
31
|
|
|
private const MESSAGE_TAG_NOT_ALLOWED = 'The comment tag "%s" is not allowed.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* This tags are disallowed and could be injected from the outside. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
public array $disallowedTags = []; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The possible tags of this php structure. |
42
|
|
|
* |
43
|
|
|
* @var array|null Tag tokens. |
44
|
|
|
*/ |
45
|
|
|
private ?array $tags = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns true if the requirements for this sniff are met. |
49
|
|
|
* |
50
|
|
|
* @return bool Are the requirements met and the sniff should proceed? |
51
|
|
|
*/ |
52
|
|
|
protected function areRequirementsMet(): bool |
53
|
|
|
{ |
54
|
|
|
return $this->getDocCommentPos() && $this->getAllTags(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Checks and registers errors for the disallowed tags. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
private function checkAndRegisterDisallowedTagsError(): void |
63
|
|
|
{ |
64
|
|
|
$disallowedTags = $this->getDisallowedTags(); |
65
|
|
|
$tags = $this->getAllTags(); |
66
|
|
|
|
67
|
|
|
foreach ($tags as $tagPos => $tag) { |
68
|
|
|
$tagContent = $tag['content']; |
69
|
|
|
|
70
|
|
|
if (in_array(substr($tagContent, 1), $disallowedTags)) { |
71
|
|
|
$this->file->addError( |
72
|
|
|
self::MESSAGE_TAG_NOT_ALLOWED, |
73
|
|
|
$tagPos, |
74
|
|
|
static::CODE_TAG_NOT_ALLOWED, |
75
|
|
|
[$tagContent] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns all tag tokens for this doc block. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function getAllTags(): array |
87
|
|
|
{ |
88
|
|
|
if ($this->tags === null) { |
89
|
|
|
$this->tags = $this->loadAllTags(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->tags; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Type-safe getter for the disallowed tags. |
97
|
|
|
* |
98
|
|
|
* We need this because the ruleset user can "break" the api, if he does not provide an array with his config. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function getDisallowedTags(): array |
103
|
|
|
{ |
104
|
|
|
return $this->disallowedTags; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Loads all tags of the structures doc block. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
private function loadAllTags(): array |
113
|
|
|
{ |
114
|
|
|
return (new DocTagHelper( |
115
|
|
|
$this->file, |
116
|
|
|
$this->getDocCommentPos(), |
117
|
|
|
$this->tokens |
118
|
|
|
) |
119
|
|
|
)->getTagTokens(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Processes the token. |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
protected function processToken(): void |
128
|
|
|
{ |
129
|
|
|
$this->checkAndRegisterDisallowedTagsError(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Removes the cached data. |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
protected function tearDown(): void |
138
|
|
|
{ |
139
|
|
|
$this->resetDocCommentPos(); |
140
|
|
|
|
141
|
|
|
$this->tags = null; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|