本文整理汇总了PHP中PHP_CodeSniffer_File::getTokensAsString方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::getTokensAsString方法的具体用法?PHP PHP_CodeSniffer_File::getTokensAsString怎么用?PHP PHP_CodeSniffer_File::getTokensAsString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer_File
的用法示例。
在下文中一共展示了PHP_CodeSniffer_File::getTokensAsString方法的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)
{
if ($phpcsFile->getTokensAsString($stackPtr, 4) === '$form_state[\'input\']' || $phpcsFile->getTokensAsString($stackPtr, 4) === '$form_state["input"]') {
$warning = 'Do not use the raw $form_state[\'input\'], use $form_state[\'values\'] instead where possible';
$phpcsFile->addWarning($warning, $stackPtr, 'Input');
}
}
示例2: process
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
* @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();
// No bracket can have space before them.
$prevType = $tokens[$stackPtr - 1]['code'];
if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 2, null, true);
$expected = $tokens[$nonSpace]['content'] . $tokens[$stackPtr]['content'];
$found = $phpcsFile->getTokensAsString($nonSpace, $stackPtr - $nonSpace) . $tokens[$stackPtr]['content'];
$error = 'Space found before square bracket; expected "%s" but found "%s"';
$data = array($expected, $found);
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeBracket', $data);
}
if ($tokens[$stackPtr]['type'] === 'T_OPEN_SQUARE_BRACKET') {
// Open brackets can't have spaces on after them either.
$nextType = $tokens[$stackPtr + 1]['code'];
if (in_array($nextType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 2, null, true);
$expected = $tokens[$stackPtr]['content'] . $tokens[$nonSpace]['content'];
$found = $phpcsFile->getTokensAsString($stackPtr, $nonSpace - $stackPtr + 1);
$error = 'Space found after square bracket; expected "%s" but found "%s"';
$data = array($expected, $found);
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterBracket', $data);
}
}
}
示例3: 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();
// 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 lower case class names
$legalChars = '[a-z]';
if (preg_match("|[^{$legalChars}]|", $name) === 0) {
$valid = true;
} else {
$valid = false;
}
// Check for camel caps format.
// $valid = PHP_CodeSniffer::isLowerCase($name, true, true, true);
if ($valid === false) {
$type = ucfirst($tokens[$stackPtr]['content']);
$error = "{$type} name \"{$name}\" is not in lower case format";
$phpcsFile->addError($error, $stackPtr);
}
}
示例4: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
// get tokens
$tokens = $phpcsFile->getTokens();
$current = $tokens[$stackPtr];
// skip for-statements without body.
if (isset($current['scope_opener']) === false) {
return;
}
// get next
$next = ++$current['scope_opener'];
$end = --$current['scope_closer'];
$emptyBody = true;
for (; $next <= $end; ++$next) {
if (in_array($tokens[$next]['code'], array(T_WHITESPACE)) === false) {
$emptyBody = false;
break;
}
}
if ($emptyBody === true) {
// Get token identifier.
$name = $phpcsFile->getTokensAsString($stackPtr, 1);
$error = sprintf('Empty %s statement detected', strtoupper($name));
$phpcsFile->addWarning($error, $stackPtr);
}
// cleanup
unset($tokens);
unset($current);
unset($next);
unset($end);
}
示例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();
$prevType = $tokens[$stackPtr - 1]['code'];
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$prevType]) === false) {
return;
}
$nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_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();
}
}
示例6: 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);
}
}
示例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();
$namespace = '';
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), 0);
while ($stackPtr !== false) {
// Keep track of what namespace we are in.
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
$nsEnd = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE), $stackPtr + 1, null, true);
$namespace = trim($phpcsFile->getTokensAsString($stackPtr + 1, $nsEnd - $stackPtr - 1));
$stackPtr = $nsEnd;
} else {
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = $tokens[$nameToken]['content'];
if ($namespace !== '') {
$name = $namespace . '\\' . $name;
}
$compareName = strtolower($name);
if (isset($this->foundClasses[$compareName]) === true) {
$type = strtolower($tokens[$stackPtr]['content']);
$file = $this->foundClasses[$compareName]['file'];
$line = $this->foundClasses[$compareName]['line'];
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
$data = array($type, $name, $file, $line);
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
} else {
$this->foundClasses[$compareName] = array('file' => $phpcsFile->getFilename(), 'line' => $tokens[$stackPtr]['line']);
}
}
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), $stackPtr + 1);
}
//end while
}
示例8: isInherited
/**
* Check if an php doc contains @inheritdoc.
*
* @param PHP_CodeSniffer_File $phpcsFile
* @param int $stackPtr
* @param int $commentStart
*
* @return bool
*/
protected function isInherited(PHP_CodeSniffer_File $phpcsFile, $commentStart)
{
$tokens = $phpcsFile->getTokens();
$comment = strtolower($phpcsFile->getTokensAsString($commentStart, $tokens[$commentStart]['comment_closer']));
// Accept inheriting of comments to be sufficient.
return strpos($comment, '@inheritdoc') !== false;
}
示例9: getAlias
private static function getAlias(CodeSnifferFile $file, $stackPtr)
{
$nextPtr = $stackPtr;
$alias = static::$aliasMap;
$tokens = $file->getTokens();
$foundParenthesis = false;
while (is_array($alias)) {
$nextPtr = $file->findNext([T_WHITESPACE, T_COMMA, T_SEMICOLON], ++$nextPtr, null, true, null, true);
if ($nextPtr === false) {
break;
}
if ($tokens[$nextPtr]['code'] === T_OPEN_PARENTHESIS) {
if ($foundParenthesis) {
$nextPtr = $tokens[$nextPtr]['parenthesis_closer'] + 1;
}
$foundParenthesis = true;
continue;
}
if (isset($alias[$tokens[$nextPtr]['code']])) {
$alias = $alias[$tokens[$nextPtr]['code']];
} elseif (isset($alias[$tokens[$nextPtr]['content']])) {
$alias = $alias[$tokens[$nextPtr]['content']];
} else {
return [null, null];
}
}
return [$alias, sprintf('%s%s', $file->getTokensAsString($stackPtr + 1, $nextPtr - $stackPtr), static::getClosing($file, $nextPtr + 1))];
}
示例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();
if ($tokens[$stackPtr]['code'] === T_CASE && isset($tokens[$stackPtr]['scope_closer'])) {
// $internalCase = $phpcsFile->findNext(T_CASE, $stackPtr + 1, $tokens[$stackPtr]['scope_closer']);
// if ($internalCase !== false) {
// $comment = $phpcsFile->findNext(T_COMMENT, $stackPtr + 1, $internalCase - 1);
// if ($comment === false) {
// $phpcsFile->addError($this->getReqPrefix('REQ.PHP.2.5.12') . '"case" has not break and has not any comment', $stackPtr);
// }
// }
$switch = $phpcsFile->findPrevious(T_SWITCH, $stackPtr - 1);
if ($switch !== false) {
$nextCase = $phpcsFile->findNext(array(T_CASE, T_DEFAULT), $stackPtr + 1, $tokens[$switch]['scope_closer']);
if ($nextCase !== false) {
$prevBreak = $phpcsFile->findPrevious(T_BREAK, $nextCase - 1, $stackPtr);
if ($prevBreak !== false) {
$breakWS = $phpcsFile->findNext(T_WHITESPACE, $prevBreak + 1, $nextCase - 1);
if ($breakWS !== false) {
$str = $phpcsFile->getTokensAsString($breakWS, $nextCase - $breakWS - 1);
if (!preg_match("/^\n\n[ ]*\$/Ss", $str)) {
$breakWS = false;
}
}
if ($breakWS === false) {
$phpcsFile->addError($this->getReqPrefix('REQ.PHP.2.5.14') . '"case" must has empty line between current "case" and previous "break"', $stackPtr);
}
}
}
}
} elseif ($tokens[$stackPtr]['code'] === T_DEFAULT) {
}
}
示例11: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile File where the token was found
* @param int $stackPtr Position in the stack where the token was found
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
if ($open !== false && $phpcsFile->getTokensAsString($open, 2) == '()') {
$phpcsFile->addError('Parentheses should not be used in calls to class constructors without parameters', $stackPtr);
}
}
示例12: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
// If we've already parsed this comment, pass
if ($phpcsFile == $this->_phpcsFile && $stackPtr < $this->_lastParsed) {
return;
}
$this->_phpcsFile = $phpcsFile;
$tokens = $phpcsFile->getTokens();
// Find the end of the comment block
$endPtr = $phpcsFile->findNext($tokens[$stackPtr]['code'], $stackPtr + 1, null, true);
$this->_lastParsed = $endPtr;
// Get the whole comment text
$comment = $phpcsFile->getTokensAsString($stackPtr, $endPtr - $stackPtr + 1);
$this->parser = new Scisr_CommentParser_ChangeTagValues($comment, $phpcsFile);
$this->parser->parse();
$elements = $this->parser->getTagElements();
$columns = $this->parser->getTagElementColumns();
foreach ($elements as $tagName => $tagArray) {
$method = array($this, 'process' . ucfirst($tagName));
if (is_callable($method)) {
foreach ($tagArray as $i => $tag) {
call_user_func($method, $tag, $stackPtr, $phpcsFile, $columns[$tagName][$i]);
}
}
}
}
示例13: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $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();
$errorData = array($tokens[$stackPtr]['content']);
// braces
$openingBrace = $tokens[$stackPtr]['scope_opener'];
$closingBrace = $tokens[$stackPtr]['scope_closer'];
// look for empty bodies
$body = $phpcsFile->getTokensAsString($openingBrace, $closingBrace - $openingBrace + 1);
if (preg_match('/^\\{\\s*\\}$/', $body)) {
if (preg_match('/\\s/', $body)) {
$error = 'Opening and closing braces of an empty %s must be on
the same line as the definition, with no spaces between them.';
$phpcsFile->addError($error, $stackPtr, 'EmptyBodyBraces', $errorData);
}
// body has content
} else {
parent::process($phpcsFile, $stackPtr);
// check braces alignment
if ($tokens[$openingBrace]['column'] !== $tokens[$closingBrace]['column']) {
$error = 'Closing braces must have the same indentation than
their respective opening one.';
$phpcsFile->addError($error, $closingBrace, 'BadClosingBraceIndentation', $errorData);
}
}
}
示例14: isInheritDoc
/**
* Is the comment an inheritdoc?
*
* @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 boolean True if the comment is an inheritdoc
*/
protected function isInheritDoc(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$start = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1);
$end = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $start);
$content = $phpcsFile->getTokensAsString($start, $end - $start);
return preg_match('#{@inheritDoc}#', $content) === 1;
}
示例15: 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)
{
$sequence = $phpcsFile->getTokensAsString($stackPtr, 3);
if ($sequence === "['und']" || $sequence === '["und"]') {
$warning = "Are you accessing field values here? Then you should use LANGUAGE_NONE instead of 'und'";
$phpcsFile->addWarning($warning, $stackPtr + 1, 'Und');
}
}