~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/CommentParser/ParameterElement.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20140721144241-t0v9knmgtzftbsw6
Tags: upstream-1.5.3
Import upstream version 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
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-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
14
 
 */
15
 
 
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);
19
 
}
20
 
 
21
 
/**
22
 
 * A class to represent param tags within a function comment.
23
 
 *
24
 
 * @category  PHP
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
32
 
 */
33
 
class PHP_CodeSniffer_CommentParser_ParameterElement extends PHP_CodeSniffer_CommentParser_AbstractDocElement
34
 
{
35
 
 
36
 
    /**
37
 
     * The variable name of this parameter name, including the $ sign.
38
 
     *
39
 
     * @var string
40
 
     */
41
 
    private $_varName = '';
42
 
 
43
 
    /**
44
 
     * The comment of this parameter tag.
45
 
     *
46
 
     * @var string
47
 
     */
48
 
    private $_comment = '';
49
 
 
50
 
    /**
51
 
     * The variable type of this parameter tag.
52
 
     *
53
 
     * @var string
54
 
     */
55
 
    private $_type = '';
56
 
 
57
 
    /**
58
 
     * The whitespace that exists before the variable name.
59
 
     *
60
 
     * @var string
61
 
     */
62
 
    private $_varNameWhitespace = '';
63
 
 
64
 
    /**
65
 
     * The whitespace that exists before the comment.
66
 
     *
67
 
     * @var string
68
 
     */
69
 
    private $_commentWhitespace = null;
70
 
 
71
 
    /**
72
 
     * The whitespace that exists before the variable type.
73
 
     *
74
 
     * @var string
75
 
     */
76
 
    private $_typeWhitespace = '';
77
 
 
78
 
 
79
 
    /**
80
 
     * Constructs a PHP_CodeSniffer_CommentParser_ParameterElement.
81
 
     *
82
 
     * @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
83
 
     *                                                                  previous to
84
 
     *                                                                  this one.
85
 
     * @param array                                    $tokens          The tokens
86
 
     *                                                                  that make up
87
 
     *                                                                  this element.
88
 
     * @param PHP_CodeSniffer_File                     $phpcsFile       The file that
89
 
     *                                                                  this element
90
 
     *                                                                  is in.
91
 
     */
92
 
    public function __construct(
93
 
        $previousElement,
94
 
        $tokens,
95
 
        PHP_CodeSniffer_File $phpcsFile
96
 
    ) {
97
 
        parent::__construct($previousElement, $tokens, 'param', $phpcsFile);
98
 
 
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();
103
 
            $matches    = array();
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]);
111
 
                }
112
 
            }
113
 
        }
114
 
 
115
 
    }//end __construct()
116
 
 
117
 
 
118
 
    /**
119
 
     * Returns the element names that this tag is comprised of, in the order
120
 
     * that they appear in the tag.
121
 
     *
122
 
     * @return array(string)
123
 
     * @see processSubElement()
124
 
     */
125
 
    protected function getSubElements()
126
 
    {
127
 
        return array(
128
 
                'type',
129
 
                'varName',
130
 
                'comment',
131
 
               );
132
 
 
133
 
    }//end getSubElements()
134
 
 
135
 
 
136
 
    /**
137
 
     * Processes the sub element with the specified name.
138
 
     *
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
142
 
     *                                 sub element.
143
 
     *
144
 
     * @return void
145
 
     * @see getSubElements()
146
 
     */
147
 
    protected function processSubElement($name, $content, $beforeWhitespace)
148
 
    {
149
 
        $element           = '_'.$name;
150
 
        $whitespace        = $element.'Whitespace';
151
 
        $this->$element    = $content;
152
 
        $this->$whitespace = $beforeWhitespace;
153
 
 
154
 
    }//end processSubElement()
155
 
 
156
 
 
157
 
    /**
158
 
     * Returns the variable name that this parameter tag represents.
159
 
     *
160
 
     * @return string
161
 
     */
162
 
    public function getVarName()
163
 
    {
164
 
        return $this->_varName;
165
 
 
166
 
    }//end getVarName()
167
 
 
168
 
 
169
 
    /**
170
 
     * Returns the variable type that this string represents.
171
 
     *
172
 
     * @return string
173
 
     */
174
 
    public function getType()
175
 
    {
176
 
        return $this->_type;
177
 
 
178
 
    }//end getType()
179
 
 
180
 
 
181
 
    /**
182
 
     * Returns the comment of this comment for this parameter.
183
 
     *
184
 
     * @return string
185
 
     */
186
 
    public function getComment()
187
 
    {
188
 
        return $this->_comment;
189
 
 
190
 
    }//end getComment()
191
 
 
192
 
 
193
 
    /**
194
 
     * Returns the whitespace before the variable type.
195
 
     *
196
 
     * @return string
197
 
     * @see getWhiteSpaceBeforeVarName()
198
 
     * @see getWhiteSpaceBeforeComment()
199
 
     */
200
 
    public function getWhiteSpaceBeforeType()
201
 
    {
202
 
        return $this->_typeWhitespace;
203
 
 
204
 
    }//end getWhiteSpaceBeforeType()
205
 
 
206
 
 
207
 
    /**
208
 
     * Returns the whitespace before the variable name.
209
 
     *
210
 
     * @return string
211
 
     * @see getWhiteSpaceBeforeComment()
212
 
     * @see getWhiteSpaceBeforeType()
213
 
     */
214
 
    public function getWhiteSpaceBeforeVarName()
215
 
    {
216
 
        return $this->_varNameWhitespace;
217
 
 
218
 
    }//end getWhiteSpaceBeforeVarName()
219
 
 
220
 
 
221
 
    /**
222
 
     * Returns the whitespace before the comment.
223
 
     *
224
 
     * @return string
225
 
     * @see getWhiteSpaceBeforeVarName()
226
 
     * @see getWhiteSpaceBeforeType()
227
 
     */
228
 
    public function getWhiteSpaceBeforeComment()
229
 
    {
230
 
        return $this->_commentWhitespace;
231
 
 
232
 
    }//end getWhiteSpaceBeforeComment()
233
 
 
234
 
 
235
 
    /**
236
 
     * Returns the position of this parameter are it appears in the comment.
237
 
     *
238
 
     * This method differs from getOrder as it is only relative to method
239
 
     * parameters.
240
 
     *
241
 
     * @return int
242
 
     */
243
 
    public function getPosition()
244
 
    {
245
 
        if (($this->getPreviousElement() instanceof PHP_CodeSniffer_CommentParser_ParameterElement) === false) {
246
 
            return 1;
247
 
        } else {
248
 
            return ($this->getPreviousElement()->getPosition() + 1);
249
 
        }
250
 
 
251
 
    }//end getPosition()
252
 
 
253
 
 
254
 
    /**
255
 
     * Returns true if this parameter's variable aligns with the other's.
256
 
     *
257
 
     * @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
258
 
     *                                                              to check
259
 
     *                                                              alignment with.
260
 
     *
261
 
     * @return boolean
262
 
     */
263
 
    public function alignsVariableWith(
264
 
        PHP_CodeSniffer_CommentParser_ParameterElement $other
265
 
    ) {
266
 
        // Format is:
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) {
273
 
            return false;
274
 
        }
275
 
 
276
 
        return true;
277
 
 
278
 
    }//end alignsVariableWith()
279
 
 
280
 
 
281
 
    /**
282
 
     * Returns true if this parameter's comment aligns with the other's.
283
 
     *
284
 
     * @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
285
 
     *                                                              to check
286
 
     *                                                              alignment with.
287
 
     *
288
 
     * @return boolean
289
 
     */
290
 
    public function alignsCommentWith(
291
 
        PHP_CodeSniffer_CommentParser_ParameterElement $other
292
 
    ) {
293
 
        // Compares the index before param comment.
294
 
        if (strlen($other->_commentWhitespace) === 0 && strlen($this->_commentWhitespace) === 0) {
295
 
            return true;
296
 
        }
297
 
 
298
 
        $otherComment
299
 
            = (strlen($other->_varName) + strlen($other->_commentWhitespace));
300
 
        $thisComment
301
 
            = (strlen($this->_varName) + strlen($this->_commentWhitespace));
302
 
 
303
 
        if ($otherComment !== $thisComment) {
304
 
            return false;
305
 
        }
306
 
 
307
 
        return true;
308
 
 
309
 
    }//end alignsCommentWith()
310
 
 
311
 
 
312
 
    /**
313
 
     * Returns true if this parameter aligns with the other paramter.
314
 
     *
315
 
     * @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
316
 
     *                                                              to check
317
 
     *                                                              alignment with.
318
 
     *
319
 
     * @return boolean
320
 
     */
321
 
    public function alignsWith(PHP_CodeSniffer_CommentParser_ParameterElement $other)
322
 
    {
323
 
        if ($this->alignsVariableWith($other) === false) {
324
 
            return false;
325
 
        }
326
 
 
327
 
        if ($this->alignsCommentWith($other) === false) {
328
 
            return false;
329
 
        }
330
 
 
331
 
        return true;
332
 
 
333
 
    }//end alignsWith()
334
 
 
335
 
 
336
 
}//end class
337
 
 
338
 
?>