1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\CodeSniffer\Helper; |
6
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\File; |
8
|
|
|
use BestIt\Sniffs\Commenting\AbstractDocSniff; |
9
|
|
|
use PHP_CodeSniffer\Util\Tokens; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class DocHelper |
13
|
|
|
* |
14
|
|
|
* @package BestIt\Helper |
15
|
|
|
* @author Nick Lubisch <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class DocHelper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The php cs file. |
21
|
|
|
* |
22
|
|
|
* @var File |
23
|
|
|
*/ |
24
|
|
|
private $file; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Pointer to the token which is to be listened. |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $stackPtr; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Token stack of the current file. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $tokens; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* DocHelper constructor. |
42
|
|
|
* |
43
|
|
|
* @param File $file File object of file which is processed. |
44
|
|
|
* @param int $stackPtr Pointer to the token which is processed. |
45
|
|
|
*/ |
46
|
121 |
|
public function __construct(File $file, $stackPtr) |
47
|
|
|
{ |
48
|
121 |
|
$this->file = $file; |
49
|
121 |
|
$this->tokens = $file->getTokens(); |
50
|
121 |
|
$this->stackPtr = $stackPtr; |
51
|
121 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Checks if a comment for the class exists. |
55
|
|
|
* |
56
|
|
|
* @param int $listenerPtr Pointer of the listener token |
57
|
|
|
* @param bool $isVariable Is the current token a variable |
58
|
|
|
* |
59
|
|
|
* @return bool Indicator if the comment exists or not |
60
|
121 |
|
*/ |
61
|
|
|
public function checkCommentExists(int $listenerPtr, bool $isVariable): bool |
62
|
121 |
|
{ |
63
|
121 |
|
$listenerToken = $this->tokens[$listenerPtr]; |
64
|
|
|
$commentEndToken = $this->getCommentEndToken(); |
65
|
121 |
|
$commentExists = true; |
66
|
121 |
|
|
67
|
|
|
if ($commentEndToken['type'] !== 'T_DOC_COMMENT_CLOSE_TAG' |
68
|
4 |
|
|| ($listenerToken['line'] - 1) !== $commentEndToken['line'] |
69
|
4 |
|
) { |
70
|
4 |
|
$commentExists = false; |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
if (!$isVariable && !$commentExists) { |
74
|
4 |
|
$this->file->addError( |
75
|
|
|
AbstractDocSniff::MESSAGE_NO_IMMEDIATE_DOC_FOUND, |
76
|
|
|
$listenerPtr, |
77
|
117 |
|
AbstractDocSniff::CODE_NO_IMMEDIATE_DOC_FOUND |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $commentExists; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
117 |
|
* Checks if the comment is multi line. |
86
|
|
|
* |
87
|
117 |
|
* @param bool $isVariable Is the current token a variable |
88
|
117 |
|
* |
89
|
|
|
* @return bool Indicator if the comment is multiline |
90
|
117 |
|
*/ |
91
|
4 |
|
public function checkCommentMultiLine(bool $isVariable): bool |
92
|
4 |
|
{ |
93
|
4 |
|
$commentStart = $this->getCommentStartToken(); |
94
|
4 |
|
$commentEnd = $this->getCommentEndToken(); |
95
|
|
|
|
96
|
|
|
if (!$isVariable && $commentStart['line'] === $commentEnd['line']) { |
97
|
4 |
|
$this->file->addErrorOnLine( |
98
|
|
|
AbstractDocSniff::MESSAGE_COMMENT_NOT_MULTI_LINE, |
99
|
|
|
$commentStart['line'], |
100
|
113 |
|
AbstractDocSniff::CODE_COMMENT_NOT_MULTI_LINE |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
117 |
|
|
109
|
|
|
/** |
110
|
117 |
|
* Returns pointer to the class comment start. |
111
|
|
|
* |
112
|
117 |
|
* @return int Pointer to the class comment start. |
113
|
|
|
*/ |
114
|
|
|
public function getCommentStartPointer(): int |
115
|
|
|
{ |
116
|
|
|
$commentEndToken = $this->getCommentEndToken(); |
117
|
|
|
|
118
|
|
|
return $commentEndToken['comment_opener']; |
119
|
|
|
} |
120
|
117 |
|
|
121
|
|
|
/** |
122
|
117 |
|
* Returns token data of the evaluated class comment start. |
123
|
|
|
* |
124
|
117 |
|
* @return array Token data of the comment start. |
125
|
|
|
*/ |
126
|
|
|
public function getCommentStartToken(): array |
127
|
|
|
{ |
128
|
|
|
$commentStartPtr = $this->getCommentStartPointer(); |
129
|
|
|
|
130
|
|
|
return $this->tokens[$commentStartPtr]; |
131
|
|
|
} |
132
|
121 |
|
|
133
|
|
|
/** |
134
|
121 |
|
* Returns pointer to the class comment end. |
135
|
121 |
|
* |
136
|
121 |
|
* @return int Pointer to the class comment end. |
137
|
|
|
*/ |
138
|
|
|
public function getCommentEndPointer(): int |
139
|
121 |
|
{ |
140
|
121 |
|
$whitelistedTokens = array_merge( |
141
|
121 |
|
[T_WHITESPACE], |
142
|
121 |
|
Tokens::$methodPrefixes |
143
|
121 |
|
); |
144
|
|
|
|
145
|
|
|
return $this->file->findPrevious( |
146
|
|
|
$whitelistedTokens, |
147
|
|
|
$this->stackPtr - 1, |
148
|
|
|
null, |
149
|
|
|
true |
150
|
|
|
); |
151
|
|
|
} |
152
|
121 |
|
|
153
|
|
|
/** |
154
|
121 |
|
* Returns token data of the evaluated class comment end. |
155
|
|
|
* |
156
|
|
|
* @return array Token data of the comment end. |
157
|
|
|
*/ |
158
|
|
|
public function getCommentEndToken(): array |
159
|
|
|
{ |
160
|
|
|
return $this->tokens[$this->getCommentEndPointer()]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|