本文整理汇总了PHP中PHP_CodeSniffer::prepareForOutput方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer::prepareForOutput方法的具体用法?PHP PHP_CodeSniffer::prepareForOutput怎么用?PHP PHP_CodeSniffer::prepareForOutput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer
的用法示例。
在下文中一共展示了PHP_CodeSniffer::prepareForOutput方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: tokenizeString
/**
* Creates an array of tokens when given some CSS code.
*
* Uses the PHP tokenizer to do all the tricky work
*
* @param string $string The string to tokenize.
* @param string $eolChar The EOL character to use for splitting strings.
*
* @return array
*/
public function tokenizeString($string, $eolChar = '\\n')
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START CSS TOKENIZING ***" . PHP_EOL;
}
// If the content doesn't have an EOL char on the end, add one so
// the open and close tags we add are parsed correctly.
$eolAdded = false;
if (substr($string, strlen($eolChar) * -1) !== $eolChar) {
$string .= $eolChar;
$eolAdded = true;
}
$string = str_replace('<?php', '^PHPCS_CSS_T_OPEN_TAG^', $string);
$string = str_replace('?>', '^PHPCS_CSS_T_CLOSE_TAG^', $string);
$tokens = parent::tokenizeString('<?php ' . $string . '?>', $eolChar);
$finalTokens = array();
$finalTokens[0] = array('code' => T_OPEN_TAG, 'type' => 'T_OPEN_TAG', 'content' => '');
$newStackPtr = 1;
$numTokens = count($tokens);
$multiLineComment = false;
for ($stackPtr = 1; $stackPtr < $numTokens; $stackPtr++) {
$token = $tokens[$stackPtr];
// CSS files don't have lists, breaks etc, so convert these to
// standard strings early so they can be converted into T_STYLE
// tokens and joined with other strings if needed.
if ($token['code'] === T_BREAK || $token['code'] === T_LIST || $token['code'] === T_DEFAULT) {
$token['type'] = 'T_STRING';
$token['code'] = T_STRING;
}
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $token['type'];
$content = PHP_CodeSniffer::prepareForOutput($token['content']);
echo "\tProcess token {$stackPtr}: {$type} => {$content}" . PHP_EOL;
}
if ($token['code'] === T_POWER && $tokens[$stackPtr + 1]['content'] === 'PHPCS_CSS_T_OPEN_TAG') {
$content = '<?php';
for ($stackPtr = $stackPtr + 3; $stackPtr < $numTokens; $stackPtr++) {
if ($tokens[$stackPtr]['code'] === T_POWER && $tokens[$stackPtr + 1]['content'] === 'PHPCS_CSS_T_CLOSE_TAG') {
// Add the end tag and ignore the * we put at the end.
$content .= '?>';
$stackPtr += 2;
break;
} else {
$content .= $tokens[$stackPtr]['content'];
}
}
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t=> Found embedded PHP code: ";
$cleanContent = PHP_CodeSniffer::prepareForOutput($content);
echo $cleanContent . PHP_EOL;
}
$finalTokens[$newStackPtr] = array('type' => 'T_EMBEDDED_PHP', 'code' => T_EMBEDDED_PHP, 'content' => $content);
$newStackPtr++;
continue;
}
//end if
if ($token['code'] === T_GOTO_LABEL) {
// Convert these back to T_STRING followed by T_COLON so we can
// more easily process style definitions.
$finalTokens[$newStackPtr] = array('type' => 'T_STRING', 'code' => T_STRING, 'content' => substr($token['content'], 0, -1));
$newStackPtr++;
$finalTokens[$newStackPtr] = array('type' => 'T_COLON', 'code' => T_COLON, 'content' => ':');
$newStackPtr++;
continue;
}
if ($token['code'] === T_FUNCTION) {
// There are no functions in CSS, so convert this to a string.
$finalTokens[$newStackPtr] = array('type' => 'T_STRING', 'code' => T_STRING, 'content' => $token['content']);
$newStackPtr++;
continue;
}
if ($token['code'] === T_COMMENT && substr($token['content'], 0, 2) === '/*') {
// Multi-line comment. Record it so we can ignore other
// comment tags until we get out of this one.
$multiLineComment = true;
}
if ($token['code'] === T_COMMENT && $multiLineComment === false && (substr($token['content'], 0, 2) === '//' || $token['content'][0] === '#')) {
$content = ltrim($token['content'], '#/');
// Guard against PHP7+ syntax errors by stripping
// leading zeros so the content doesn't look like an invalid int.
$leadingZero = false;
if ($content[0] === '0') {
$content = '1' . $content;
$leadingZero = false;
}
$commentTokens = parent::tokenizeString('<?php ' . $content . '?>', $eolChar);
// The first and last tokens are the open/close tags.
array_shift($commentTokens);
array_pop($commentTokens);
if ($leadingZero === true) {
//.........这里部分代码省略.........
示例2: tokenizeString
/**
* 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.
*
* @return array
*/
public function tokenizeString($string, $eolChar = '\\n')
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START PHP TOKENIZING ***" . PHP_EOL;
$isWin = false;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$isWin = true;
}
}
$tokens = @token_get_all($string);
$finalTokens = array();
$newStackPtr = 0;
$numTokens = count($tokens);
$lastNotEmptyToken = 0;
$insideInlineIf = array();
$commentTokenizer = new PHP_CodeSniffer_Tokenizers_Comment();
for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
$token = (array) $tokens[$stackPtr];
$tokenIsArray = isset($token[1]);
if (PHP_CODESNIFFER_VERBOSITY > 1) {
if ($tokenIsArray === true) {
$type = token_name($token[0]);
$content = PHP_CodeSniffer::prepareForOutput($token[1]);
} else {
$newToken = self::resolveSimpleToken($token[0]);
$type = $newToken['type'];
$content = PHP_CodeSniffer::prepareForOutput($token[0]);
}
echo "\tProcess token ";
if ($tokenIsArray === true) {
echo "[{$stackPtr}]";
} else {
echo " {$stackPtr} ";
}
echo ": {$type} => {$content}";
}
//end if
if ($newStackPtr > 0 && $finalTokens[$newStackPtr - 1]['code'] !== T_WHITESPACE) {
$lastNotEmptyToken = $newStackPtr - 1;
}
/*
If we are using \r\n newline characters, the \r and \n are sometimes
split over two tokens. This normally occurs after comments. We need
to merge these two characters together so that our line endings are
consistent for all lines.
*/
if ($tokenIsArray === true && substr($token[1], -1) === "\r") {
if (isset($tokens[$stackPtr + 1]) === true && is_array($tokens[$stackPtr + 1]) === true && $tokens[$stackPtr + 1][1][0] === "\n") {
$token[1] .= "\n";
if (PHP_CODESNIFFER_VERBOSITY > 1) {
if ($isWin === true) {
echo '\\n';
} else {
echo "[30;1m\\n[0m";
}
}
if ($tokens[$stackPtr + 1][1] === "\n") {
// This token's content has been merged into the previous,
// so we can skip it.
$tokens[$stackPtr + 1] = '';
} else {
$tokens[$stackPtr + 1][1] = substr($tokens[$stackPtr + 1][1], 1);
}
}
}
//end if
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
}
/*
Parse doc blocks into something that can be easily iterated over.
*/
if ($tokenIsArray === true && $token[0] === T_DOC_COMMENT) {
$commentTokens = $commentTokenizer->tokenizeString($token[1], $eolChar, $newStackPtr);
foreach ($commentTokens as $commentToken) {
$finalTokens[$newStackPtr] = $commentToken;
$newStackPtr++;
}
continue;
}
/*
If this is a double quoted string, PHP will tokenize the whole
thing which causes problems with the scope map when braces are
within the string. So we need to merge the tokens together to
provide a single string.
*/
if ($tokenIsArray === false && ($token[0] === '"' || $token[0] === 'b"')) {
// Binary casts need a special token.
if ($token[0] === 'b"') {
//.........这里部分代码省略.........
示例3: revertToken
/**
* Reverts the previous fix made to a token.
*
* @param int $stackPtr The position of the token in the token stack.
*
* @return bool If a change was reverted.
*/
public function revertToken($stackPtr)
{
if (isset($this->_fixedTokens[$stackPtr]) === false) {
return false;
}
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$bt = debug_backtrace();
if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') {
$sniff = $bt[2]['class'];
$line = $bt[1]['line'];
} else {
$sniff = $bt[1]['class'];
$line = $bt[0]['line'];
}
$tokens = $this->_currentFile->getTokens();
$type = $tokens[$stackPtr]['type'];
$oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]);
$newContent = PHP_CodeSniffer::prepareForOutput($this->_fixedTokens[$stackPtr]);
if (trim($this->_tokens[$stackPtr]) === '' && isset($tokens[$stackPtr + 1]) === true) {
// Add some context for whitespace only changes.
$append = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr + 1]);
$oldContent .= $append;
$newContent .= $append;
}
}
//end if
$this->_tokens[$stackPtr] = $this->_fixedTokens[$stackPtr];
unset($this->_fixedTokens[$stackPtr]);
$this->_numFixes--;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$indent = "\t";
if (empty($this->_changeset) === false) {
$indent .= "\tR: ";
}
@ob_end_clean();
echo "{$indent}{$sniff} (line {$line}) reverted token {$stackPtr} ({$type}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
ob_start();
}
return true;
}
示例4: _createLevelMap
/**
* Constructs the level map.
*
* The level map adds a 'level' index to each token which indicates the
* depth that a token within a set of scope blocks. It also adds a
* 'condition' index which is an array of the scope conditions that opened
* each of the scopes - position 0 being the first scope opener.
*
* @param array $tokens The array of tokens to process.
* @param object $tokenizer The tokenizer being used to process this file.
* @param string $eolChar The EOL character to use for splitting strings.
*
* @return void
*/
private static function _createLevelMap(&$tokens, $tokenizer, $eolChar)
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START LEVEL MAP ***" . PHP_EOL;
}
$numTokens = count($tokens);
$level = 0;
$conditions = array();
$lastOpener = null;
$openers = array();
for ($i = 0; $i < $numTokens; $i++) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$i]['type'];
$line = $tokens[$i]['line'];
$len = $tokens[$i]['length'];
$col = $tokens[$i]['column'];
$content = PHP_CodeSniffer::prepareForOutput($tokens[$i]['content']);
echo str_repeat("\t", $level + 1);
echo "Process token {$i} on line {$line} [col:{$col};len:{$len};lvl:{$level};";
if (empty($conditions) !== true) {
$condString = 'conds;';
foreach ($conditions as $condition) {
$condString .= token_name($condition) . ',';
}
echo rtrim($condString, ',') . ';';
}
echo "]: {$type} => {$content}" . PHP_EOL;
}
//end if
$tokens[$i]['level'] = $level;
$tokens[$i]['conditions'] = $conditions;
if (isset($tokens[$i]['scope_condition']) === true) {
// Check to see if this token opened the scope.
if ($tokens[$i]['scope_opener'] === $i) {
$stackPtr = $tokens[$i]['scope_condition'];
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $level + 1);
echo "=> Found scope opener for {$stackPtr}:{$type}" . PHP_EOL;
}
$stackPtr = $tokens[$i]['scope_condition'];
// If we find a scope opener that has a shared closer,
// then we need to go back over the condition map that we
// just created and fix ourselves as we just added some
// conditions where there was none. This happens for T_CASE
// statements that are using the same break statement.
if ($lastOpener !== null && $tokens[$lastOpener]['scope_closer'] === $tokens[$i]['scope_closer']) {
// This opener shares its closer with the previous opener,
// but we still need to check if the two openers share their
// closer with each other directly (like CASE and DEFAULT)
// or if they are just sharing because one doesn't have a
// closer (like CASE with no BREAK using a SWITCHes closer).
$thisType = $tokens[$tokens[$i]['scope_condition']]['code'];
$opener = $tokens[$lastOpener]['scope_condition'];
$isShared = isset($tokenizer->scopeOpeners[$thisType]['with'][$tokens[$opener]['code']]);
reset($tokenizer->scopeOpeners[$thisType]['end']);
reset($tokenizer->scopeOpeners[$tokens[$opener]['code']]['end']);
$sameEnd = current($tokenizer->scopeOpeners[$thisType]['end']) === current($tokenizer->scopeOpeners[$tokens[$opener]['code']]['end']);
if ($isShared === true && $sameEnd === true) {
$badToken = $opener;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$badToken]['type'];
echo str_repeat("\t", $level + 1);
echo "* shared closer, cleaning up {$badToken}:{$type} *" . PHP_EOL;
}
for ($x = $tokens[$i]['scope_condition']; $x <= $i; $x++) {
$oldConditions = $tokens[$x]['conditions'];
$oldLevel = $tokens[$x]['level'];
$tokens[$x]['level']--;
unset($tokens[$x]['conditions'][$badToken]);
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$x]['type'];
$oldConds = '';
foreach ($oldConditions as $condition) {
$oldConds .= token_name($condition) . ',';
}
$oldConds = rtrim($oldConds, ',');
$newConds = '';
foreach ($tokens[$x]['conditions'] as $condition) {
$newConds .= token_name($condition) . ',';
}
$newConds = rtrim($newConds, ',');
$newLevel = $tokens[$x]['level'];
echo str_repeat("\t", $level + 1);
echo "* cleaned {$x}:{$type} *" . PHP_EOL;
echo str_repeat("\t", $level + 2);
//.........这里部分代码省略.........
示例5: processAdditional
/**
* Performs additional processing after main tokenizing.
*
* This additional processing looks for properties, closures, labels and objects.
*
* @param array $tokens The array of tokens to process.
* @param string $eolChar The EOL character to use for splitting strings.
*
* @return void
*/
public function processAdditional(&$tokens, $eolChar)
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START ADDITIONAL JS PROCESSING ***" . PHP_EOL;
}
$numTokens = count($tokens);
$classStack = array();
for ($i = 0; $i < $numTokens; $i++) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$i]['type'];
$content = PHP_CodeSniffer::prepareForOutput($tokens[$i]['content']);
echo str_repeat("\t", count($classStack));
echo "\tProcess token {$i}: {$type} => {$content}" . PHP_EOL;
}
// Looking for functions that are actually closures.
if ($tokens[$i]['code'] === T_FUNCTION && isset($tokens[$i]['scope_opener']) === true) {
for ($x = $i + 1; $x < $numTokens; $x++) {
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
break;
}
}
if ($tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
$tokens[$i]['code'] = T_CLOSURE;
$tokens[$i]['type'] = 'T_CLOSURE';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$line = $tokens[$i]['line'];
echo str_repeat("\t", count($classStack));
echo "\t* token {$i} on line {$line} changed from T_FUNCTION to T_CLOSURE" . PHP_EOL;
}
for ($x = $tokens[$i]['scope_opener'] + 1; $x < $tokens[$i]['scope_closer']; $x++) {
if (isset($tokens[$x]['conditions'][$i]) === false) {
continue;
}
$tokens[$x]['conditions'][$i] = T_CLOSURE;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$x]['type'];
echo str_repeat("\t", count($classStack));
echo "\t\t* cleaned {$x} ({$type}) *" . PHP_EOL;
}
}
}
//end if
continue;
} else {
if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET && isset($tokens[$i]['scope_condition']) === false) {
$classStack[] = $i;
$closer = $tokens[$i]['bracket_closer'];
$tokens[$i]['code'] = T_OBJECT;
$tokens[$i]['type'] = 'T_OBJECT';
$tokens[$closer]['code'] = T_CLOSE_OBJECT;
$tokens[$closer]['type'] = 'T_CLOSE_OBJECT';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", count($classStack));
echo "\t* token {$i} converted from T_OPEN_CURLY_BRACKET to T_OBJECT *" . PHP_EOL;
echo str_repeat("\t", count($classStack));
echo "\t* token {$closer} converted from T_CLOSE_CURLY_BRACKET to T_CLOSE_OBJECT *" . PHP_EOL;
}
for ($x = $i + 1; $x < $closer; $x++) {
$tokens[$x]['conditions'][$i] = T_OBJECT;
ksort($tokens[$x]['conditions'], SORT_NUMERIC);
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$x]['type'];
echo str_repeat("\t", count($classStack));
echo "\t\t* added T_OBJECT condition to {$x} ({$type}) *" . PHP_EOL;
}
}
} else {
if ($tokens[$i]['code'] === T_CLOSE_OBJECT) {
$opener = array_pop($classStack);
} else {
if ($tokens[$i]['code'] === T_COLON) {
// If it is a scope opener, it belongs to a
// DEFAULT or CASE statement.
if (isset($tokens[$i]['scope_condition']) === true) {
continue;
}
// Make sure this is not part of an inline IF statement.
for ($x = $i - 1; $x >= 0; $x--) {
if ($tokens[$x]['code'] === T_INLINE_THEN) {
$tokens[$i]['code'] = T_INLINE_ELSE;
$tokens[$i]['type'] = 'T_INLINE_ELSE';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", count($classStack));
echo "\t* token {$i} converted from T_COLON to T_INLINE_THEN *" . PHP_EOL;
}
continue 2;
} else {
if ($tokens[$x]['line'] < $tokens[$i]['line']) {
break;
}
//.........这里部分代码省略.........
示例6: replaceToken
/**
* Replace the entire contents of a token.
*
* @param int $stackPtr The position of the token in the token stack.
* @param string $content The new content of the token.
*
* @return bool If the change was accepted.
*/
public function replaceToken($stackPtr, $content)
{
if ($this->_inChangeset === false && isset($this->_fixedTokens[$stackPtr]) === true) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
@ob_end_clean();
echo "\t* token {$stackPtr} has already been modified, skipping *" . PHP_EOL;
ob_start();
}
return false;
}
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$bt = debug_backtrace();
if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') {
$sniff = $bt[2]['class'];
$line = $bt[1]['line'];
} else {
$sniff = $bt[1]['class'];
$line = $bt[0]['line'];
}
$tokens = $this->_currentFile->getTokens();
$type = $tokens[$stackPtr]['type'];
$oldContent = PHP_CodeSniffer::prepareForOutput($tokens[$stackPtr]['content']);
$newContent = PHP_CodeSniffer::prepareForOutput($content);
if (trim($tokens[$stackPtr]['content']) === '' && isset($tokens[$stackPtr + 1]) === true) {
// Add some context for whitespace only changes.
$append = PHP_CodeSniffer::prepareForOutput($tokens[$stackPtr + 1]['content']);
$oldContent .= $append;
$newContent .= $append;
}
}
//end if
if ($this->_inChangeset === true) {
$this->_changeset[$stackPtr] = $content;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
@ob_end_clean();
echo "\t\tQ: {$sniff} (line {$line}) replaced token {$stackPtr} ({$type}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
ob_start();
}
return true;
}
if (isset($this->_oldTokenValues[$stackPtr]) === false) {
$this->_oldTokenValues[$stackPtr] = array(1 => $content, 2 => $this->_tokens[$stackPtr]);
} else {
if ($this->_oldTokenValues[$stackPtr][2] === $content) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$indent = "\t";
if (empty($this->_changeset) === false) {
$indent .= "\t";
}
@ob_end_clean();
echo "{$indent}**** {$sniff} (line {$line}) has possible conflict with another sniff; ignoring change ****" . PHP_EOL;
ob_start();
}
return false;
}
$this->_oldTokenValues[$stackPtr][2] = $this->_oldTokenValues[$stackPtr][1];
$this->_oldTokenValues[$stackPtr][1] = $content;
}
//end if
$this->_tokens[$stackPtr] = $content;
$this->_numFixes++;
$this->_fixedTokens[$stackPtr] = true;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$indent = "\t";
if (empty($this->_changeset) === false) {
$indent .= "\tA: ";
}
@ob_end_clean();
echo "{$indent}{$sniff} (line {$line}) replaced token {$stackPtr} ({$type}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
ob_start();
}
return true;
}
示例7: tokenizeString
/**
* 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.
*
* @return array
*/
public function tokenizeString($string, $eolChar, $stackPtr)
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t*** START COMMENT TOKENIZING ***" . PHP_EOL;
}
$tokens = array();
$numChars = strlen($string);
/*
Doc block comments start with /*, but typically contain an
extra star when they are used for function and class comments.
*/
for ($c = 0; $c < $numChars; $c++) {
if ($string[$c] !== '/' && $string[$c] !== '*') {
break;
}
}
$openTag = substr($string, 0, $c);
$tokens[$stackPtr] = array('content' => $openTag, 'code' => T_DOC_COMMENT_OPEN_TAG, 'type' => 'T_DOC_COMMENT_OPEN_TAG', 'comment_tags' => array());
$openPtr = $stackPtr;
$stackPtr++;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$content = PHP_CodeSniffer::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.
*/
for ($i = $numChars - 1; $i > $c; $i--) {
if ($string[$i] !== '/' && $string[$i] !== '*') {
break;
}
}
$i++;
$closeTag = array('content' => substr($string, $i), 'code' => T_DOC_COMMENT_CLOSE_TAG, 'type' => 'T_DOC_COMMENT_CLOSE_TAG', 'comment_opener' => $openPtr);
$string = substr($string, 0, $i);
$numChars = strlen($string);
/*
Process each line of the comment.
*/
while ($c < $numChars) {
$lineTokens = $this->_processLine($string, $eolChar, $c, $numChars);
foreach ($lineTokens as $lineToken) {
$tokens[$stackPtr] = $lineToken;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$content = PHP_CodeSniffer::prepareForOutput($lineToken['content']);
$type = $lineToken['type'];
echo "\t\tCreate comment token: {$type} => {$content}" . PHP_EOL;
}
if ($lineToken['code'] === T_DOC_COMMENT_TAG) {
$tokens[$openPtr]['comment_tags'][] = $stackPtr;
}
$c += strlen($lineToken['content']);
$stackPtr++;
}
if ($c === $numChars) {
break;
}
// We've started a new line, so process the indent.
$space = $this->_collectWhitespace($string, $c, $numChars);
if ($space !== null) {
$tokens[$stackPtr] = $space;
$stackPtr++;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$content = PHP_CodeSniffer::prepareForOutput($space['content']);
$type = $lineToken['type'];
echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => {$content}" . PHP_EOL;
}
$c += strlen($space['content']);
if ($c === $numChars) {
break;
}
}
if ($string[$c] === '*') {
// This is a function or class doc block line.
$c++;
$tokens[$stackPtr] = array('content' => '*', 'code' => T_DOC_COMMENT_STAR, 'type' => 'T_DOC_COMMENT_STAR');
$stackPtr++;
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.
// So round we go.
}
//end while
$tokens[$stackPtr] = $closeTag;
//.........这里部分代码省略.........
示例8: processAdditional
/**
* Performs additional processing after main tokenizing.
*
* This additional processing looks for properties, labels and objects.
*
* @param array $tokens The array of tokens to process.
* @param string $eolChar The EOL character to use for splitting strings.
*
* @return void
*/
public function processAdditional(&$tokens, $eolChar)
{
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t*** START ADDITIONAL JS PROCESSING ***" . PHP_EOL;
}
$numTokens = count($tokens);
$classStack = array();
for ($i = 0; $i < $numTokens; $i++) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$i]['type'];
$content = PHP_CodeSniffer::prepareForOutput($tokens[$i]['content']);
echo str_repeat("\t", count($classStack));
echo "\tProcess token {$i}: {$type} => {$content}" . PHP_EOL;
}
if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET && isset($tokens[$i]['scope_condition']) === false) {
$classStack[] = $i;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", count($classStack));
echo "\t=> Found property opener" . PHP_EOL;
}
// This could also be an object definition.
for ($x = $i - 1; $x >= 0; $x--) {
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
// Non-whitespace content.
break;
}
}
if ($tokens[$x]['code'] === T_EQUAL) {
for ($x--; $x >= 0; $x--) {
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
break;
}
}
if ($tokens[$x]['code'] === T_STRING || $tokens[$x]['code'] === T_PROTOTYPE) {
// Find the first string in this definition.
// E.g., WantedString.DontWantThis.prototype.
for ($x--; $x >= 0; $x--) {
if ($tokens[$x]['code'] !== T_STRING && $tokens[$x]['code'] !== T_PROTOTYPE && $tokens[$x]['code'] !== T_OBJECT_OPERATOR) {
$x++;
break;
}
}
$closer = $tokens[$i]['bracket_closer'];
$tokens[$i]['scope_condition'] = $x;
$tokens[$i]['scope_closer'] = $closer;
$tokens[$closer]['scope_condition'] = $x;
$tokens[$closer]['scope_opener'] = $i;
$tokens[$x]['scope_opener'] = $i;
$tokens[$x]['scope_closer'] = $closer;
$tokens[$x]['code'] = T_OBJECT;
$tokens[$x]['type'] = 'T_OBJECT';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", count($classStack));
echo "\t* token {$x} converted from T_STRING to T_OBJECT *" . PHP_EOL;
echo str_repeat("\t", count($classStack));
echo "\t* set scope opener ({$i}) and closer ({$closer}) for token {$x} *" . PHP_EOL;
}
}
//end if
}
//end if
} else {
if ($tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET && (isset($tokens[$i]['scope_condition']) === false || $tokens[$tokens[$i]['scope_condition']]['code'] === T_OBJECT)) {
$opener = array_pop($classStack);
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", count($classStack));
echo "\t\t=> Found property closer for {$opener}" . PHP_EOL;
}
} else {
if ($tokens[$i]['code'] === T_COLON) {
// If it is a scope opener, it belongs to a
// DEFAULT or CASE statement.
if (isset($tokens[$i]['scope_condition']) === true) {
continue;
}
// Make sure this is not part of an inline IF statement.
for ($x = $i - 1; $x >= 0; $x--) {
if ($tokens[$x]['code'] === T_INLINE_THEN) {
$tokens[$i]['code'] = T_INLINE_ELSE;
$tokens[$i]['type'] = 'T_INLINE_ELSE';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", count($classStack));
echo "\t* token {$i} converted from T_COLON to T_INLINE_THEN *" . PHP_EOL;
}
continue 2;
} else {
if ($tokens[$x]['line'] < $tokens[$i]['line']) {
break;
}
}
//.........这里部分代码省略.........