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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.3/CodeSniffer/CommentParser/CommentElement.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 Comments of a doc 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-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
 
14
 */
 
15
 
 
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);
 
19
}
 
20
 
 
21
/**
 
22
 * A class to represent Comments of a doc comment.
 
23
 *
 
24
 * Comments are in the following format.
 
25
 * <code>
 
26
 * /** <--this is the start of the comment.
 
27
 *  * This is a short comment description
 
28
 *  *
 
29
 *  * This is a long comment description
 
30
 *  * <-- this is the end of the comment
 
31
 *  * @return something
 
32
 *  {@/}
 
33
 *  </code>
 
34
 *
 
35
 * Note that the sentence before two newlines is assumed
 
36
 * the short comment description.
 
37
 *
 
38
 * @category  PHP
 
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
 
46
 */
 
47
class PHP_CodeSniffer_CommentParser_CommentElement extends PHP_CodeSniffer_CommentParser_SingleElement
 
48
{
 
49
 
 
50
 
 
51
    /**
 
52
     * Constructs a PHP_CodeSniffer_CommentParser_CommentElement.
 
53
     *
 
54
     * @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
 
55
     *                                                                  that
 
56
     *                                                                  appears
 
57
     *                                                                  before this
 
58
     *                                                                  element.
 
59
     * @param array                                    $tokens          The tokens
 
60
     *                                                                  that make
 
61
     *                                                                  up this
 
62
     *                                                                  element.
 
63
     * @param PHP_CodeSniffer_File                     $phpcsFile       The file
 
64
     *                                                                  that this
 
65
     *                                                                  element is
 
66
     *                                                                  in.
 
67
     */
 
68
    public function __construct(
 
69
        $previousElement,
 
70
        $tokens,
 
71
        PHP_CodeSniffer_File $phpcsFile
 
72
    ) {
 
73
        parent::__construct($previousElement, $tokens, 'comment', $phpcsFile);
 
74
 
 
75
    }//end __construct()
 
76
 
 
77
 
 
78
    /**
 
79
     * Returns the short comment description.
 
80
     *
 
81
     * @return string
 
82
     * @see getLongComment()
 
83
     */
 
84
    public function getShortComment()
 
85
    {
 
86
        $pos = $this->_getShortCommentEndPos();
 
87
        if ($pos === -1) {
 
88
            return '';
 
89
        }
 
90
 
 
91
        return implode('', array_slice($this->tokens, 0, ($pos + 1)));
 
92
 
 
93
    }//end getShortComment()
 
94
 
 
95
 
 
96
    /**
 
97
     * Returns the last token position of the short comment description.
 
98
     *
 
99
     * @return int The last token position of the short comment description
 
100
     * @see _getLongCommentStartPos()
 
101
     */
 
102
    private function _getShortCommentEndPos()
 
103
    {
 
104
        $found      = false;
 
105
        $whiteSpace = array(
 
106
                       ' ',
 
107
                       "\t",
 
108
                      );
 
109
 
 
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.
 
115
                    continue;
 
116
                } else {
 
117
                    if (isset($this->tokens[($pos + 1)]) === true) {
 
118
                        if ($this->tokens[($pos + 1)] === $this->phpcsFile->eolChar) {
 
119
                            return ($pos - 1);
 
120
                        }
 
121
                    } else {
 
122
                        return $pos;
 
123
                    }
 
124
                }
 
125
            } else {
 
126
                $found = true;
 
127
            }
 
128
        }//end foreach
 
129
 
 
130
        return (count($this->tokens) - 1);
 
131
 
 
132
    }//end _getShortCommentEndPos()
 
133
 
 
134
 
 
135
    /**
 
136
     * Returns the long comment description.
 
137
     *
 
138
     * @return string
 
139
     * @see getShortComment
 
140
     */
 
141
    public function getLongComment()
 
142
    {
 
143
        $start = $this->_getLongCommentStartPos();
 
144
        if ($start === -1) {
 
145
            return '';
 
146
        }
 
147
 
 
148
        return implode('', array_slice($this->tokens, $start));
 
149
 
 
150
    }//end getLongComment()
 
151
 
 
152
 
 
153
    /**
 
154
     * Returns the start position of the long comment description.
 
155
     *
 
156
     * Returns -1 if there is no long comment.
 
157
     *
 
158
     * @return int The start position of the long comment description.
 
159
     * @see _getShortCommentEndPos()
 
160
     */
 
161
    private function _getLongCommentStartPos()
 
162
    {
 
163
        $pos = ($this->_getShortCommentEndPos() + 1);
 
164
        if ($pos === (count($this->tokens) - 1)) {
 
165
            return -1;
 
166
        }
 
167
 
 
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} === '@') {
 
173
                    return -1;
 
174
                }
 
175
 
 
176
                return $i;
 
177
            }
 
178
        }
 
179
 
 
180
        return -1;
 
181
 
 
182
    }//end _getLongCommentStartPos()
 
183
 
 
184
 
 
185
    /**
 
186
     * Returns the whitespace that exists between
 
187
     * the short and the long comment description.
 
188
     *
 
189
     * @return string
 
190
     */
 
191
    public function getWhiteSpaceBetween()
 
192
    {
 
193
        $endShort  = ($this->_getShortCommentEndPos() + 1);
 
194
        $startLong = ($this->_getLongCommentStartPos() - 1);
 
195
        if ($startLong === -1) {
 
196
            return '';
 
197
        }
 
198
 
 
199
        return implode(
 
200
            '',
 
201
            array_slice($this->tokens, $endShort, ($startLong - $endShort))
 
202
        );
 
203
 
 
204
    }//end getWhiteSpaceBetween()
 
205
 
 
206
 
 
207
    /**
 
208
     * Returns the number of newlines that exist before the tags.
 
209
     *
 
210
     * @return int
 
211
     */
 
212
    public function getNewlineAfter()
 
213
    {
 
214
        $long = $this->getLongComment();
 
215
        if ($long !== '') {
 
216
            $long     = rtrim($long, ' ');
 
217
            $long     = strrev($long);
 
218
            $newlines = strspn($long, $this->phpcsFile->eolChar);
 
219
        } else {
 
220
            $endShort = ($this->_getShortCommentEndPos() + 1);
 
221
            $after    = implode('', array_slice($this->tokens, $endShort));
 
222
            $after    = trim($after, ' ');
 
223
            $newlines = strspn($after, $this->phpcsFile->eolChar);
 
224
        }
 
225
 
 
226
        return ($newlines / strlen($this->phpcsFile->eolChar));
 
227
 
 
228
    }//end getNewlineAfter()
 
229
 
 
230
 
 
231
    /**
 
232
     * Returns true if there is no comment.
 
233
     *
 
234
     * @return boolean
 
235
     */
 
236
    public function isEmpty()
 
237
    {
 
238
        return (trim($this->getContent()) === '');
 
239
 
 
240
    }//end isEmpty()
 
241
 
 
242
 
 
243
}//end class
 
244
 
 
245
?>