| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tokenizes doc block comments. |
| 4 |
* |
| 5 |
* @author Greg Sherwood <[email protected]> |
| 6 |
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 |
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace PHP_CodeSniffer\Tokenizers; |
| 11 |
|
| 12 |
use PHP_CodeSniffer\Util; |
| 13 |
|
| 14 |
class Comment { |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Creates an array of tokens when given some PHP code. |
| 19 |
* |
| 20 |
* Starts by using token_get_all() but does a lot of extra processing |
| 21 |
* to insert information about the context of the token. |
| 22 |
* |
| 23 |
* @param string $string The string to tokenize. |
| 24 |
* @param string $eolChar The EOL character to use for splitting strings. |
| 25 |
* @param int $stackPtr The position of the first token in the file. |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public function tokenizeString( $string, $eolChar, $stackPtr ) { |
| 30 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 31 |
echo "\t\t*** START COMMENT TOKENIZING ***" . PHP_EOL; |
| 32 |
} |
| 33 |
|
| 34 |
$tokens = []; |
| 35 |
$numChars = strlen( $string ); |
| 36 |
|
| 37 |
/* |
| 38 |
Doc block comments start with /*, but typically contain an |
| 39 |
extra star when they are used for function and class comments. |
| 40 |
*/ |
| 41 |
|
| 42 |
$char = ( $numChars - strlen( ltrim( $string, '/*' ) ) ); |
| 43 |
$openTag = substr( $string, 0, $char ); |
| 44 |
$string = ltrim( $string, '/*' ); |
| 45 |
|
| 46 |
$tokens[ $stackPtr ] = [ |
| 47 |
'content' => $openTag, |
| 48 |
'code' => T_DOC_COMMENT_OPEN_TAG, |
| 49 |
'type' => 'T_DOC_COMMENT_OPEN_TAG', |
| 50 |
'comment_tags' => [], |
| 51 |
]; |
| 52 |
|
| 53 |
$openPtr = $stackPtr; |
| 54 |
$stackPtr ++; |
| 55 |
|
| 56 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 57 |
$content = Util\Common::prepareForOutput( $openTag ); |
| 58 |
echo "\t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content" . PHP_EOL; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
Strip off the close tag so it doesn't interfere with any |
| 63 |
of our comment line processing. The token will be added to the |
| 64 |
stack just before we return it. |
| 65 |
*/ |
| 66 |
|
| 67 |
$closeTag = [ |
| 68 |
'content' => substr( $string, strlen( rtrim( $string, '/*' ) ) ), |
| 69 |
'code' => T_DOC_COMMENT_CLOSE_TAG, |
| 70 |
'type' => 'T_DOC_COMMENT_CLOSE_TAG', |
| 71 |
'comment_opener' => $openPtr, |
| 72 |
]; |
| 73 |
|
| 74 |
if ( $closeTag['content'] === false ) { |
| 75 |
$closeTag['content'] = ''; |
| 76 |
} |
| 77 |
|
| 78 |
$string = rtrim( $string, '/*' ); |
| 79 |
|
| 80 |
/* |
| 81 |
Process each line of the comment. |
| 82 |
*/ |
| 83 |
|
| 84 |
$lines = explode( $eolChar, $string ); |
| 85 |
$numLines = count( $lines ); |
| 86 |
foreach ( $lines as $lineNum => $string ) { |
| 87 |
if ( $lineNum !== ( $numLines - 1 ) ) { |
| 88 |
$string .= $eolChar; |
| 89 |
} |
| 90 |
|
| 91 |
$char = 0; |
| 92 |
$numChars = strlen( $string ); |
| 93 |
|
| 94 |
// We've started a new line, so process the indent. |
| 95 |
$space = $this->collectWhitespace( $string, $char, $numChars ); |
| 96 |
if ( $space !== null ) { |
| 97 |
$tokens[ $stackPtr ] = $space; |
| 98 |
$stackPtr ++; |
| 99 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 100 |
$content = Util\Common::prepareForOutput( $space['content'] ); |
| 101 |
echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content" . PHP_EOL; |
| 102 |
} |
| 103 |
|
| 104 |
$char += strlen( $space['content'] ); |
| 105 |
if ( $char === $numChars ) { |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ( $string === '' ) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
if ( $lineNum > 0 && $string[ $char ] === '*' ) { |
| 115 |
// This is a function or class doc block line. |
| 116 |
$char ++; |
| 117 |
$tokens[ $stackPtr ] = [ |
| 118 |
'content' => '*', |
| 119 |
'code' => T_DOC_COMMENT_STAR, |
| 120 |
'type' => 'T_DOC_COMMENT_STAR', |
| 121 |
]; |
| 122 |
|
| 123 |
$stackPtr ++; |
| 124 |
|
| 125 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 126 |
echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *" . PHP_EOL; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Now we are ready to process the actual content of the line. |
| 131 |
$lineTokens = $this->processLine( $string, $eolChar, $char, $numChars ); |
| 132 |
foreach ( $lineTokens as $lineToken ) { |
| 133 |
$tokens[ $stackPtr ] = $lineToken; |
| 134 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 135 |
$content = Util\Common::prepareForOutput( $lineToken['content'] ); |
| 136 |
$type = $lineToken['type']; |
| 137 |
echo "\t\tCreate comment token: $type => $content" . PHP_EOL; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $lineToken['code'] === T_DOC_COMMENT_TAG ) { |
| 141 |
$tokens[ $openPtr ]['comment_tags'][] = $stackPtr; |
| 142 |
} |
| 143 |
|
| 144 |
$stackPtr ++; |
| 145 |
} |
| 146 |
}//end foreach |
| 147 |
|
| 148 |
$tokens[ $stackPtr ] = $closeTag; |
| 149 |
$tokens[ $openPtr ]['comment_closer'] = $stackPtr; |
| 150 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 151 |
$content = Util\Common::prepareForOutput( $closeTag['content'] ); |
| 152 |
echo "\t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content" . PHP_EOL; |
| 153 |
} |
| 154 |
|
| 155 |
if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
| 156 |
echo "\t\t*** END COMMENT TOKENIZING ***" . PHP_EOL; |
| 157 |
} |
| 158 |
|
| 159 |
return $tokens; |
| 160 |
|
| 161 |
}//end tokenizeString() |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Process a single line of a comment. |
| 166 |
* |
| 167 |
* @param string $string The comment string being tokenized. |
| 168 |
* @param string $eolChar The EOL character to use for splitting strings. |
| 169 |
* @param int $start The position in the string to start processing. |
| 170 |
* @param int $end The position in the string to end processing. |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
private function processLine( $string, $eolChar, $start, $end ) { |
| 175 |
$tokens = []; |
| 176 |
|
| 177 |
// Collect content padding. |
| 178 |
$space = $this->collectWhitespace( $string, $start, $end ); |
| 179 |
if ( $space !== null ) { |
| 180 |
$tokens[] = $space; |
| 181 |
$start += strlen( $space['content'] ); |
| 182 |
} |
| 183 |
|
| 184 |
if ( isset( $string[ $start ] ) === false ) { |
| 185 |
return $tokens; |
| 186 |
} |
| 187 |
|
| 188 |
if ( $string[ $start ] === '@' ) { |
| 189 |
// The content up until the first whitespace is the tag name. |
| 190 |
$matches = []; |
| 191 |
preg_match( '/@[^\s]+/', $string, $matches, 0, $start ); |
| 192 |
if ( isset( $matches[0] ) === true |
| 193 |
&& substr( strtolower( $matches[0] ), 0, 7 ) !== '@phpcs:' |
| 194 |
) { |
| 195 |
$tagName = $matches[0]; |
| 196 |
$start += strlen( $tagName ); |
| 197 |
$tokens[] = [ |
| 198 |
'content' => $tagName, |
| 199 |
'code' => T_DOC_COMMENT_TAG, |
| 200 |
'type' => 'T_DOC_COMMENT_TAG', |
| 201 |
]; |
| 202 |
|
| 203 |
// Then there will be some whitespace. |
| 204 |
$space = $this->collectWhitespace( $string, $start, $end ); |
| 205 |
if ( $space !== null ) { |
| 206 |
$tokens[] = $space; |
| 207 |
$start += strlen( $space['content'] ); |
| 208 |
} |
| 209 |
} |
| 210 |
}//end if |
| 211 |
|
| 212 |
// Process the rest of the line. |
| 213 |
$eol = strpos( $string, $eolChar, $start ); |
| 214 |
if ( $eol === false ) { |
| 215 |
$eol = $end; |
| 216 |
} |
| 217 |
|
| 218 |
if ( $eol > $start ) { |
| 219 |
$tokens[] = [ |
| 220 |
'content' => substr( $string, $start, ( $eol - $start ) ), |
| 221 |
'code' => T_DOC_COMMENT_STRING, |
| 222 |
'type' => 'T_DOC_COMMENT_STRING', |
| 223 |
]; |
| 224 |
} |
| 225 |
|
| 226 |
if ( $eol !== $end ) { |
| 227 |
$tokens[] = [ |
| 228 |
'content' => substr( $string, $eol, strlen( $eolChar ) ), |
| 229 |
'code' => T_DOC_COMMENT_WHITESPACE, |
| 230 |
'type' => 'T_DOC_COMMENT_WHITESPACE', |
| 231 |
]; |
| 232 |
} |
| 233 |
|
| 234 |
return $tokens; |
| 235 |
|
| 236 |
}//end processLine() |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Collect consecutive whitespace into a single token. |
| 241 |
* |
| 242 |
* @param string $string The comment string being tokenized. |
| 243 |
* @param int $start The position in the string to start processing. |
| 244 |
* @param int $end The position in the string to end processing. |
| 245 |
* |
| 246 |
* @return array|null |
| 247 |
*/ |
| 248 |
private function collectWhitespace( $string, $start, $end ) { |
| 249 |
$space = ''; |
| 250 |
for ( $start; $start < $end; $start ++ ) { |
| 251 |
if ( $string[ $start ] !== ' ' && $string[ $start ] !== "\t" ) { |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
$space .= $string[ $start ]; |
| 256 |
} |
| 257 |
|
| 258 |
if ( $space === '' ) { |
| 259 |
return null; |
| 260 |
} |
| 261 |
|
| 262 |
$token = [ |
| 263 |
'content' => $space, |
| 264 |
'code' => T_DOC_COMMENT_WHITESPACE, |
| 265 |
'type' => 'T_DOC_COMMENT_WHITESPACE', |
| 266 |
]; |
| 267 |
|
| 268 |
return $token; |
| 269 |
|
| 270 |
}//end collectWhitespace() |
| 271 |
|
| 272 |
|
| 273 |
}//end class |
| 274 |
|