1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\CodeSniffer\Helper; |
6
|
|
|
|
7
|
|
|
use PHP_CodeSniffer\Files\File; |
8
|
|
|
use function array_key_exists; |
9
|
|
|
use function in_array; |
10
|
|
|
use const T_DOC_COMMENT_CLOSE_TAG; |
11
|
|
|
use const T_DOC_COMMENT_STRING; |
12
|
|
|
use const T_DOC_COMMENT_TAG; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DocTagHelper |
16
|
|
|
* |
17
|
|
|
* @author Nick Lubisch <[email protected]> |
18
|
|
|
* @package BestIt\CodeSniffer\Helper |
19
|
|
|
*/ |
20
|
|
|
class DocTagHelper |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The stating token of the comment. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private array $token; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The PHP CS file |
31
|
|
|
* |
32
|
|
|
* @var File |
33
|
|
|
*/ |
34
|
|
|
private File $file; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The token stack from php cs file. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private array $tokens; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* DocTagHelper constructor. |
45
|
|
|
* |
46
|
|
|
* @param File $file The php cs file |
47
|
|
|
* @param int $stackPos Position to the token which is to be listened |
48
|
|
|
* @param array $tokens Another token array if we want to overwrite them. |
49
|
|
|
*/ |
50
|
|
|
public function __construct(File $file, int $stackPos, array $tokens = []) |
51
|
|
|
{ |
52
|
|
|
$this->file = $file; |
53
|
|
|
$this->tokens = $tokens ?: $file->getTokens(); |
54
|
|
|
$this->token = $this->tokens[$stackPos]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the comment start token. |
59
|
|
|
* |
60
|
|
|
* @return array the token. |
61
|
|
|
*/ |
62
|
|
|
private function getCommentStartToken(): array |
63
|
|
|
{ |
64
|
|
|
return $this->token; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
121 |
|
* Loads the tag content for the given tag position. |
69
|
|
|
* |
70
|
121 |
|
* @param int $tagPosition The position of the tag. |
71
|
121 |
|
* @param int $iteratedPosition |
72
|
121 |
|
* |
73
|
121 |
|
* @return array The content tokens of the tag. |
74
|
121 |
|
*/ |
75
|
|
|
private function loadTagContentTokens(int $tagPosition, int &$iteratedPosition): array |
76
|
|
|
{ |
77
|
|
|
$contents = []; |
78
|
|
|
$myColumn = $this->tokens[$tagPosition]['column']; |
79
|
|
|
$closingPos = $this->file->findNext([T_DOC_COMMENT_CLOSE_TAG], $position = $tagPosition + 1); |
80
|
|
|
|
81
|
|
|
while ($position < $closingPos) { |
82
|
|
|
$contentToken = $this->tokens[$position++]; |
83
|
|
|
|
84
|
113 |
|
if (($contentToken['code'] === T_DOC_COMMENT_TAG) && ($contentToken['column'] <= $myColumn)) { |
85
|
|
|
break; |
86
|
113 |
|
} |
87
|
113 |
|
|
88
|
|
|
if (in_array($contentToken['code'], [T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG])) { |
89
|
113 |
|
$contents[$position] = $contentToken; |
90
|
113 |
|
} |
91
|
|
|
|
92
|
113 |
|
$iteratedPosition = $position; |
93
|
113 |
|
} |
94
|
113 |
|
|
95
|
|
|
return $contents; |
96
|
113 |
|
} |
97
|
113 |
|
|
98
|
|
|
/** |
99
|
16 |
|
* Returns array of all comment tag tokens. |
100
|
|
|
* |
101
|
|
|
* @return array List of all comment tag tokens indexed by token position |
102
|
100 |
|
*/ |
103
|
100 |
|
public function getTagTokens(): array |
104
|
|
|
{ |
105
|
|
|
$iteratedPos = 0; |
106
|
|
|
$tagPositions = $this->getCommentStartToken()['comment_tags']; |
107
|
|
|
$tagTokens = []; |
108
|
|
|
|
109
|
|
|
/** @var int $tagPos */ |
110
|
100 |
|
foreach ($tagPositions as $tagPos) { |
111
|
|
|
if ($tagPos >= $iteratedPos) { |
112
|
100 |
|
$tagTokens[$tagPos] = $this->tokens[$tagPos] + [ |
113
|
|
|
'contents' => $this->loadTagContentTokens($tagPos, $iteratedPos) |
114
|
100 |
|
]; |
115
|
100 |
|
} |
116
|
|
|
} |
117
|
100 |
|
|
118
|
100 |
|
return $tagTokens; |
119
|
62 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
100 |
|
* Returns the individual count of every tag. |
123
|
100 |
|
* |
124
|
|
|
* @param array $tagTokens Array of tag tokens. |
125
|
|
|
* |
126
|
100 |
|
* @return array List of comment tags with there count of the current comment |
127
|
100 |
|
*/ |
128
|
|
|
public function getTagCounts(array $tagTokens): array |
129
|
|
|
{ |
130
|
|
|
$tagCounts = []; |
131
|
|
|
|
132
|
|
|
foreach ($tagTokens as $tagToken) { |
133
|
|
|
$tagName = $tagToken['content']; |
134
|
|
|
|
135
|
|
|
if (!array_key_exists($tagName, $tagCounts)) { |
136
|
|
|
$tagCounts[$tagName] = 0; |
137
|
|
|
} |
138
|
62 |
|
|
139
|
|
|
++$tagCounts[$tagName]; |
140
|
62 |
|
} |
141
|
62 |
|
|
142
|
|
|
return $tagCounts; |
143
|
62 |
|
} |
144
|
|
|
} |
145
|
|
|
|