本文整理汇总了PHP中PHP_CodeSniffer_File::findFirstOnLine方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::findFirstOnLine方法的具体用法?PHP PHP_CodeSniffer_File::findFirstOnLine怎么用?PHP PHP_CodeSniffer_File::findFirstOnLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer_File
的用法示例。
在下文中一共展示了PHP_CodeSniffer_File::findFirstOnLine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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)
{
$tokens = $phpcsFile->getTokens();
// If this is an inline condition (ie. there is no scope opener), then
// return, as this is not a new scope.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
// We need to actually find the first piece of content on this line,
// as if this is a method with tokens before it (public, static etc)
// or an if with an else before it, then we need to start the scope
// checking from there, rather than the current token.
$lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
$startColumn = $tokens[$lineStart]['column'];
$scopeStart = $tokens[$stackPtr]['scope_opener'];
$scopeEnd = $tokens[$stackPtr]['scope_closer'];
// Check that the closing brace is on it's own line.
$lastContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_INLINE_HTML, T_OPEN_TAG), $scopeEnd - 1, $scopeStart, true);
if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
$error = 'Closing brace must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($scopeEnd);
}
return;
}
}
示例2: checkLineLength
/**
* Checks if a line is too long.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param array $tokens The token stack.
* @param int $stackPtr The first token on the next line.
*
* @return void
*/
protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr)
{
if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$stackPtr - 1]['code']]) === TRUE) {
$doc_comment_tag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, $stackPtr - 1);
if ($doc_comment_tag !== FALSE) {
// Allow doc comment tags such as long @param tags to exceed the 80
// character limit.
return;
}
if ($tokens[$stackPtr - 1]['code'] === T_COMMENT && (preg_match('/^[[:space:]]*\\/\\/ @.+/', $tokens[$stackPtr - 1]['content']) === 1 || strpos(trim($tokens[$stackPtr - 1]['content'], "/ \n"), ' ') === FALSE)) {
// Allow @link and @see documentation to exceed the 80 character
// limit.
return;
}
// Code examples between @code and @endcode are allowed to exceed 80
// characters.
if (isset($tokens[$stackPtr]) === TRUE && $tokens[$stackPtr]['code'] === T_DOC_COMMENT_WHITESPACE) {
$tag = $phpcsFile->findPrevious(array(T_DOC_COMMENT_TAG, T_DOC_COMMENT_OPEN_TAG), $stackPtr - 1);
if ($tokens[$tag]['content'] === '@code') {
return;
}
}
// Drupal 8 annotations can have long translatable descriptions and we
// allow them to exceed 80 characters.
if ($tokens[$stackPtr - 2]['code'] === T_DOC_COMMENT_STRING && strpos($tokens[$stackPtr - 2]['content'], '@Translation(') !== FALSE) {
return;
}
// Allow comments preceded by the line with @code and ended by the line
// with @endcode to be excluded.
if ($this->isInCodeExample($phpcsFile, $stackPtr) === TRUE) {
return;
}
parent::checkLineLength($phpcsFile, $tokens, $stackPtr);
}
}
示例3: isPublicMethod
/**
* @param \PHP_CodeSniffer_File $phpCsFile
* @param int $stackPointer
*
* @return bool
*/
protected function isPublicMethod(\PHP_CodeSniffer_File $phpCsFile, $stackPointer)
{
$publicPosition = $phpCsFile->findFirstOnLine(T_PUBLIC, $stackPointer);
if ($publicPosition) {
return true;
}
return false;
}
示例4: isMethodPrivate
/**
* @param \PHP_CodeSniffer_File $phpCsFile
* @param int $stackPointer
*
* @return bool
*/
protected function isMethodPrivate(\PHP_CodeSniffer_File $phpCsFile, $stackPointer)
{
$privateTokenPointer = $phpCsFile->findFirstOnLine(T_PRIVATE, $stackPointer);
if ($privateTokenPointer) {
return true;
}
return false;
}
示例5: 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)
{
$tokens = $phpcsFile->getTokens();
// If this is an inline condition (ie. there is no scope opener), then
// return, as this is not a new scope.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
// We need to actually find the first piece of content on this line,
// as if this is a method with tokens before it (public, static etc)
// or an if with an else before it, then we need to start the scope
// checking from there, rather than the current token.
$lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
$startColumn = $tokens[$lineStart]['column'];
$scopeStart = $tokens[$stackPtr]['scope_opener'];
$scopeEnd = $tokens[$stackPtr]['scope_closer'];
// Check that the closing brace is on it's own line.
$lastContent = $phpcsFile->findPrevious(array(T_INLINE_HTML, T_WHITESPACE, T_OPEN_TAG), $scopeEnd - 1, $scopeStart, true);
if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
$error = 'Closing brace must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($scopeEnd);
}
return;
}
// Check now that the closing brace is lined up correctly.
$lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $scopeEnd, true);
$braceIndent = $tokens[$lineStart]['column'];
if ($tokens[$stackPtr]['code'] !== T_DEFAULT && $tokens[$stackPtr]['code'] !== T_CASE && $braceIndent !== $startColumn) {
$error = 'Closing brace indented incorrectly; expected %s spaces, found %s';
$data = array($startColumn - 1, $braceIndent - 1);
$fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Indent', $data);
if ($fix === true) {
$diff = $startColumn - $braceIndent;
if ($diff > 0) {
$phpcsFile->fixer->addContentBefore($scopeEnd, str_repeat(' ', $diff));
} else {
$phpcsFile->fixer->substrToken($scopeEnd - 1, 0, $diff);
}
}
}
//end if
}
示例6: 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();
if ($stackPtr === 0) {
$trim_len = -1;
} else {
$trim_len = strlen(trim($tokens[$stackPtr - 1]['content']));
}
if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
if ($tokens[$stackPtr - 1]['line'] !== $tokens[$stackPtr]['line']) {
return;
}
$lastContent = $phpcsFile->findFirstOnLine(T_OPEN_TAG, $stackPtr);
if ($lastContent === false && $trim_len === 0) {
$error = 'Closing PHP tag must not be indented when on its own line';
$phpcsFile->addError($error, $stackPtr, 'CloseTag', array());
return;
}
} elseif ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
if ($trim_len === 0 && $tokens[$stackPtr - 1]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Opening PHP tag must not be indented when on its own line';
$phpcsFile->addError($error, $stackPtr, 'OpenTag', array());
return;
}
$closeTagPtr = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr);
if ($closeTagPtr !== false && $tokens[$closeTagPtr]['line'] === $tokens[$stackPtr]['line']) {
return;
}
if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Can not have code on same line as PHP open tag';
$phpcsFile->addError($error, $stackPtr, 'SameLine', array());
return;
}
}
if ($tokens[$stackPtr]['level'] !== 0) {
if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
$openTagPtr = $phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr);
if ($openTagPtr === false || $tokens[$openTagPtr]['line'] != $tokens[$stackPtr]['line']) {
return;
}
if (isset($tokens[$openTagPtr - 1]) && $tokens[$openTagPtr - 1]['line'] != $tokens[$openTagPtr]['line']) {
$error = 'Can not use inline PHP within indented block';
$phpcsFile->addError($error, $stackPtr, 'Inline', array());
return;
}
}
}
}
示例7: checkLineLength
/**
* Checks if a line is too long.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param array $tokens The token stack.
* @param int $stackPtr The first token on the next line.
*
* @return void
*/
protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr)
{
if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$stackPtr - 1]['code']]) === true) {
$doc_comment_tag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, $stackPtr - 1);
if ($doc_comment_tag !== false) {
// Allow doc comment tags such as long @param tags to exceed the 80
// character limit.
return;
}
if ($tokens[$stackPtr - 1]['code'] === T_COMMENT && preg_match('/^[[:space:]]*\\/\\/ @.+/', $tokens[$stackPtr - 1]['content']) === 1) {
// Allow @link and @see documentation to exceed the 80 character
// limit.
return;
}
parent::checkLineLength($phpcsFile, $tokens, $stackPtr);
}
}
示例8: process
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Find first non-whitespace token on current line
$first_token = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
// If this token ( -> ) is the first token on the line, skip "space found before operator" check
if ($first_token !== $stackPtr) {
// Find token preceding operator
$prevType = $tokens[$stackPtr - 1]['code'];
// If preceding operator is whitespace, throw error
if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$error = 'Space found before object operator';
$phpcsFile->addError($error, $stackPtr, 'Before');
}
}
$nextType = $tokens[$stackPtr + 1]['code'];
if (in_array($nextType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$error = 'Space found after object operator';
$phpcsFile->addError($error, $stackPtr, 'After');
}
}
示例9: processClose
/**
* Processes the closing section of a class declaration.
*
* @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 processClose(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
$closeBrace = $tokens[$stackPtr]['scope_closer'];
// Check that the closing brace has one blank line after it.
for ($nextContent = $closeBrace + 1; $nextContent < $phpcsFile->numTokens; $nextContent++) {
// Ignore comments on the same lines as the brace.
if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line'] && ($tokens[$nextContent]['code'] === T_WHITESPACE || $tokens[$nextContent]['code'] === T_COMMENT)) {
continue;
}
if ($tokens[$nextContent]['code'] !== T_WHITESPACE) {
break;
}
}
if ($nextContent === $phpcsFile->numTokens) {
// Ignore the line check as this is the very end of the file.
$difference = 1;
} else {
$difference = $tokens[$nextContent]['line'] - $tokens[$closeBrace]['line'] - 1;
}
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $closeBrace - 1, $stackPtr, true);
if ($difference === -1 || $tokens[$lastContent]['line'] === $tokens[$closeBrace]['line']) {
$error = 'Closing %s brace must be on a line by itself';
$data = array($tokens[$stackPtr]['content']);
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceSameLine', $data);
if ($fix === true) {
if ($difference === -1) {
$phpcsFile->fixer->addNewlineBefore($nextContent);
}
if ($tokens[$lastContent]['line'] === $tokens[$closeBrace]['line']) {
$phpcsFile->fixer->addNewlineBefore($closeBrace);
}
}
} else {
if ($tokens[$closeBrace - 1]['code'] === T_WHITESPACE) {
$prevContent = $tokens[$closeBrace - 1]['content'];
if ($prevContent !== $phpcsFile->eolChar) {
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
$spaces = strlen($blankSpace);
if ($spaces !== 0) {
if ($tokens[$closeBrace - 1]['line'] !== $tokens[$closeBrace]['line']) {
$error = 'Expected 0 spaces before closing brace; newline found';
$phpcsFile->addError($error, $closeBrace, 'NewLineBeforeCloseBrace');
} else {
$error = 'Expected 0 spaces before closing brace; %s found';
$data = array($spaces);
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($closeBrace - 1, '');
}
}
}
}
}
}
//end if
if ($difference !== -1 && $difference !== 1) {
$error = 'Closing brace of a %s must be followed by a single blank line; found %s';
$data = array($tokens[$stackPtr]['content'], $difference);
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data);
if ($fix === true) {
if ($difference === 0) {
$first = $phpcsFile->findFirstOnLine(array(), $nextContent, true);
$phpcsFile->fixer->addNewlineBefore($first);
} else {
$phpcsFile->fixer->beginChangeset();
for ($i = $closeBrace + 1; $i < $nextContent; $i++) {
if ($tokens[$i]['line'] <= $tokens[$closeBrace]['line'] + 1) {
continue;
} else {
if ($tokens[$i]['line'] === $tokens[$nextContent]['line']) {
break;
}
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
}
//end if
}
示例10: 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);
//.........这里部分代码省略.........
示例11: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $file The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
*/
public function process(PHP_CodeSniffer_File $file, $stackPtr)
{
$tokens = $file->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// The end of the function occurs at the end of the argument list. Its
// like this because some people like to break long function
// declarations over multiple lines.
$openingBrace = $tokens[$stackPtr]['scope_opener'];
$parenthesisOpener = $tokens[$stackPtr]['parenthesis_opener'];
$parenthesisCloser = $tokens[$stackPtr]['parenthesis_closer'];
$functionStartLine = $tokens[$parenthesisOpener]['line'];
$functionLine = $tokens[$parenthesisCloser]['line'];
$braceLine = $tokens[$openingBrace]['line'];
$lineDifference = $braceLine - $functionLine;
$isMultiline = $functionStartLine != $functionLine;
if ($lineDifference === 0 && !$isMultiline) {
$error = 'Opening brace should be on a new line';
$fix = $file->addFixableError($error, $openingBrace, 'BraceOnSameLine');
if ($fix === true) {
$file->fixer->beginChangeset();
$indent = $file->findFirstOnLine([], $openingBrace);
if ($tokens[$indent]['code'] === T_WHITESPACE) {
$file->fixer->addContentBefore($openingBrace, $tokens[$indent]['content']);
}
$file->fixer->addNewlineBefore($openingBrace);
$file->fixer->endChangeset();
}
$file->recordMetric($stackPtr, 'Function opening brace placement', 'same line');
} else {
if ($lineDifference > 1) {
$error = 'Opening brace should be on the line after the' . ' declaration; found %s blank line(s)';
$data = [$lineDifference - 1];
$fix = $file->addFixableError($error, $openingBrace, 'BraceSpacing', $data);
if ($fix === true) {
$afterCloser = $parenthesisCloser + 1;
for ($i = $afterCloser; $i < $openingBrace; $i++) {
if ($tokens[$i]['line'] === $braceLine) {
$file->fixer->addNewLineBefore($i);
break;
}
$file->fixer->replaceToken($i, '');
}
}
}
}
$next = $file->findNext(T_WHITESPACE, $openingBrace + 1, null, true);
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
if ($next === $tokens[$stackPtr]['scope_closer']) {
// Ignore empty functions.
return;
}
$error = 'Opening brace must be the last content on the line';
$fix = $file->addFixableError($error, $openingBrace, 'ContentAfterBrace');
if ($fix === true) {
$file->fixer->addNewline($openingBrace);
}
}
// Only continue checking if the opening brace looks good.
if ($lineDifference !== 1) {
return;
}
// We need to actually find the first piece of content on this line,
// as if this is a method with tokens before it (public, static etc)
// or an if with an else before it, then we need to start the scope
// checking from there, rather than the current token.
$lineStart = $stackPtr;
while (($lineStart = $file->findPrevious(T_WHITESPACE, $lineStart - 1, null, false)) !== false) {
$position = strpos($tokens[$lineStart]['content'], $file->eolChar);
if ($position !== false) {
break;
}
}
// We found a new line, now go forward and find the first
// non-whitespace token.
$lineStart = $file->findNext(T_WHITESPACE, $lineStart, null, true);
// The opening brace is on the correct line, now it needs to be
// checked to be correctly indented.
$startColumn = $tokens[$lineStart]['column'];
$braceIndent = $tokens[$openingBrace]['column'];
if ($braceIndent !== $startColumn) {
$expected = $startColumn - 1;
$found = $braceIndent - 1;
$error = 'Opening brace indented incorrectly;' . ' expected %s spaces, found %s';
$data = [$expected, $found];
$fix = $file->addFixableError($error, $openingBrace, 'BraceIndent', $data);
if ($fix === true) {
$indent = str_repeat(' ', $expected);
if ($found === 0) {
$file->fixer->addContentBefore($openingBrace, $indent);
} else {
//.........这里部分代码省略.........
示例12: processMemberVar
/**
* 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.
*
* @return void
*/
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = PHP_CodeSniffer_Tokens::$methodPrefixes;
$ignore[] = T_VAR;
$ignore[] = T_WHITESPACE;
$start = $stackPtr;
$prev = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true);
if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[$prev]['code']]) === true) {
// Assume the comment belongs to the member var if it is on a line by itself.
$prevContent = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $prev - 1, null, true);
if ($tokens[$prevContent]['line'] !== $tokens[$prev]['line']) {
// Check the spacing, but then skip it.
$foundLines = $tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1;
if ($foundLines > 0) {
$error = 'Expected 0 blank lines after member var comment; %s found';
$data = array($foundLines);
$fix = $phpcsFile->addFixableError($error, $prev, 'AfterComment', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $prev + 1; $i <= $stackPtr; $i++) {
if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addNewline($prev);
$phpcsFile->fixer->endChangeset();
}
}
//end if
$start = $prev;
}
//end if
}
//end if
// There needs to be 0 blank line before the var, if there are not comments, otherwise 1 blank line
$expectedLines = 0;
if ($start === $stackPtr) {
// No comment found.
$first = $phpcsFile->findFirstOnLine(PHP_CodeSniffer_Tokens::$emptyTokens, $start, true);
if ($first === false) {
$first = $start;
}
} else {
if ($tokens[$start]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
$first = $tokens[$start]['comment_opener'];
$openingBracket = $phpcsFile->findPrevious(T_OPEN_CURLY_BRACKET, $first - 1, null);
$isFirst = true;
for ($i = $openingBracket + 1; $i <= $first - 1; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$isFirst = false;
break;
}
}
if (!$isFirst) {
$expectedLines = 1;
} else {
$first--;
}
} else {
$first = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $start - 1, null, true);
$first = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$commentTokens, $first + 1);
}
}
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $first - 1, null, true);
$foundLines = $tokens[$first]['line'] - $tokens[$prev]['line'] - 1;
if ($foundLines === $expectedLines) {
return;
}
if ($expectedLines === 0) {
$error = 'Expected 0 blank lines before member var without comment or first member var; %s found';
} else {
$error = 'Expected 1 blank line before member var with comment; %s found';
}
$data = array($foundLines);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
if ($expectedLines === 1) {
for ($i = $prev + 1; $i < $first; $i++) {
if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
continue;
}
if ($tokens[$i]['line'] === $tokens[$first]['line']) {
$phpcsFile->fixer->addNewline($i - 1);
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
} else {
for ($i = $prev + 1; $i < $first; $i++) {
//.........这里部分代码省略.........
示例13: _validateMultilineEmbeddedPhp
/**
* Validates embedded PHP that exists on multiple lines.
*
* @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
*/
private function _validateMultilineEmbeddedPhp(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prevTag = $phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1);
if ($prevTag === false) {
// This is the first open tag.
return;
}
$firstContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
$closingTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr);
if ($closingTag !== false) {
$nextContent = $phpcsFile->findNext(T_WHITESPACE, $closingTag + 1, $phpcsFile->numTokens, true);
if ($nextContent === false) {
// Final closing tag. It will be handled elsewhere.
return;
}
// We have an opening and a closing tag, that lie within other content.
if ($firstContent === $closingTag) {
$error = 'Empty embedded PHP tag found';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr; $i <= $closingTag; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
return;
}
}
//end if
if ($tokens[$firstContent]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Opening PHP tag must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen');
if ($fix === true) {
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
$padding = strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']));
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewline($stackPtr);
$phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $padding));
$phpcsFile->fixer->endChangeset();
}
} else {
// Check the indent of the first line, except if it is a scope closer.
if (isset($tokens[$firstContent]['scope_closer']) === false || $tokens[$firstContent]['scope_closer'] !== $firstContent) {
// Check for a blank line at the top.
if ($tokens[$firstContent]['line'] > $tokens[$stackPtr]['line'] + 1) {
// Find a token on the blank line to throw the error on.
$i = $stackPtr;
do {
$i++;
} while ($tokens[$i]['line'] !== $tokens[$stackPtr]['line'] + 1);
$error = 'Blank line found at start of embedded PHP content';
$fix = $phpcsFile->addFixableError($error, $i, 'SpacingBefore');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr + 1; $i < $firstContent; $i++) {
if ($tokens[$i]['line'] === $tokens[$firstContent]['line'] || $tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
continue;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
//end if
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr);
if ($first === false) {
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
$indent = strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']));
} else {
$indent = $tokens[$first + 1]['column'] - 1;
}
$contentColumn = $tokens[$firstContent]['column'] - 1;
if ($contentColumn !== $indent) {
$error = 'First line of embedded PHP code must be indented %s spaces; %s found';
$data = array($indent, $contentColumn);
$fix = $phpcsFile->addFixableError($error, $firstContent, 'Indent', $data);
if ($fix === true) {
$padding = str_repeat(' ', $indent);
if ($contentColumn === 0) {
$phpcsFile->fixer->addContentBefore($firstContent, $padding);
} else {
$phpcsFile->fixer->replaceToken($firstContent - 1, $padding);
}
}
}
}
//end if
}
//end if
//.........这里部分代码省略.........
示例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();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
$openingBrace = $tokens[$stackPtr]['scope_opener'];
// The end of the function occurs at the end of the argument list. Its
// like this because some people like to break long function declarations
// over multiple lines.
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
$braceLine = $tokens[$openingBrace]['line'];
$lineDifference = $braceLine - $functionLine;
if ($lineDifference === 0) {
$error = 'Opening brace should be on a new line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnSameLine');
if ($fix === true && $phpcsFile->fixer->enabled === true) {
$phpcsFile->fixer->beginChangeset();
$indent = $phpcsFile->findFirstOnLine(T_WHITESPACE, $openingBrace);
if ($indent !== false) {
$phpcsFile->fixer->addContentBefore($openingBrace, $tokens[$indent]['content']);
}
$phpcsFile->fixer->addNewlineBefore($openingBrace);
$phpcsFile->fixer->endChangeset();
}
return;
}
if ($lineDifference > 1) {
$error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
$data = array($lineDifference - 1);
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceSpacing', $data);
if ($fix === true && $phpcsFile->fixer->enabled === true) {
for ($i = $tokens[$stackPtr]['parenthesis_closer'] + 1; $i < $openingBrace; $i++) {
if ($tokens[$i]['line'] === $braceLine) {
$phpcsFile->fixer->addNewLineBefore($i);
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
}
return;
}
// We need to actually find the first piece of content on this line,
// as if this is a method with tokens before it (public, static etc)
// or an if with an else before it, then we need to start the scope
// checking from there, rather than the current token.
$lineStart = $stackPtr;
while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), $lineStart - 1, null, false)) !== false) {
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
break;
}
}
// We found a new line, now go forward and find the first non-whitespace
// token.
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true);
// The opening brace is on the correct line, now it needs to be
// checked to be correctly indented.
$startColumn = $tokens[$lineStart]['column'];
$braceIndent = $tokens[$openingBrace]['column'];
if ($braceIndent !== $startColumn) {
$expected = $startColumn - 1;
$found = $braceIndent - 1;
$error = 'Opening brace indented incorrectly; expected %s spaces, found %s';
$data = array($expected, $found);
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceIndent', $data);
if ($fix === true && $phpcsFile->fixer->enabled === true) {
$indent = str_repeat(' ', $expected);
if ($found === 0) {
$phpcsFile->fixer->addContentBefore($openingBrace, $indent);
} else {
$phpcsFile->fixer->replaceToken($openingBrace - 1, $indent);
}
}
}
//end if
}
示例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 (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
if ($tokens[$stackPtr]['code'] === T_FUNCTION && (bool) $this->checkFunctions === false || $tokens[$stackPtr]['code'] === T_CLOSURE && (bool) $this->checkClosures === false) {
return;
}
$openingBrace = $tokens[$stackPtr]['scope_opener'];
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
$use = $phpcsFile->findNext(T_USE, $closeBracket + 1, $tokens[$stackPtr]['scope_opener']);
if ($use !== false) {
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $use + 1);
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
}
}
$functionLine = $tokens[$closeBracket]['line'];
$braceLine = $tokens[$openingBrace]['line'];
$lineDifference = $braceLine - $functionLine;
if ($lineDifference === 0) {
$error = 'Opening brace should be on a new line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnSameLine');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$indent = $phpcsFile->findFirstOnLine(array(), $openingBrace);
if ($tokens[$indent]['code'] === T_WHITESPACE) {
$phpcsFile->fixer->addContentBefore($openingBrace, $tokens[$indent]['content']);
}
$phpcsFile->fixer->addNewlineBefore($openingBrace);
$phpcsFile->fixer->endChangeset();
}
$phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'same line');
} else {
if ($lineDifference > 1) {
$error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
$data = array($lineDifference - 1);
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceSpacing', $data);
if ($fix === true) {
for ($i = $tokens[$stackPtr]['parenthesis_closer'] + 1; $i < $openingBrace; $i++) {
if ($tokens[$i]['line'] === $braceLine) {
$phpcsFile->fixer->addNewLineBefore($i);
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
}
}
}
//end if
$next = $phpcsFile->findNext(T_WHITESPACE, $openingBrace + 1, null, true);
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
if ($next === $tokens[$stackPtr]['scope_closer']) {
// Ignore empty functions.
return;
}
$error = 'Opening brace must be the last content on the line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
if ($fix === true) {
$phpcsFile->fixer->addNewline($openingBrace);
}
}
// Only continue checking if the opening brace looks good.
if ($lineDifference !== 1) {
return;
}
// We need to actually find the first piece of content on this line,
// as if this is a method with tokens before it (public, static etc)
// or an if with an else before it, then we need to start the scope
// checking from there, rather than the current token.
$lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
// The opening brace is on the correct line, now it needs to be
// checked to be correctly indented.
$startColumn = $tokens[$lineStart]['column'];
$braceIndent = $tokens[$openingBrace]['column'];
if ($braceIndent !== $startColumn) {
$expected = $startColumn - 1;
$found = $braceIndent - 1;
$error = 'Opening brace indented incorrectly; expected %s spaces, found %s';
$data = array($expected, $found);
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceIndent', $data);
if ($fix === true) {
$indent = str_repeat(' ', $expected);
if ($found === 0) {
$phpcsFile->fixer->addContentBefore($openingBrace, $indent);
} else {
$phpcsFile->fixer->replaceToken($openingBrace - 1, $indent);
}
}
}
//.........这里部分代码省略.........