| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* A class to represent param tags within a function comment.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: ParameterElement.php 270281 2008-12-02 02:38:34Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
if (class_exists('PHP_CodeSniffer_CommentParser_AbstractDocElement', true) === false) {
|
|
|
18 |
$error = 'Class PHP_CodeSniffer_CommentParser_AbstractDocElement not found';
|
|
|
19 |
throw new PHP_CodeSniffer_Exception($error);
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* A class to represent param tags within a function comment.
|
|
|
24 |
*
|
|
|
25 |
* @category PHP
|
|
|
26 |
* @package PHP_CodeSniffer
|
|
|
27 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
28 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
29 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
30 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
31 |
* @version Release: 1.2.1
|
|
|
32 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
33 |
*/
|
|
|
34 |
class PHP_CodeSniffer_CommentParser_ParameterElement extends PHP_CodeSniffer_CommentParser_AbstractDocElement
|
|
|
35 |
{
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* The variable name of this parameter name, including the $ sign.
|
|
|
39 |
*
|
|
|
40 |
* @var string
|
|
|
41 |
*/
|
|
|
42 |
private $_varName = '';
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* The comment of this parameter tag.
|
|
|
46 |
*
|
|
|
47 |
* @var string
|
|
|
48 |
*/
|
|
|
49 |
private $_comment = '';
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* The variable type of this parameter tag.
|
|
|
53 |
*
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
private $_type = '';
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* The whitespace that exists before the variable name.
|
|
|
60 |
*
|
|
|
61 |
* @var string
|
|
|
62 |
*/
|
|
|
63 |
private $_varNameWhitespace = '';
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* The whitespace that exists before the comment.
|
|
|
67 |
*
|
|
|
68 |
* @var string
|
|
|
69 |
*/
|
|
|
70 |
private $_commentWhitespace = null;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* The whitespace that exists before the variable type.
|
|
|
74 |
*
|
|
|
75 |
* @var string
|
|
|
76 |
*/
|
|
|
77 |
private $_typeWhitespace = '';
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Constructs a PHP_CodeSniffer_CommentParser_ParameterElement.
|
|
|
82 |
*
|
|
|
83 |
* @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
|
|
|
84 |
* previous to
|
|
|
85 |
* this one.
|
|
|
86 |
* @param array $tokens The tokens
|
|
|
87 |
* that make up
|
|
|
88 |
* this element.
|
|
|
89 |
* @param PHP_CodeSniffer_File $phpcsFile The file that
|
|
|
90 |
* this element
|
|
|
91 |
* is in.
|
|
|
92 |
*/
|
|
|
93 |
public function __construct(
|
|
|
94 |
$previousElement,
|
|
|
95 |
$tokens,
|
|
|
96 |
PHP_CodeSniffer_File $phpcsFile
|
|
|
97 |
) {
|
|
|
98 |
parent::__construct($previousElement, $tokens, 'param', $phpcsFile);
|
|
|
99 |
|
|
|
100 |
// Handle special variable type: array(x => y).
|
|
|
101 |
$type = strtolower($this->_type);
|
|
|
102 |
if ($this->_varName === '=>' && strpos($type, 'array(') !== false) {
|
|
|
103 |
$rawContent = $this->getRawContent();
|
|
|
104 |
$matches = array();
|
|
|
105 |
$pattern = '/^(\s+)(array\(.*\))(\s+)(\$\S*)(\s+)(.*)/i';
|
|
|
106 |
if (preg_match($pattern, $rawContent, $matches) !== 0) {
|
|
|
107 |
// Process the sub elements correctly for this special case.
|
|
|
108 |
if (count($matches) === 7) {
|
|
|
109 |
$this->processSubElement('type', $matches[2], $matches[1]);
|
|
|
110 |
$this->processSubElement('varName', $matches[4], $matches[3]);
|
|
|
111 |
$this->processSubElement('comment', $matches[6], $matches[5]);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
}//end __construct()
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Returns the element names that this tag is comprised of, in the order
|
|
|
121 |
* that they appear in the tag.
|
|
|
122 |
*
|
|
|
123 |
* @return array(string)
|
|
|
124 |
* @see processSubElement()
|
|
|
125 |
*/
|
|
|
126 |
protected function getSubElements()
|
|
|
127 |
{
|
|
|
128 |
return array(
|
|
|
129 |
'type',
|
|
|
130 |
'varName',
|
|
|
131 |
'comment',
|
|
|
132 |
);
|
|
|
133 |
|
|
|
134 |
}//end getSubElements()
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Processes the sub element with the specified name.
|
|
|
139 |
*
|
|
|
140 |
* @param string $name The name of the sub element to process.
|
|
|
141 |
* @param string $content The content of this sub element.
|
|
|
142 |
* @param string $beforeWhitespace The whitespace that exists before the
|
|
|
143 |
* sub element.
|
|
|
144 |
*
|
|
|
145 |
* @return void
|
|
|
146 |
* @see getSubElements()
|
|
|
147 |
*/
|
|
|
148 |
protected function processSubElement($name, $content, $beforeWhitespace)
|
|
|
149 |
{
|
|
|
150 |
$element = '_'.$name;
|
|
|
151 |
$whitespace = $element.'Whitespace';
|
|
|
152 |
$this->$element = $content;
|
|
|
153 |
$this->$whitespace = $beforeWhitespace;
|
|
|
154 |
|
|
|
155 |
}//end processSubElement()
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Returns the variable name that this parameter tag represents.
|
|
|
160 |
*
|
|
|
161 |
* @return string
|
|
|
162 |
*/
|
|
|
163 |
public function getVarName()
|
|
|
164 |
{
|
|
|
165 |
return $this->_varName;
|
|
|
166 |
|
|
|
167 |
}//end getVarName()
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Returns the variable type that this string represents.
|
|
|
172 |
*
|
|
|
173 |
* @return string
|
|
|
174 |
*/
|
|
|
175 |
public function getType()
|
|
|
176 |
{
|
|
|
177 |
return $this->_type;
|
|
|
178 |
|
|
|
179 |
}//end getType()
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Returns the comment of this comment for this parameter.
|
|
|
184 |
*
|
|
|
185 |
* @return string
|
|
|
186 |
*/
|
|
|
187 |
public function getComment()
|
|
|
188 |
{
|
|
|
189 |
return $this->_comment;
|
|
|
190 |
|
|
|
191 |
}//end getComment()
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Returns the whitespace before the variable type.
|
|
|
196 |
*
|
|
|
197 |
* @return stirng
|
|
|
198 |
* @see getWhiteSpaceBeforeVarName()
|
|
|
199 |
* @see getWhiteSpaceBeforeComment()
|
|
|
200 |
*/
|
|
|
201 |
public function getWhiteSpaceBeforeType()
|
|
|
202 |
{
|
|
|
203 |
return $this->_typeWhitespace;
|
|
|
204 |
|
|
|
205 |
}//end getWhiteSpaceBeforeType()
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Returns the whitespace before the variable name.
|
|
|
210 |
*
|
|
|
211 |
* @return string
|
|
|
212 |
* @see getWhiteSpaceBeforeComment()
|
|
|
213 |
* @see getWhiteSpaceBeforeType()
|
|
|
214 |
*/
|
|
|
215 |
public function getWhiteSpaceBeforeVarName()
|
|
|
216 |
{
|
|
|
217 |
return $this->_varNameWhitespace;
|
|
|
218 |
|
|
|
219 |
}//end getWhiteSpaceBeforeVarName()
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Returns the whitespace before the comment.
|
|
|
224 |
*
|
|
|
225 |
* @return string
|
|
|
226 |
* @see getWhiteSpaceBeforeVarName()
|
|
|
227 |
* @see getWhiteSpaceBeforeType()
|
|
|
228 |
*/
|
|
|
229 |
public function getWhiteSpaceBeforeComment()
|
|
|
230 |
{
|
|
|
231 |
return $this->_commentWhitespace;
|
|
|
232 |
|
|
|
233 |
}//end getWhiteSpaceBeforeComment()
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Returns the postition of this parameter are it appears in the comment.
|
|
|
238 |
*
|
|
|
239 |
* This method differs from getOrder as it is only relative to method
|
|
|
240 |
* parameters.
|
|
|
241 |
*
|
|
|
242 |
* @return int
|
|
|
243 |
*/
|
|
|
244 |
public function getPosition()
|
|
|
245 |
{
|
|
|
246 |
if (($this->getPreviousElement() instanceof PHP_CodeSniffer_CommentParser_ParameterElement) === false) {
|
|
|
247 |
return 1;
|
|
|
248 |
} else {
|
|
|
249 |
return ($this->getPreviousElement()->getPosition() + 1);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
}//end getPosition()
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Returns true if this parameter's variable aligns with the other's.
|
|
|
257 |
*
|
|
|
258 |
* @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
|
|
|
259 |
* to check
|
|
|
260 |
* alignment with.
|
|
|
261 |
*
|
|
|
262 |
* @return boolean
|
|
|
263 |
*/
|
|
|
264 |
public function alignsVariableWith(
|
|
|
265 |
PHP_CodeSniffer_CommentParser_ParameterElement $other
|
|
|
266 |
) {
|
|
|
267 |
// Format is:
|
|
|
268 |
// @param type $variable Comment.
|
|
|
269 |
// @param <-a-><---b---->
|
|
|
270 |
// Compares the index before param variable.
|
|
|
271 |
$otherVar = (strlen($other->_type) + strlen($other->_varNameWhitespace));
|
|
|
272 |
$thisVar = (strlen($this->_type) + strlen($this->_varNameWhitespace));
|
|
|
273 |
if ($otherVar !== $thisVar) {
|
|
|
274 |
return false;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
return true;
|
|
|
278 |
|
|
|
279 |
}//end alignsVariableWith()
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Returns true if this parameter's comment aligns with the other's.
|
|
|
284 |
*
|
|
|
285 |
* @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
|
|
|
286 |
* to check
|
|
|
287 |
* alignment with.
|
|
|
288 |
*
|
|
|
289 |
* @return boolean
|
|
|
290 |
*/
|
|
|
291 |
public function alignsCommentWith(
|
|
|
292 |
PHP_CodeSniffer_CommentParser_ParameterElement $other
|
|
|
293 |
) {
|
|
|
294 |
// Compares the index before param comment.
|
|
|
295 |
$otherComment
|
|
|
296 |
= (strlen($other->_varName) + strlen($other->_commentWhitespace));
|
|
|
297 |
$thisComment
|
|
|
298 |
= (strlen($this->_varName) + strlen($this->_commentWhitespace));
|
|
|
299 |
|
|
|
300 |
if ($otherComment !== $thisComment) {
|
|
|
301 |
return false;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
return true;
|
|
|
305 |
|
|
|
306 |
}//end alignsCommentWith()
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
/**
|
|
|
310 |
* Returns true if this parameter aligns with the other paramter.
|
|
|
311 |
*
|
|
|
312 |
* @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
|
|
|
313 |
* to check
|
|
|
314 |
* alignment with.
|
|
|
315 |
*
|
|
|
316 |
* @return boolean
|
|
|
317 |
*/
|
|
|
318 |
public function alignsWith(PHP_CodeSniffer_CommentParser_ParameterElement $other)
|
|
|
319 |
{
|
|
|
320 |
if ($this->alignsVariableWith($other) === false) {
|
|
|
321 |
return false;
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
if ($this->alignsCommentWith($other) === false) {
|
|
|
325 |
return false;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
return true;
|
|
|
329 |
|
|
|
330 |
}//end alignsWith()
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
}//end class
|
|
|
334 |
|
|
|
335 |
?>
|