3
* A class to represent param tags within a function comment.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2012 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_AbstractDocElement', true) === false) {
17
$error = 'Class PHP_CodeSniffer_CommentParser_AbstractDocElement not found';
18
throw new PHP_CodeSniffer_Exception($error);
22
* A class to represent param tags within a function comment.
25
* @package PHP_CodeSniffer
26
* @author Greg Sherwood <gsherwood@squiz.net>
27
* @author Marc McIntyre <mmcintyre@squiz.net>
28
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
29
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
30
* @version Release: 1.5.0RC2
31
* @link http://pear.php.net/package/PHP_CodeSniffer
33
class PHP_CodeSniffer_CommentParser_ParameterElement extends PHP_CodeSniffer_CommentParser_AbstractDocElement
37
* The variable name of this parameter name, including the $ sign.
41
private $_varName = '';
44
* The comment of this parameter tag.
48
private $_comment = '';
51
* The variable type of this parameter tag.
58
* The whitespace that exists before the variable name.
62
private $_varNameWhitespace = '';
65
* The whitespace that exists before the comment.
69
private $_commentWhitespace = null;
72
* The whitespace that exists before the variable type.
76
private $_typeWhitespace = '';
80
* Constructs a PHP_CodeSniffer_CommentParser_ParameterElement.
82
* @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
85
* @param array $tokens The tokens
88
* @param PHP_CodeSniffer_File $phpcsFile The file that
92
public function __construct(
95
PHP_CodeSniffer_File $phpcsFile
97
parent::__construct($previousElement, $tokens, 'param', $phpcsFile);
99
// Handle special variable type: array(x => y).
100
$type = strtolower($this->_type);
101
if ($this->_varName === '=>' && strpos($type, 'array(') !== false) {
102
$rawContent = $this->getRawContent();
104
$pattern = '/^(\s+)(array\(.*\))(\s+)(\$\S*)(\s+)(.*)/i';
105
if (preg_match($pattern, $rawContent, $matches) !== 0) {
106
// Process the sub elements correctly for this special case.
107
if (count($matches) === 7) {
108
$this->processSubElement('type', $matches[2], $matches[1]);
109
$this->processSubElement('varName', $matches[4], $matches[3]);
110
$this->processSubElement('comment', $matches[6], $matches[5]);
119
* Returns the element names that this tag is comprised of, in the order
120
* that they appear in the tag.
122
* @return array(string)
123
* @see processSubElement()
125
protected function getSubElements()
133
}//end getSubElements()
137
* Processes the sub element with the specified name.
139
* @param string $name The name of the sub element to process.
140
* @param string $content The content of this sub element.
141
* @param string $beforeWhitespace The whitespace that exists before the
145
* @see getSubElements()
147
protected function processSubElement($name, $content, $beforeWhitespace)
149
$element = '_'.$name;
150
$whitespace = $element.'Whitespace';
151
$this->$element = $content;
152
$this->$whitespace = $beforeWhitespace;
154
}//end processSubElement()
158
* Returns the variable name that this parameter tag represents.
162
public function getVarName()
164
return $this->_varName;
170
* Returns the variable type that this string represents.
174
public function getType()
182
* Returns the comment of this comment for this parameter.
186
public function getComment()
188
return $this->_comment;
194
* Returns the whitespace before the variable type.
197
* @see getWhiteSpaceBeforeVarName()
198
* @see getWhiteSpaceBeforeComment()
200
public function getWhiteSpaceBeforeType()
202
return $this->_typeWhitespace;
204
}//end getWhiteSpaceBeforeType()
208
* Returns the whitespace before the variable name.
211
* @see getWhiteSpaceBeforeComment()
212
* @see getWhiteSpaceBeforeType()
214
public function getWhiteSpaceBeforeVarName()
216
return $this->_varNameWhitespace;
218
}//end getWhiteSpaceBeforeVarName()
222
* Returns the whitespace before the comment.
225
* @see getWhiteSpaceBeforeVarName()
226
* @see getWhiteSpaceBeforeType()
228
public function getWhiteSpaceBeforeComment()
230
return $this->_commentWhitespace;
232
}//end getWhiteSpaceBeforeComment()
236
* Returns the position of this parameter are it appears in the comment.
238
* This method differs from getOrder as it is only relative to method
243
public function getPosition()
245
if (($this->getPreviousElement() instanceof PHP_CodeSniffer_CommentParser_ParameterElement) === false) {
248
return ($this->getPreviousElement()->getPosition() + 1);
255
* Returns true if this parameter's variable aligns with the other's.
257
* @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
263
public function alignsVariableWith(
264
PHP_CodeSniffer_CommentParser_ParameterElement $other
267
// @param type $variable Comment.
268
// @param <-a-><---b---->
269
// Compares the index before param variable.
270
$otherVar = (strlen($other->_type) + strlen($other->_varNameWhitespace));
271
$thisVar = (strlen($this->_type) + strlen($this->_varNameWhitespace));
272
if ($otherVar !== $thisVar) {
278
}//end alignsVariableWith()
282
* Returns true if this parameter's comment aligns with the other's.
284
* @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
290
public function alignsCommentWith(
291
PHP_CodeSniffer_CommentParser_ParameterElement $other
293
// Compares the index before param comment.
294
if (strlen($other->_commentWhitespace) === 0 && strlen($this->_commentWhitespace) === 0) {
299
= (strlen($other->_varName) + strlen($other->_commentWhitespace));
301
= (strlen($this->_varName) + strlen($this->_commentWhitespace));
303
if ($otherComment !== $thisComment) {
309
}//end alignsCommentWith()
313
* Returns true if this parameter aligns with the other paramter.
315
* @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
321
public function alignsWith(PHP_CodeSniffer_CommentParser_ParameterElement $other)
323
if ($this->alignsVariableWith($other) === false) {
327
if ($this->alignsCommentWith($other) === false) {