Source for file Comment.php
Documentation is available at Comment.php
* Tokenizes doc block comments.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Tokenizers;
use PHP_CodeSniffer\Util;
* Creates an array of tokens when given some PHP code.
* Starts by using token_get_all() but does a lot of extra processing
* to insert information about the context of the token.
* @param string $string The string to tokenize.
* @param string $eolChar The EOL character to use for splitting strings.
* @param int $stackPtr The position of the first token in the file.
public function tokenizeString ($string, $eolChar, $stackPtr)
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
echo "\t\t*** START COMMENT TOKENIZING ***".PHP_EOL;
Doc block comments start with /*, but typically contain an
extra star when they are used for function and class comments.
$openTag = substr($string, 0 , $char);
$string = ltrim($string, '/*');
$tokens[$stackPtr] = array (
'type' => 'T_DOC_COMMENT_OPEN_TAG',
'comment_tags' => array (),
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
$content = Util\Common ::prepareForOutput ($openTag);
echo " \t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content".PHP_EOL;
Strip off the close tag so it doesn't interfere with any
of our comment line processing. The token will be added to the
stack just before we return it.
'type' => 'T_DOC_COMMENT_CLOSE_TAG',
'comment_opener' => $openPtr,
if ($closeTag['content'] === false ) {
$closeTag['content'] = '';
$string = rtrim($string, '/*');
Process each line of the comment.
$lines = explode($eolChar, $string);
$numLines = count($lines);
foreach ($lines as $lineNum => $string) {
if ($lineNum !== ($numLines - 1 )) {
// We've started a new line, so process the indent.
$space = $this->collectWhitespace ($string, $char, $numChars);
$tokens[$stackPtr] = $space;
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
$content = Util\Common ::prepareForOutput ($space['content']);
echo " \t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content".PHP_EOL;
$char += strlen($space['content']);
if ($char === $numChars) {
if ($string[$char] === '*') {
// This is a function or class doc block line.
$tokens[$stackPtr] = array (
'type' => 'T_DOC_COMMENT_STAR',
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *".PHP_EOL;
// Now we are ready to process the actual content of the line.
$lineTokens = $this->processLine ($string, $eolChar, $char, $numChars);
foreach ($lineTokens as $lineToken) {
$tokens[$stackPtr] = $lineToken;
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
$content = Util\Common ::prepareForOutput ($lineToken['content']);
$type = $lineToken['type'];
echo " \t\tCreate comment token: $type => $content".PHP_EOL;
$tokens[$openPtr]['comment_tags'][] = $stackPtr;
$tokens[$stackPtr] = $closeTag;
$tokens[$openPtr]['comment_closer'] = $stackPtr;
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
$content = Util\Common ::prepareForOutput ($closeTag['content']);
echo " \t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content".PHP_EOL;
if (PHP_CODESNIFFER_VERBOSITY > 1 ) {
echo "\t\t*** END COMMENT TOKENIZING ***".PHP_EOL;
* Process a single line of a comment.
* @param string $string The comment string being tokenized.
* @param string $eolChar The EOL character to use for splitting strings.
* @param int $start The position in the string to start processing.
* @param int $end The position in the string to end processing.
private function processLine ($string, $eolChar, $start, $end)
// Collect content padding.
$space = $this->collectWhitespace ($string, $start, $end);
$start += strlen($space['content']);
if (isset ($string[$start]) === false ) {
if ($string[$start] === '@') {
// The content up until the first whitespace is the tag name.
preg_match('/@[^\s]+/', $string, $matches, 0 , $start);
if (isset ($matches[0 ]) === true ) {
'type' => 'T_DOC_COMMENT_TAG',
// Then there will be some whitespace.
$space = $this->collectWhitespace ($string, $start, $end);
$start += strlen($space['content']);
// Process the rest of the line.
$eol = strpos($string, $eolChar, $start);
'content' => substr($string, $start, ($eol - $start)),
'type' => 'T_DOC_COMMENT_STRING',
'type' => 'T_DOC_COMMENT_WHITESPACE',
* Collect consecutive whitespace into a single token.
* @param string $string The comment string being tokenized.
* @param int $start The position in the string to start processing.
* @param int $end The position in the string to end processing.
private function collectWhitespace ($string, $start, $end)
for ($start; $start < $end; $start++ ) {
if ($string[$start] !== ' ' && $string[$start] !== "\t") {
$space .= $string[$start];
'type' => 'T_DOC_COMMENT_WHITESPACE',
}//end collectWhitespace()
Documentation generated on Mon, 11 Mar 2019 15:27:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|