Subversion Repositories Applications.framework

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 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 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
34 aurelien 13
 * @version   CVS: $Id: CommentElement.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
if (class_exists('PHP_CodeSniffer_CommentParser_SingleElement', true) === false) {
18
    $error = 'Class PHP_CodeSniffer_CommentParser_SingleElement not found';
19
    throw new PHP_CodeSniffer_Exception($error);
20
}
21
 
22
/**
23
 * A class to represent Comments of a doc comment.
24
 *
25
 * Comments are in the following format.
26
 * <code>
27
 * /** <--this is the start of the comment.
28
 *  * This is a short comment description
29
 *  *
30
 *  * This is a long comment description
31
 *  * <-- this is the end of the comment
32
 *  * @return something
33
 *  {@/}
34
 *  </code>
35
 *
36
 * Note that the sentence before two newlines is assumed
37
 * the short comment description.
38
 *
39
 * @category  PHP
40
 * @package   PHP_CodeSniffer
41
 * @author    Greg Sherwood <gsherwood@squiz.net>
42
 * @author    Marc McIntyre <mmcintyre@squiz.net>
43
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
44
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
45
 * @version   Release: 1.2.0RC1
46
 * @link      http://pear.php.net/package/PHP_CodeSniffer
47
 */
48
class PHP_CodeSniffer_CommentParser_CommentElement extends PHP_CodeSniffer_CommentParser_SingleElement
49
{
50
 
51
 
52
    /**
53
     * Constructs a PHP_CodeSniffer_CommentParser_CommentElement.
54
     *
55
     * @param PHP_CodeSniffer_CommentParser_DocElemement $previousElement The element
56
     *                                                                    that
57
     *                                                                    appears
58
     *                                                                    before this
59
     *                                                                    element.
60
     * @param array                                      $tokens          The tokens
61
     *                                                                    that make
62
     *                                                                    up this
63
     *                                                                    element.
64
     * @param PHP_CodeSniffer_File                       $phpcsFile       The file
65
     *                                                                    that this
66
     *                                                                    element is
67
     *                                                                    in.
68
     */
69
    public function __construct(
70
        $previousElement,
71
        $tokens,
72
        PHP_CodeSniffer_File $phpcsFile
73
    ) {
74
        parent::__construct($previousElement, $tokens, 'comment', $phpcsFile);
75
 
76
    }//end __construct()
77
 
78
 
79
    /**
80
     * Returns the short comment description.
81
     *
82
     * @return string
83
     * @see getLongComment()
84
     */
85
    public function getShortComment()
86
    {
87
        $pos = $this->_getShortCommentEndPos();
88
        if ($pos === -1) {
89
            return '';
90
        }
91
 
92
        return implode('', array_slice($this->tokens, 0, ($pos + 1)));
93
 
94
    }//end getShortComment()
95
 
96
 
97
    /**
98
     * Returns the last token position of the short comment description.
99
     *
100
     * @return int The last token position of the short comment description
101
     * @see _getLongCommentStartPos()
102
     */
103
    private function _getShortCommentEndPos()
104
    {
105
        $found      = false;
106
        $whiteSpace = array(
107
                       ' ',
108
                       "\t",
109
                      );
110
 
111
        foreach ($this->tokens as $pos => $token) {
112
            $token = str_replace($whiteSpace, '', $token);
113
            if ($token === $this->phpcsFile->eolChar) {
114
                if ($found === false) {
115
                    // Include newlines before short description.
116
                    continue;
117
                } else {
118
                    if (isset($this->tokens[($pos + 1)]) === true) {
119
                        if ($this->tokens[($pos + 1)] === $this->phpcsFile->eolChar) {
120
                            return ($pos - 1);
121
                        }
122
                    } else {
123
                        return $pos;
124
                    }
125
                }
126
            } else {
127
                $found = true;
128
            }
129
        }//end foreach
130
 
131
        return (count($this->tokens) - 1);
132
 
133
    }//end _getShortCommentEndPos()
134
 
135
 
136
    /**
137
     * Returns the long comment description.
138
     *
139
     * @return string
140
     * @see getShortComment
141
     */
142
    public function getLongComment()
143
    {
144
        $start = $this->_getLongCommentStartPos();
145
        if ($start === -1) {
146
            return '';
147
        }
148
 
149
        return implode('', array_slice($this->tokens, $start));
150
 
151
    }//end getLongComment()
152
 
153
 
154
    /**
155
     * Returns the start position of the long comment description.
156
     *
157
     * Returns -1 if there is no long comment.
158
     *
159
     * @return int The start position of the long comment description.
160
     * @see _getShortCommentEndPos()
161
     */
162
    private function _getLongCommentStartPos()
163
    {
164
        $pos = ($this->_getShortCommentEndPos() + 1);
165
        if ($pos === (count($this->tokens) - 1)) {
166
            return -1;
167
        }
168
 
169
        $count = count($this->tokens);
170
        for ($i = $pos; $i < $count; $i++) {
171
            $content = trim($this->tokens[$i]);
172
            if ($content !== '') {
173
                if ($content{0} === '@') {
174
                    return -1;
175
                }
176
 
177
                return $i;
178
            }
179
        }
180
 
181
        return -1;
182
 
183
    }//end _getLongCommentStartPos()
184
 
185
 
186
    /**
187
     * Returns the whitespace that exists between
188
     * the short and the long comment description.
189
     *
190
     * @return string
191
     */
192
    public function getWhiteSpaceBetween()
193
    {
194
        $endShort  = ($this->_getShortCommentEndPos() + 1);
195
        $startLong = ($this->_getLongCommentStartPos() - 1);
196
        if ($startLong === -1) {
197
            return '';
198
        }
199
 
200
        return implode(
201
            '',
202
            array_slice($this->tokens, $endShort, ($startLong - $endShort))
203
        );
204
 
205
    }//end getWhiteSpaceBetween()
206
 
207
 
208
    /**
209
     * Returns the number of newlines that exist before the tags.
210
     *
211
     * @return int
212
     */
213
    public function getNewlineAfter()
214
    {
215
        $long = $this->getLongComment();
216
        if ($long !== '') {
217
            $long     = rtrim($long, ' ');
218
            $long     = strrev($long);
219
            $newlines = strspn($long, $this->phpcsFile->eolChar);
220
        } else {
221
            $endShort = ($this->_getShortCommentEndPos() + 1);
222
            $after    = implode('', array_slice($this->tokens, $endShort));
223
            $after    = trim($after, ' ');
224
            $newlines = strspn($after, $this->phpcsFile->eolChar);
225
        }
226
 
227
        return ($newlines / strlen($this->phpcsFile->eolChar));
228
 
229
    }//end getNewlineAfter()
230
 
231
 
232
    /**
233
     * Returns true if there is no comment.
234
     *
235
     * @return boolean
236
     */
237
    public function isEmpty()
238
    {
239
        return (trim($this->getContent()) === '');
240
 
241
    }//end isEmpty()
242
 
243
 
244
}//end class
245
 
246
?>