本文整理汇总了PHP中PHP_CodeSniffer_File::gettokens方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::gettokens方法的具体用法?PHP PHP_CodeSniffer_File::gettokens怎么用?PHP PHP_CodeSniffer_File::gettokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer_File
的用法示例。
在下文中一共展示了PHP_CodeSniffer_File::gettokens方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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(PHP_CodeSniffer_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 (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === 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);
}
}
示例2: 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(PHP_CodeSniffer_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 = sprintf('Loop incrementor (%s) jumbling with inner loop', join(', ', $diff));
$phpcsfile->addwarning($error, $stackptr);
}
}
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
if (isset($tokens[$stackptr]['scope_opener']) === false) {
// Ignore the ELSE in ELSE IF. We'll process the IF part later.
if ($tokens[$stackptr]['code'] === T_ELSE && $tokens[$stackptr + 2]['code'] === T_IF) {
return;
}
if ($tokens[$stackptr]['code'] === T_WHILE) {
// This could be from a DO WHILE, which doesn't have an opening brace.
$lastcontent = $phpcsfile->findprevious(T_WHITESPACE, $stackptr - 1, null, true);
if ($tokens[$lastcontent]['code'] === T_CLOSE_CURLY_BRACKET) {
$brace = $tokens[$lastcontent];
if (isset($brace['scope_condition']) === true) {
$condition = $tokens[$brace['scope_condition']];
if ($condition['code'] === T_DO) {
return;
}
}
}
}
// This is a control structure without an opening brace,
// so it is an inline statement.
if ($this->error === true) {
$phpcsfile->adderror('Inline control structures are not allowed', $stackptr);
} else {
$phpcsfile->addwarning('Inline control structures are discouraged', $stackptr);
}
return;
}
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$workingstring = $tokens[$stackptr]['content'];
// Check if it's a double quoted string.
if (strpos($workingstring, '"') === false) {
return;
}
// Make sure it's not a part of a string started above.
// If it is, then we have already checked it.
if ($workingstring[0] !== '"') {
return;
}
// Work through the following tokens, in case this string is stretched
// over multiple Lines.
for ($i = $stackptr + 1; $i < $phpcsfile->numTokens; $i++) {
if ($tokens[$i]['type'] !== 'T_CONSTANT_ENCAPSED_STRING') {
break;
}
$workingstring .= $tokens[$i]['content'];
}
$allowedchars = array('\\n', '\\r', '\\f', '\\t', '\\v', '\\x', '\'', '$');
foreach ($allowedchars as $testchar) {
if (strpos($workingstring, $testchar) !== false) {
return;
}
}
$error = "String {$workingstring} does not require double quotes; use single quotes instead";
$phpcsfile->addwarning($error, $stackptr);
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];
// Skip for-statements without body.
if (isset($token['scope_opener']) === false) {
return;
}
$next = ++$token['scope_opener'];
$end = --$token['scope_closer'];
$emptybody = true;
for (; $next <= $end; ++$next) {
if (in_array($tokens[$next]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === false) {
$emptybody = false;
break;
}
}
if ($emptybody === true) {
// Get token identifier.
$name = $phpcsfile->gettokensAsString($stackptr, 1);
$error = sprintf('Empty %s statement detected', strtoupper($name));
if ($this->_tokens[$token['code']] === true) {
$phpcsfile->adderror($error, $stackptr);
} else {
$phpcsfile->addwarning($error, $stackptr);
}
}
}
示例6: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
if (isset($tokens[$stackptr]['scope_opener']) === false) {
$error = 'possible parse error: ';
$error .= $tokens[$stackptr]['content'];
$error .= ' missing opening or closing brace';
$phpcsfile->addwarning($error, $stackptr);
return;
}
$curlybrace = $tokens[$stackptr]['scope_opener'];
$lastcontent = $phpcsfile->findprevious(T_WHITESPACE, $curlybrace - 1, $stackptr, true);
$classline = $tokens[$lastcontent]['line'];
$braceline = $tokens[$curlybrace]['line'];
if ($braceline != $classline) {
$error = 'Opening brace of a ';
$error .= $tokens[$stackptr]['content'];
$error .= ' must be on the same line as the definition';
$phpcsfile->adderror($error, $curlybrace);
return;
}
if ($tokens[$curlybrace - 1]['code'] === T_WHITESPACE) {
$prevcontent = $tokens[$curlybrace - 1]['content'];
if ($prevcontent !== $phpcsfile->eolChar) {
$blankspace = substr($prevcontent, strpos($prevcontent, $phpcsfile->eolChar));
$spaces = strlen($blankspace);
if ($spaces !== 1) {
$error = "Expected 1 space before opening brace; {$spaces} found";
$phpcsfile->adderror($error, $curlybrace);
}
}
}
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];
// Skip for-loop without body.
if (isset($token['parenthesis_opener']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$goodcondition = false;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === true) {
continue;
} else {
if ($code !== T_TRUE && $code !== T_FALSE) {
$goodcondition = true;
}
}
}
if ($goodcondition === false) {
$error = 'Avoid IF statements that are always true or false';
$phpcsfile->addwarning($error, $stackptr);
}
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
//$nexttoken = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, ($stackptr + 1), null, true);
//if ($tokens[$nexttoken]['code'] === T_OPEN_PARENTHESIS) {
// $error = '"'.$tokens[$stackptr]['content'].'"';
// $error .= ' is a statement, not a function; ';
// $error .= 'no parentheses are required';
// $phpcsfile->adderror($error, $stackptr);
//}
$incondition = count($tokens[$stackptr]['conditions']) !== 0 ? true : false;
// Check to see if this including statement is within the parenthesis of a condition.
// If that's the case then we need to process it as being within a condition, as they
// are checking the return value.
if (isset($tokens[$stackptr]['nested_parenthesis']) === true) {
foreach ($tokens[$stackptr]['nested_parenthesis'] as $left => $right) {
if (isset($tokens[$left]['parenthesis_owner']) === true) {
$incondition = true;
}
}
}
// Check to see if they are assigning the return value of this including call.
// If they are then they are probably checking it, so its conditional.
$previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr - 1, null, true);
if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_tokens::$assignmentTokens) === true) {
// The have assigned the return value to it, so its conditional.
$incondition = true;
}
$tokencode = $tokens[$stackptr]['code'];
if ($incondition === true) {
// We are inside a conditional statement. We need an include_once.
if ($tokencode === T_REQUIRE_ONCE) {
$error = 'File is being conditionally included; ';
$error .= 'use "include_once" instead';
$phpcsfile->adderror($error, $stackptr);
} else {
if ($tokencode === T_REQUIRE) {
$error = 'File is being conditionally included; ';
$error .= 'use "include" instead';
$phpcsfile->adderror($error, $stackptr);
}
}
} else {
// We are unconditionally including, we need a require_once.
if ($tokencode === T_INCLUDE_ONCE) {
$error = 'File is being unconditionally included; ';
$error .= 'use "require_once" instead';
$phpcsfile->adderror($error, $stackptr);
} else {
if ($tokencode === T_INCLUDE) {
$error = 'File is being unconditionally included; ';
$error .= 'use "require" instead';
$phpcsfile->adderror($error, $stackptr);
}
}
}
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$last_token = end($tokens);
if ($last_token['code'] === T_CLOSE_TAG) {
$error = 'Closing PHP tag is not required at the end of the file. Please remove.';
$phpcsfile->addwarning($error, $stackptr);
}
}
示例10: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$keyword = $tokens[$stackptr]['content'];
if (strtolower($keyword) !== $keyword) {
$error = 'TRUE, FALSE and NULL must be lowercase; expected "' . strtolower($keyword) . '" but found "' . $keyword . '"';
$phpcsfile->adderror($error, $stackptr);
}
}
示例11: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
if ($tokens[$stackptr]['content'][0] === '#') {
$error = 'Perl-style comments are not allowed. Use "// Comment."';
$error .= ' or "/* comment */" instead.';
$phpcsfile->adderror($error, $stackptr);
}
}
示例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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
// Find the next non-empty token.
$next = $phpcsfile->findnext(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr + 1, null, true);
if ($tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
// Not a function call.
return;
}
if (isset($tokens[$next]['parenthesis_closer']) === false) {
// Not a function call.
return;
}
// Find the previous non-empty token.
$previous = $phpcsfile->findprevious(PHP_CodeSniffer_tokens::$emptyTokens, $stackptr - 1, null, true);
if ($tokens[$previous]['code'] === T_FUNCTION) {
// It's a function definition, not a function call.
return;
}
if ($tokens[$previous]['code'] === T_NEW) {
// We are creating an object, not calling a function.
return;
}
if ($stackptr + 1 !== $next) {
// Checking this: $value = my_function[*](...).
$error = 'Space before opening parenthesis of function call prohibited';
$phpcsfile->adderror($error, $stackptr);
}
if ($tokens[$next + 1]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function([*]...).
$error = 'Space after opening parenthesis of function call prohibited';
$phpcsfile->adderror($error, $stackptr);
}
$closer = $tokens[$next]['parenthesis_closer'];
if ($tokens[$closer - 1]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function(...[*]).
$between = $phpcsfile->findnext(T_WHITESPACE, $next + 1, null, true);
// Only throw an error if there is some content between the parenthesis.
// IE. Checking for this: $value = my_function().
// If there is no content, then we would have thrown an error in the
// previous IF statement because it would look like this:
// $value = my_function( ).
if ($between !== $closer) {
$error = 'Space before closing parenthesis of function call prohibited';
$phpcsfile->adderror($error, $closer);
}
}
$next = $phpcsfile->findnext(T_WHITESPACE, $closer + 1, null, true);
if ($tokens[$next]['code'] === T_SEMICOLON) {
if (in_array($tokens[$closer + 1]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === true) {
$error = 'Space after closing parenthesis of function call prohibited';
$phpcsfile->adderror($error, $closer);
}
}
}
示例13: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$classname = $phpcsfile->findnext(T_STRING, $stackptr);
$name = trim($tokens[$classname]['content']);
// Make sure that the word is all lowercase
if (!preg_match('/[a-z]?/', $name)) {
$error = ucfirst($tokens[$stackptr]['content']) . ' name is not valid, must be all lower-case';
$phpcsfile->adderror($error, $stackptr);
}
}
示例14: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
$content = $tokens[$stackptr]['content'];
if ($content !== strtolower($content)) {
$type = strtoupper($content);
$expected = strtolower($content);
$error = "{$type} keyword must be lowercase; expected \"{$expected}\" but found \"{$content}\"";
$phpcsfile->adderror($error, $stackptr);
}
}
示例15: 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(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
$tokens = $phpcsfile->gettokens();
if ($tokens[$stackptr + 1]['code'] !== T_WHITESPACE) {
$error = 'A cast statement must be followed by a single space';
$phpcsfile->adderror($error, $stackptr);
return;
}
if ($tokens[$stackptr + 1]['content'] !== ' ') {
$error = 'A cast statement must be followed by a single space';
$phpcsfile->adderror($error, $stackptr);
}
}