本文整理汇总了PHP中PHP_CodeSniffer\Files\File::getTokens方法的典型用法代码示例。如果您正苦于以下问题:PHP File::getTokens方法的具体用法?PHP File::getTokens怎么用?PHP File::getTokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer\Files\File
的用法示例。
在下文中一共展示了File::getTokens方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$found = $phpcsFile->eolChar;
$found = str_replace("\n", '\\n', $found);
$found = str_replace("\r", '\\r', $found);
$phpcsFile->recordMetric($stackPtr, 'EOL char', $found);
if ($found === $this->eolChar) {
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
// Check for single line files without an EOL. This is a very special
// case and the EOL char is set to \n when this happens.
if ($found === '\\n') {
$tokens = $phpcsFile->getTokens();
$lastToken = $phpcsFile->numTokens - 1;
if ($tokens[$lastToken]['line'] === 1 && $tokens[$lastToken]['content'] !== "\n") {
return;
}
}
$error = 'End of line character is invalid; expected "%s" but found "%s"';
$expected = $this->eolChar;
$expected = str_replace("\n", '\\n', $expected);
$expected = str_replace("\r", '\\r', $expected);
$data = array($expected, $found);
// Errors are always reported on line 1, no matter where the first PHP tag is.
$fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data);
if ($fix === true) {
$tokens = $phpcsFile->getTokens();
switch ($this->eolChar) {
case '\\n':
$eolChar = "\n";
break;
case '\\r':
$eolChar = "\r";
break;
case '\\r\\n':
$eolChar = "\r\n";
break;
default:
$eolChar = $this->eolChar;
break;
}
for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
if (isset($tokens[$i + 1]) === false || $tokens[$i + 1]['line'] > $tokens[$i]['line']) {
// Token is the last on a line.
if (isset($tokens[$i]['orig_content']) === true) {
$tokenContent = $tokens[$i]['orig_content'];
} else {
$tokenContent = $tokens[$i]['content'];
}
$newContent = rtrim($tokenContent, "\r\n");
$newContent .= $eolChar;
$phpcsFile->fixer->replaceToken($i, $newContent);
}
}
}
//end if
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
示例2: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
$error = 'Possible parse error: %s missing opening or closing brace';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
return;
}
// Determine the name of the class or interface. Note that we cannot
// simply look for the first T_STRING because a class name
// starting with the number will be multiple tokens.
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true);
$nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
if ($nameEnd === false) {
$name = $tokens[$nameStart]['content'];
} else {
$name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
}
// Check for camel caps format.
$valid = Common::isCamelCaps($name, true, true, false);
if ($valid === false) {
$type = ucfirst($tokens[$stackPtr]['content']);
$error = '%s name "%s" is not in camel caps format';
$data = array($type, $name);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
$phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes');
}
}
示例3: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// We are only interested in the first token in a multi-line string.
if ($tokens[$stackPtr]['code'] === $tokens[$stackPtr - 1]['code']) {
return;
}
$workingString = $tokens[$stackPtr]['content'];
$lastStringToken = $stackPtr;
$i = $stackPtr + 1;
if (isset($tokens[$i]) === true) {
while ($i < $phpcsFile->numTokens && $tokens[$i]['code'] === $tokens[$stackPtr]['code']) {
$workingString .= $tokens[$i]['content'];
$lastStringToken = $i;
$i++;
}
}
// Check if it's a double quoted string.
if (strpos($workingString, '"') === false) {
return;
}
// Make sure it's not a part of a string started in a previous line.
// If it is, then we have already checked it.
if ($workingString[0] !== '"') {
return;
}
// The use of variables in double quoted strings is not allowed.
if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
$stringTokens = token_get_all('<?php ' . $workingString);
foreach ($stringTokens as $token) {
if (is_array($token) === true && $token[0] === T_VARIABLE) {
$error = 'Variable "%s" not allowed in double quoted string; use concatenation instead';
$data = array($token[1]);
$phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data);
}
}
return;
}
//end if
$allowedChars = array('\\0', '\\1', '\\2', '\\3', '\\4', '\\5', '\\6', '\\7', '\\n', '\\r', '\\f', '\\t', '\\v', '\\x', '\\b', '\\e', '\\u', '\'');
foreach ($allowedChars as $testChar) {
if (strpos($workingString, $testChar) !== false) {
return;
}
}
$error = 'String %s does not require double quotes; use single quotes instead';
$data = array(str_replace("\n", '\\n', $workingString));
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$innerContent = substr($workingString, 1, -1);
$innerContent = str_replace('\\"', '"', $innerContent);
$phpcsFile->fixer->replaceToken($stackPtr, "'{$innerContent}'");
while ($lastStringToken !== $stackPtr) {
$phpcsFile->fixer->replaceToken($lastStringToken, '');
$lastStringToken--;
}
$phpcsFile->fixer->endChangeset();
}
}
示例4: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$parts = array(0, 0, 0);
$index = 0;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if ($code === T_SEMICOLON) {
++$index;
} else {
if (isset(Tokens::$emptyTokens[$code]) === false) {
++$parts[$index];
}
}
}
if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
$error = 'This FOR loop can be simplified to a WHILE loop';
$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
}
}
示例5: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Check if the next non whitespace token is a string.
$index = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($tokens[$index]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
// Make sure it is the only thing in the square brackets.
$next = $phpcsFile->findNext(T_WHITESPACE, $index + 1, null, true);
if ($tokens[$next]['code'] !== T_CLOSE_SQUARE_BRACKET) {
return;
}
// Allow indexes that have dots in them because we can't write
// them in dot notation.
$content = trim($tokens[$index]['content'], '"\' ');
if (strpos($content, '.') !== false) {
return;
}
// Also ignore reserved words.
if ($content === 'super') {
return;
}
// Token before the opening square bracket cannot be a var name.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$prev]['code'] === T_STRING) {
$error = 'Object indexes must be written in dot notation';
$phpcsFile->addError($error, $prev, 'Found');
}
}
示例6: processTokenWithinScope
/**
* Processes the function tokens within the class.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
* @param int $currScope The current scope opener token.
*
* @return void
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$tokens = $phpcsFile->getTokens();
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) {
// Ignore nested functions.
return;
}
$modifier = null;
for ($i = $stackPtr - 1; $i > 0; $i--) {
if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
break;
} else {
if (isset(Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) {
$modifier = $i;
break;
}
}
}
if ($modifier === null) {
$error = 'Visibility must be declared on method "%s"';
$data = array($methodName);
$phpcsFile->addError($error, $stackPtr, 'Missing', $data);
}
}
示例7: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Ignore this.something and other uses of "this" that are not
// direct assignments.
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($tokens[$next]['code'] !== T_SEMICOLON) {
if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
return;
}
}
// Something must be assigned to "this".
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$prev]['code'] !== T_EQUAL) {
return;
}
// A variable needs to be assigned to "this".
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $prev - 1, null, true);
if ($tokens[$prev]['code'] !== T_STRING) {
return;
}
// We can only assign "this" to a var called "self".
if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') {
$error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"';
$phpcsFile->addError($error, $prev, 'NotSelf');
}
}
示例8: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prevType = $tokens[$stackPtr - 1]['code'];
if (isset(Tokens::$emptyTokens[$prevType]) === false) {
return;
}
$nonSpace = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 2, null, true);
if ($tokens[$nonSpace]['code'] === T_SEMICOLON) {
// Empty statement.
return;
}
$expected = $tokens[$nonSpace]['content'] . ';';
$found = $phpcsFile->getTokensAsString($nonSpace, $stackPtr - $nonSpace) . ';';
$error = 'Space found before semicolon; expected "%s" but found "%s"';
$data = array($expected, $found);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr - 1; $i > $nonSpace; $i--) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
示例9: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// Detect start and end of this function definition.
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$nestingLevel = 0;
// Find the maximum nesting level of any token in the function.
for ($i = $start + 1; $i < $end; $i++) {
$level = $tokens[$i]['level'];
if ($nestingLevel < $level) {
$nestingLevel = $level;
}
}
// We subtract the nesting level of the function itself.
$nestingLevel = $nestingLevel - $tokens[$stackPtr]['level'] - 1;
if ($nestingLevel > $this->absoluteNestingLevel) {
$error = 'Function\'s nesting level (%s) exceeds allowed maximum of %s';
$data = array($nestingLevel, $this->absoluteNestingLevel);
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
} else {
if ($nestingLevel > $this->nestingLevel) {
$warning = 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function';
$data = array($nestingLevel, $this->nestingLevel);
$phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data);
}
}
}
示例10: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$find = Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;
$commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT) {
$phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing');
$phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no');
return;
}
$phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes');
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
$phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle');
return;
}
if ($tokens[$commentEnd]['line'] !== $tokens[$stackPtr]['line'] - 1) {
$error = 'There must be no blank lines after the class comment';
$phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
}
$commentStart = $tokens[$commentEnd]['comment_opener'];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
$error = '%s tag is not allowed in class comment';
$data = array($tokens[$tag]['content']);
$phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
}
}
示例11: process
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$fileName = $phpcsFile->getFilename();
$matches = array();
if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) {
// Not an actions file.
return;
}
$ownClass = $matches[2];
$tokens = $phpcsFile->getTokens();
$typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr + 2, null, false, true);
$typeName = trim($tokens[$typeName]['content'], " '");
switch (strtolower($tokens[$stackPtr + 1]['content'])) {
case 'includesystem':
$included = strtolower($typeName);
break;
case 'includeasset':
$included = strtolower($typeName) . 'assettype';
break;
case 'includewidget':
$included = strtolower($typeName) . 'widgettype';
break;
default:
return;
}
if ($included === strtolower($ownClass)) {
$error = "You do not need to include \"%s\" from within the system's own actions file";
$data = array($ownClass);
$phpcsFile->addError($error, $stackPtr, 'NotRequired', $data);
}
}
示例12: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
// Skip for-loop without body.
if (isset($token['scope_opener']) === false) {
return;
}
// Find incrementors for outer loop.
$outer = $this->findIncrementers($tokens, $token);
// Skip if empty.
if (count($outer) === 0) {
return;
}
// Find nested for loops.
$start = ++$token['scope_opener'];
$end = --$token['scope_closer'];
for (; $start <= $end; ++$start) {
if ($tokens[$start]['code'] !== T_FOR) {
continue;
}
$inner = $this->findIncrementers($tokens, $tokens[$start]);
$diff = array_intersect($outer, $inner);
if (count($diff) !== 0) {
$error = 'Loop incrementor (%s) jumbling with inner loop';
$data = array(join(', ', $diff));
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
}
}
}
示例13: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$lastLine = $tokens[$stackPtr]['line'];
$end = $tokens[$stackPtr]['bracket_closer'];
$endLine = $tokens[$end]['line'];
// Do not check nested style definitions as, for example, in @media style rules.
$nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $stackPtr + 1, $end);
if ($nested !== false) {
return;
}
$foundColon = false;
$foundString = false;
for ($i = $stackPtr + 1; $i <= $end; $i++) {
if ($tokens[$i]['line'] !== $lastLine) {
// We changed lines.
if ($foundColon === false && $foundString !== false) {
// We didn't find a colon on the previous line.
$error = 'No style definition found on line; check for missing colon';
$phpcsFile->addError($error, $foundString, 'Found');
}
$foundColon = false;
$foundString = false;
$lastLine = $tokens[$i]['line'];
}
if ($tokens[$i]['code'] === T_STRING) {
$foundString = $i;
} else {
if ($tokens[$i]['code'] === T_COLON) {
$foundColon = $i;
}
}
}
//end for
}
示例14: processTokenWithinScope
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$tokens = $phpcsFile->getTokens();
$function = $tokens[$stackPtr + 2];
if ($function['code'] !== T_STRING) {
return;
}
$functionName = $function['content'];
$classOpener = $tokens[$currScope]['scope_condition'];
$className = $tokens[$classOpener + 2]['content'];
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if ($methodProps['is_static'] === true) {
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// There is no scope opener or closer, so the function
// must be abstract.
return;
}
$thisUsage = $stackPtr;
while (($thisUsage = $phpcsFile->findNext(array(T_VARIABLE), $thisUsage + 1, $tokens[$stackPtr]['scope_closer'], false, '$this')) !== false) {
if ($thisUsage === false) {
return;
}
$error = 'Usage of "$this" in static methods will cause runtime errors';
$phpcsFile->addError($error, $thisUsage, 'Found');
}
}
//end if
}
示例15: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$colour = $tokens[$stackPtr]['content'];
$expected = strtoupper($colour);
if ($colour !== $expected) {
$error = 'CSS colours must be defined in uppercase; expected %s but found %s';
$data = array($expected, $colour);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotUpper', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
}
}
// Now check if shorthand can be used.
if (strlen($colour) !== 7) {
return;
}
if ($colour[1] === $colour[2] && $colour[3] === $colour[4] && $colour[5] === $colour[6]) {
$expected = '#' . $colour[1] . $colour[3] . $colour[5];
$error = 'CSS colours must use shorthand if available; expected %s but found %s';
$data = array($expected, $colour);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Shorthand', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
}
}
}