3
* A class to represent Comments of a doc comment.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
16
if (class_exists('PHP_CodeSniffer_CommentParser_SingleElement', true) === false) {
17
$error = 'Class PHP_CodeSniffer_CommentParser_SingleElement not found';
18
throw new PHP_CodeSniffer_Exception($error);
22
* A class to represent Comments of a doc comment.
24
* Comments are in the following format.
26
* /** <--this is the start of the comment.
27
* * This is a short comment description
29
* * This is a long comment description
30
* * <-- this is the end of the comment
35
* Note that the sentence before two newlines is assumed
36
* the short comment description.
39
* @package PHP_CodeSniffer
40
* @author Greg Sherwood <gsherwood@squiz.net>
41
* @author Marc McIntyre <mmcintyre@squiz.net>
42
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
43
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
44
* @version Release: 1.5.3
45
* @link http://pear.php.net/package/PHP_CodeSniffer
47
class PHP_CodeSniffer_CommentParser_CommentElement extends PHP_CodeSniffer_CommentParser_SingleElement
52
* Constructs a PHP_CodeSniffer_CommentParser_CommentElement.
54
* @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
59
* @param array $tokens The tokens
63
* @param PHP_CodeSniffer_File $phpcsFile The file
68
public function __construct(
71
PHP_CodeSniffer_File $phpcsFile
73
parent::__construct($previousElement, $tokens, 'comment', $phpcsFile);
79
* Returns the short comment description.
82
* @see getLongComment()
84
public function getShortComment()
86
$pos = $this->_getShortCommentEndPos();
91
return implode('', array_slice($this->tokens, 0, ($pos + 1)));
93
}//end getShortComment()
97
* Returns the last token position of the short comment description.
99
* @return int The last token position of the short comment description
100
* @see _getLongCommentStartPos()
102
private function _getShortCommentEndPos()
110
foreach ($this->tokens as $pos => $token) {
111
$token = str_replace($whiteSpace, '', $token);
112
if ($token === $this->phpcsFile->eolChar) {
113
if ($found === false) {
114
// Include newlines before short description.
117
if (isset($this->tokens[($pos + 1)]) === true) {
118
if ($this->tokens[($pos + 1)] === $this->phpcsFile->eolChar) {
130
return (count($this->tokens) - 1);
132
}//end _getShortCommentEndPos()
136
* Returns the long comment description.
139
* @see getShortComment
141
public function getLongComment()
143
$start = $this->_getLongCommentStartPos();
148
return implode('', array_slice($this->tokens, $start));
150
}//end getLongComment()
154
* Returns the start position of the long comment description.
156
* Returns -1 if there is no long comment.
158
* @return int The start position of the long comment description.
159
* @see _getShortCommentEndPos()
161
private function _getLongCommentStartPos()
163
$pos = ($this->_getShortCommentEndPos() + 1);
164
if ($pos === (count($this->tokens) - 1)) {
168
$count = count($this->tokens);
169
for ($i = $pos; $i < $count; $i++) {
170
$content = trim($this->tokens[$i]);
171
if ($content !== '') {
172
if ($content{0} === '@') {
182
}//end _getLongCommentStartPos()
186
* Returns the whitespace that exists between
187
* the short and the long comment description.
191
public function getWhiteSpaceBetween()
193
$endShort = ($this->_getShortCommentEndPos() + 1);
194
$startLong = ($this->_getLongCommentStartPos() - 1);
195
if ($startLong === -1) {
201
array_slice($this->tokens, $endShort, ($startLong - $endShort))
204
}//end getWhiteSpaceBetween()
208
* Returns the number of newlines that exist before the tags.
212
public function getNewlineAfter()
214
$long = $this->getLongComment();
216
$long = rtrim($long, ' ');
217
$long = strrev($long);
218
$newlines = strspn($long, $this->phpcsFile->eolChar);
220
$endShort = ($this->_getShortCommentEndPos() + 1);
221
$after = implode('', array_slice($this->tokens, $endShort));
222
$after = trim($after, ' ');
223
$newlines = strspn($after, $this->phpcsFile->eolChar);
226
return ($newlines / strlen($this->phpcsFile->eolChar));
228
}//end getNewlineAfter()
232
* Returns true if there is no comment.
236
public function isEmpty()
238
return (trim($this->getContent()) === '');