本文整理汇总了PHP中PHP_CodeSniffer_File::findStartOfStatement方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::findStartOfStatement方法的具体用法?PHP PHP_CodeSniffer_File::findStartOfStatement怎么用?PHP PHP_CodeSniffer_File::findStartOfStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer_File
的用法示例。
在下文中一共展示了PHP_CodeSniffer_File::findStartOfStatement方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processMultiLineCall
/**
* Processes multi-line calls.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $openBracket The position of the opening bracket
* in the stack passed in $tokens.
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return void
*/
public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
{
// We need to work out how far indented the function
// call itself is, so we can work out how far to
// indent the arguments.
$start = $phpcsFile->findStartOfStatement($stackPtr);
foreach (array('stackPtr', 'start') as $checkToken) {
$x = ${$checkToken};
for ($i = $x - 1; $i >= 0; $i--) {
if ($tokens[$i]['line'] !== $tokens[$x]['line']) {
$i++;
break;
}
}
if ($i <= 0) {
$functionIndent = 0;
} else {
if ($tokens[$i]['code'] === T_WHITESPACE) {
$functionIndent = strlen($tokens[$i]['content']);
} else {
if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) {
$functionIndent = 0;
} else {
$trimmed = ltrim($tokens[$i]['content']);
if ($trimmed === '') {
if ($tokens[$i]['code'] === T_INLINE_HTML) {
$functionIndent = strlen($tokens[$i]['content']);
} else {
$functionIndent = $tokens[$i]['column'] - 1;
}
} else {
$functionIndent = strlen($tokens[$i]['content']) - strlen($trimmed);
}
}
}
}
$varName = $checkToken . 'Indent';
${$varName} = $functionIndent;
}
//end foreach
$functionIndent = max($startIndent, $stackPtrIndent);
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $openBracket + 1, null, true);
if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
$error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket');
if ($fix === true) {
$phpcsFile->fixer->addContent($openBracket, $phpcsFile->eolChar . str_repeat(' ', $functionIndent + $this->indent));
}
}
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $closeBracket - 1, null, true);
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
$error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine');
if ($fix === true) {
$phpcsFile->fixer->addContentBefore($closeBracket, $phpcsFile->eolChar . str_repeat(' ', $functionIndent + $this->indent));
}
}
// Each line between the parenthesis should be indented n spaces.
$lastLine = $tokens[$openBracket]['line'];
$argStart = null;
$argEnd = null;
$inArg = false;
for ($i = $openBracket + 1; $i < $closeBracket; $i++) {
if ($i > $argStart && $i < $argEnd) {
$inArg = true;
} else {
$inArg = false;
}
if ($tokens[$i]['line'] !== $lastLine) {
$lastLine = $tokens[$i]['line'];
// Ignore heredoc indentation.
if (isset(PHP_CodeSniffer_Tokens::$heredocTokens[$tokens[$i]['code']]) === true) {
continue;
}
// Ignore multi-line string indentation.
if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true && $tokens[$i]['code'] === $tokens[$i - 1]['code']) {
continue;
}
// Ignore inline HTML.
if ($tokens[$i]['code'] === T_INLINE_HTML) {
continue;
}
// We changed lines, so this should be a whitespace indent token, but first make
// sure it isn't a blank line because we don't need to check indent unless there
// is actually some code to indent.
if ($tokens[$i]['code'] === T_WHITESPACE) {
//.........这里部分代码省略.........
示例2: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
* @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 ($this->_tabWidth === null) {
$cliValues = $phpcsFile->phpcs->cli->getCommandLineValues();
if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) {
// We have no idea how wide tabs are, so assume 4 spaces for fixing.
// It shouldn't really matter because indent checks elsewhere in the
// standard should fix things up.
$this->_tabWidth = 4;
} else {
$this->_tabWidth = $cliValues['tabWidth'];
}
}
$currentIndent = 0;
$lastOpenTag = $stackPtr;
$lastCloseTag = null;
$openScopes = array();
$adjustments = array();
$tokens = $phpcsFile->getTokens();
$currentIndent = $tokens[$stackPtr]['column'] - 1;
if (empty($this->_ignoreIndentationTokens) === true) {
$this->_ignoreIndentationTokens = array(T_INLINE_HTML => true);
foreach ($this->ignoreIndentationTokens as $token) {
if (is_int($token) === false) {
if (defined($token) === false) {
continue;
}
$token = constant($token);
}
$this->_ignoreIndentationTokens[$token] = true;
}
}
//end if
$this->exact = (bool) $this->exact;
$this->tabIndent = (bool) $this->tabIndent;
for ($i = $stackPtr + 1; $i < $phpcsFile->numTokens; $i++) {
if ($i === false) {
// Something has gone very wrong; maybe a parse error.
break;
}
$checkToken = null;
$checkIndent = null;
$exact = (bool) $this->exact;
if ($exact === true && isset($tokens[$i]['nested_parenthesis']) === true) {
// Don't check indents exactly between parenthesis as they
// tend to have custom rules, such as with multi-line function calls
// and control structure conditions.
$exact = false;
}
// Detect line changes and figure out where the indent is.
if ($tokens[$i]['column'] === 1) {
$trimmed = ltrim($tokens[$i]['content']);
if ($trimmed === '') {
if (isset($tokens[$i + 1]) === true && $tokens[$i]['line'] === $tokens[$i + 1]['line']) {
$checkToken = $i + 1;
$tokenIndent = $tokens[$i + 1]['column'] - 1;
}
} else {
$checkToken = $i;
$tokenIndent = strlen($tokens[$i]['content']) - strlen($trimmed);
}
}
// Closing parenthesis should just be indented to at least
// the same level as where they were opened (but can be more).
if ($checkToken !== null && $tokens[$checkToken]['code'] === T_CLOSE_PARENTHESIS && isset($tokens[$checkToken]['parenthesis_opener']) === true) {
if ($this->_debug === true) {
$line = $tokens[$i]['line'];
echo "Closing parenthesis found on line {$line}" . PHP_EOL;
}
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $tokens[$checkToken]['parenthesis_opener'], true);
$checkIndent = $tokens[$first]['column'] - 1;
if (isset($adjustments[$first]) === true) {
$checkIndent += $adjustments[$first];
}
$exact = false;
if ($this->_debug === true) {
$line = $tokens[$first]['line'];
$type = $tokens[$first]['type'];
echo "\t* first token on line {$line} is {$type} *" . PHP_EOL;
}
$prev = $phpcsFile->findStartOfStatement($first);
if ($prev !== $first) {
// This is not the start of the statement.
if ($this->_debug === true) {
$line = $tokens[$prev]['line'];
$type = $tokens[$prev]['type'];
echo "\t* previous is {$type} on line {$line} *" . PHP_EOL;
}
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true);
$prev = $phpcsFile->findStartOfStatement($first);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $prev, true);
//.........这里部分代码省略.........
示例3: processMultiLineArray
//.........这里部分代码省略.........
$spaceLength = $tokens[$nextToken - 1]['length'];
if ($spaceLength !== 1) {
$content = $tokens[$nextToken - 2]['content'];
$error = 'Expected 1 space between "%s" and double arrow; %s found';
$data = [$content, $spaceLength];
$phpcsFile->addError($error, $nextToken, 'SpaceAfterComma', $data);
}
}
//end if
if ($tokens[$nextToken + 1]['code'] !== T_WHITESPACE) {
$content = $tokens[$nextToken + 1]['content'];
$error = 'Expected 1 space between double arrow and "%s"; 0 found';
$data = [$content];
$phpcsFile->addError($error, $nextToken, 'NoSpaceAfterDoubleArrow', $data);
} else {
$spaceLength = $tokens[$nextToken + 1]['length'];
if ($spaceLength !== 1) {
$content = $tokens[$nextToken + 2]['content'];
$error = 'Expected 1 space between double arrow and "%s"; %s found';
$data = [$content, $spaceLength];
$phpcsFile->addError($error, $nextToken, 'SpaceAfterDoubleArrow', $data);
}
}
//end if
if ($singleUsed === true) {
$error = 'Key specified for array entry; first entry has no key';
$phpcsFile->addError($error, $nextToken, 'KeySpecified');
return;
}
$currentEntry['arrow'] = $nextToken;
$keyUsed = true;
// Find the start of index that uses this double arrow.
$indexEnd = $phpcsFile->findPrevious(T_WHITESPACE, $nextToken - 1, $arrayStart, true);
$indexStart = $phpcsFile->findStartOfStatement($indexEnd);
if ($indexStart === $indexEnd) {
$currentEntry['index'] = $indexEnd;
$currentEntry['index_content'] = $tokens[$indexEnd]['content'];
} else {
$currentEntry['index'] = $indexStart;
$currentEntry['index_content'] = $phpcsFile->getTokensAsString($indexStart, $indexEnd - $indexStart + 1);
}
$indexLength = strlen($currentEntry['index_content']);
if ($maxLength < $indexLength) {
$maxLength = $indexLength;
}
// Find the value of this index.
$nextContent = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextToken + 1, $arrayEnd, true);
$currentEntry['value'] = $nextContent;
$indices[] = $currentEntry;
$lastToken = $nextToken;
}
//end if
}
//end for
// Check for mutli-line arrays that should be single-line.
$singleValue = false;
if (empty($indices) === true) {
$singleValue = true;
} elseif (count($indices) === 1 && $tokens[$lastToken]['code'] === T_COMMA) {
// There may be another array value without a comma.
$exclude = PHP_CodeSniffer_Tokens::$emptyTokens;
$exclude[] = T_COMMA;
$nextContent = $phpcsFile->findNext($exclude, $indices[0]['value'] + 1, $arrayEnd, true);
if ($nextContent === false) {
$singleValue = true;
}