本文整理汇总了PHP中PHP_CodeSniffer\Files\File::findFirstOnLine方法的典型用法代码示例。如果您正苦于以下问题:PHP File::findFirstOnLine方法的具体用法?PHP File::findFirstOnLine怎么用?PHP File::findFirstOnLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer\Files\File
的用法示例。
在下文中一共展示了File::findFirstOnLine方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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(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
}
示例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(File $phpcsFile, $stackPtr)
{
$debug = Config::getConfigData('scope_indent_debug');
if ($debug !== null) {
$this->debug = (bool) $debug;
}
if ($this->tabWidth === null) {
if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->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 = $phpcsFile->config->tabWidth;
}
}
$currentIndent = 0;
$lastOpenTag = $stackPtr;
$lastCloseTag = null;
$openScopes = array();
$adjustments = array();
$setIndents = array();
$tokens = $phpcsFile->getTokens();
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
$trimmed = ltrim($tokens[$first]['content']);
if ($trimmed === '') {
$currentIndent = $tokens[$stackPtr]['column'] - 1;
} else {
$currentIndent = strlen($tokens[$first]['content']) - strlen($trimmed);
}
if ($this->debug === true) {
$line = $tokens[$stackPtr]['line'];
echo "Start with token {$stackPtr} on line {$line} with indent {$currentIndent}" . PHP_EOL;
}
if (empty($this->ignoreIndentation) === true) {
$this->ignoreIndentation = array(T_INLINE_HTML => true);
foreach ($this->ignoreIndentationTokens as $token) {
if (is_int($token) === false) {
if (defined($token) === false) {
continue;
}
$token = constant($token);
}
$this->ignoreIndentation[$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 || $tokens[$i]['code'] === T_CLOSE_PARENTHESIS && isset($tokens[$i]['parenthesis_opener']) === true && isset($tokens[$i]['parenthesis_owner']) === true && $tokens[$tokens[$i]['parenthesis_owner']]['code'] === T_ARRAY) {
if ($checkToken !== null) {
$parenCloser = $checkToken;
} else {
$parenCloser = $i;
}
if ($this->debug === true) {
$line = $tokens[$i]['line'];
echo "Closing parenthesis found on line {$line}" . PHP_EOL;
}
$parenOpener = $tokens[$parenCloser]['parenthesis_opener'];
if ($tokens[$parenCloser]['line'] !== $tokens[$parenOpener]['line']) {
$parens = 0;
//.........这里部分代码省略.........
示例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(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);
}
}
}
//.........这里部分代码省略.........
示例4: 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(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
}
示例5: 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = Tokens::$methodPrefixes;
$ignore[] = T_VAR;
$ignore[] = T_WHITESPACE;
$start = $stackPtr;
$prev = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true);
if (isset(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(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();
// Inline comments have the newline included in the content but
// docblock do not.
if ($tokens[$prev]['code'] === T_COMMENT) {
$phpcsFile->fixer->replaceToken($prev, rtrim($tokens[$prev]['content']));
}
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 1 blank line before the var, not counting comments.
if ($start === $stackPtr) {
// No comment found.
$first = $phpcsFile->findFirstOnLine(Tokens::$emptyTokens, $start, true);
if ($first === false) {
$first = $start;
}
} else {
if ($tokens[$start]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
$first = $tokens[$start]['comment_opener'];
} else {
$first = $phpcsFile->findPrevious(Tokens::$emptyTokens, $start - 1, null, true);
$first = $phpcsFile->findNext(Tokens::$commentTokens, $first + 1);
}
}
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $first - 1, null, true);
$foundLines = $tokens[$first]['line'] - $tokens[$prev]['line'] - 1;
if ($foundLines === 1) {
return;
}
$error = 'Expected 1 blank line before member var; %s found';
$data = array($foundLines);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
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, '');
}
$phpcsFile->fixer->endChangeset();
}
//end if
}