本文整理汇总了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();
// Make sure this is the first PHP open tag so we don't process
// the same file twice.
$prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1);
if ($prevOpenTag !== false) {
return;
}
$fileName = $phpcsFile->getFileName();
$extension = substr($fileName, strrpos($fileName, '.'));
$nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), $stackPtr);
if ($extension === '.php') {
if ($nextClass !== false) {
$error = '%s found in ".php" file; use ".inc" extension instead';
$data = array(ucfirst($tokens[$nextClass]['content']));
$phpcsFile->addError($error, $stackPtr, 'ClassFound', $data);
}
} else {
if ($extension === '.inc') {
if ($nextClass === false) {
$error = 'No interface or class found in ".inc" file; use ".php" extension instead';
$phpcsFile->addError($error, $stackPtr, 'NoClass');
}
}
}
}
示例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)
{
// Only run this sniff once per info file.
$end = count($phpcsFile->getTokens()) + 1;
$fileExtension = strtolower(substr($phpcsFile->getFilename(), -4));
if ($fileExtension !== 'info') {
return $end;
}
$tokens = $phpcsFile->getTokens();
$contents = file_get_contents($phpcsFile->getFilename());
$info = Drupal_Sniffs_InfoFiles_ClassFilesSniff::drupalParseInfoFormat($contents);
if (isset($info['name']) === false) {
$error = '"name" property is missing in the info file';
$phpcsFile->addError($error, $stackPtr, 'Name');
}
if (isset($info['description']) === false) {
$error = '"description" property is missing in the info file';
$phpcsFile->addError($error, $stackPtr, 'Description');
}
if (isset($info['core']) === false) {
$error = '"core" property is missing in the info file';
$phpcsFile->addError($error, $stackPtr, 'Core');
} else {
if ($info['core'] === '7.x' && isset($info['php']) === true && $info['php'] <= '5.2') {
$error = 'Drupal 7 core already requires PHP 5.2';
$ptr = Drupal_Sniffs_InfoFiles_ClassFilesSniff::getPtr('php', $info['php'], $phpcsFile);
$phpcsFile->addError($error, $ptr, 'D7PHPVersion');
}
}
return $end;
}
示例3: findStackPointer
/**
* Search token stream for the corresponding node
*
* @param PHPParser_Node $node Current node
*
* @return integer
*/
private function findStackPointer($node)
{
$tokens = $this->phpcsFile->getTokens();
foreach ($tokens as $stackPtr => $token) {
if ($node->getLine() > $token['line']) {
continue;
}
return $stackPtr;
}
}
示例4: process
/**
* @param \PHP_CodeSniffer_File $phpcs_file
* @param int $stack_ptr
*/
public function process(\PHP_CodeSniffer_File $phpcs_file, $stack_ptr)
{
// Search till interface name.
$index = 0;
while (isset($phpcs_file->getTokens()[$stack_ptr + $index]) && $phpcs_file->getTokens()[$stack_ptr + $index]['type'] !== 'T_STRING') {
$index++;
}
$ptr = $stack_ptr + $index;
$f_name = $phpcs_file->getTokens()[$ptr]['content'];
if (preg_match('/Interface$/', $f_name)) {
return;
}
$phpcs_file->addError('Invalid interface name, interface should be postfixed with Interface.', $ptr);
return;
}
示例5: 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();
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);
$name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
// Check for camel caps format.
$valid = PHP_CodeSniffer::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);
}
}
示例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();
$ignore = array(T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_FUNCTION, T_CONST, T_USE, T_NS_SEPARATOR);
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
// Not a call to a PHP function.
return;
}
$function = strtolower($tokens[$stackPtr]['content']);
$pattern = null;
if ($this->patternMatch === true) {
$count = 0;
$pattern = preg_replace($this->forbiddenFunctionNames, $this->forbiddenFunctionNames, $function, 1, $count);
if ($count === 0) {
return;
}
// Remove the pattern delimiters and modifier.
$pattern = substr($pattern, 1, -2);
} else {
if (in_array($function, $this->forbiddenFunctionNames) === false) {
return;
}
}
$this->addError($phpcsFile, $stackPtr, $function, $pattern);
}
示例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();
// 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 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space before opening brace of class definition; 0 found';
$phpcsFile->addError($error, $stackPtr);
} else {
$content = $tokens[$stackPtr - 1]['content'];
if ($content !== ' ') {
$length = strlen($content);
if ($length === 1) {
$length = 'tab';
}
$error = "Expected 1 space before opening brace of class definition; {$length} found";
$phpcsFile->addError($error, $stackPtr);
}
}
//end if
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true);
if ($next !== false && $tokens[$next]['line'] !== $tokens[$stackPtr]['line'] + 1) {
$num = $tokens[$next]['line'] - $tokens[$stackPtr]['line'] - 1;
$error = "Expected 0 blank lines after opening brace of class definition; {$num} found";
$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();
if ($tokens[$stackPtr]['content'] === 'ob_end_flush') {
$phpcsFile->addError('Use of ob_end_flush() is not allowed; use ob_get_contents() and ob_end_clean() instead', $stackPtr, 'Found');
}
}
示例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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious(T_SEMICOLON, $stackPtr - 1);
if ($prev === false) {
return;
}
// Ignore multiple statements in a FOR condition.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) {
if (isset($tokens[$bracket]['parenthesis_owner']) === false) {
// Probably a closure sitting inside a function call.
continue;
}
$owner = $tokens[$bracket]['parenthesis_owner'];
if ($tokens[$owner]['code'] === T_FOR) {
return;
}
}
}
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Each PHP statement must be on a line by itself';
$phpcsFile->addError($error, $stackPtr, 'SameLine');
return;
}
}
示例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();
$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);
}
}
}
示例12: 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)
{
$path = $phpcsFile->getFileName();
if (!preg_match("/(views)/i", $path)) {
return;
}
$tokens = $phpcsFile->getTokens();
$classname = $tokens[$phpcsFile->findNext(T_STRING, $stackPtr)]['content'];
if (preg_match("/(helpers)/i", $path)) {
$final_classname = $this->classname_without_type($classname, "Helper");
$msg_on_error = "Cake convention expects the helper class name to end with 'Helper'";
} else {
$final_classname = $this->classname_with_type($classname, "View");
$msg_on_error = "Cake convention expects the view class name to end with 'View'";
}
if (is_null($final_classname)) {
$phpcsFile->addError($msg_on_error, $stackPtr);
return;
}
$expected_file_name = preg_replace('/([A-Z])/', '_${1}', $final_classname);
if (strpos($expected_file_name, "_") === 0) {
$expected_file_name = substr($expected_file_name, 1, strlen($expected_file_name));
}
$expected_file_name = strtolower($expected_file_name) . ".php";
if (!preg_match("/" . $expected_file_name . "/", $path)) {
$error = "File name is expected to be, '" . $expected_file_name . "' for Class with name, '" . $classname . "'";
$phpcsFile->addError($error, $stackPtr);
}
}
示例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();
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$properties = array();
$wantedTokens = array(T_PROPERTY, T_OPEN_CURLY_BRACKET);
$next = $phpcsFile->findNext($wantedTokens, $start + 1, $end);
while ($next !== false && $next < $end) {
// Skip nested objects.
if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) {
$next = $tokens[$next]['bracket_closer'];
} else {
$propName = $tokens[$next]['content'];
if (isset($properties[$propName]) === true) {
$line = $tokens[$properties[$propName]]['line'];
$error = "Duplicate property definition found for \"{$propName}\"; previously defined on line {$line}";
$phpcsFile->addError($error, $next);
}
$properties[$propName] = $next;
}
//end if
$next = $phpcsFile->findNext($wantedTokens, $next + 1, $end);
}
//end while
}
示例14: processFunctionCall
/**
* Processes this function call.
*
* @param PHP_CodeSniffer_File $phpcsFile
* The file being scanned.
* @param int $stackPtr
* The position of the function call in the stack.
* @param int $openBracket
* The position of the opening parenthesis in the stack.
* @param int $closeBracket
* The position of the closing parenthesis in the stack.
* @param Drupal_Sniffs_Semantics_FunctionCallSniff $sniff
* Can be used to retreive the function's arguments with the getArgument()
* method.
*
* @return void
*/
public function processFunctionCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $closeBracket, Drupal_Sniffs_Semantics_FunctionCallSniff $sniff)
{
$tokens = $phpcsFile->getTokens();
// We assume that the sequence '#default_value' => variable_get(...)
// indicates a variable that the module owns.
$arrow = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true);
if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) {
return;
}
$arrayKey = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $arrow - 1, null, true);
if ($arrayKey === false || $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING || substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value') {
return;
}
$argument = $sniff->getArgument(1);
// Variable name is not a literal string, so we return early.
if ($argument === false || $tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
$moduleName = DrupalPractice_Project::getName($phpcsFile);
if ($moduleName === false) {
return;
}
$variableName = substr($tokens[$argument['start']]['content'], 1, -1);
if (strpos($variableName, $moduleName) !== 0) {
$warning = 'All variables defined by your module must be prefixed with your module\'s name to avoid name collisions with others. Expected start with "%s" but found "%s"';
$data = array($moduleName, $variableName);
$phpcsFile->addWarning($warning, $argument['start'], 'VariableName', $data);
}
}
示例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();
// 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 ({$nestingLevel}) exceeds allowed maximum of " . $this->absoluteNestingLevel;
$phpcsFile->addError($error, $stackPtr);
} else {
if ($nestingLevel > $this->nestingLevel) {
$warning = "Function's nesting level ({$nestingLevel}) exceeds " . $this->nestingLevel . '; consider refactoring the function';
$phpcsFile->addWarning($warning, $stackPtr);
}
}
}