本文整理汇总了PHP中PHP_CodeSniffer\Files\File::addFixableError方法的典型用法代码示例。如果您正苦于以下问题:PHP File::addFixableError方法的具体用法?PHP File::addFixableError怎么用?PHP File::addFixableError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer\Files\File
的用法示例。
在下文中一共展示了File::addFixableError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$colour = $tokens[$stackPtr]['content'];
$expected = strtoupper($colour);
if ($colour !== $expected) {
$error = 'CSS colours must be defined in uppercase; expected %s but found %s';
$data = array($expected, $colour);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotUpper', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
}
}
// Now check if shorthand can be used.
if (strlen($colour) !== 7) {
return;
}
if ($colour[1] === $colour[2] && $colour[3] === $colour[4] && $colour[5] === $colour[6]) {
$expected = '#' . $colour[1] . $colour[3] . $colour[5];
$error = 'CSS colours must use shorthand if available; expected %s but found %s';
$data = array($expected, $colour);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Shorthand', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
}
}
}
示例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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr + 1]['code'] === T_SEMICOLON) {
// No content for this language construct.
return;
}
if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
$content = $tokens[$stackPtr + 1]['content'];
$contentLength = strlen($content);
if ($contentLength !== 1) {
$error = 'Language constructs must be followed by a single space; expected 1 space but found %s';
$data = array($contentLength);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr + 1, ' ');
}
}
} else {
if ($tokens[$stackPtr + 1]['code'] !== T_OPEN_PARENTHESIS) {
$error = 'Language constructs must be followed by a single space; expected "%s" but found "%s"';
$data = array($tokens[$stackPtr]['content'] . ' ' . $tokens[$stackPtr + 1]['content'], $tokens[$stackPtr]['content'] . $tokens[$stackPtr + 1]['content']);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$phpcsFile->fixer->addContent($stackPtr, ' ');
}
}
}
//end if
}
示例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();
$openTag = $tokens[$stackPtr];
if ($openTag['content'] === '<?') {
$error = 'Short PHP opening tag used; expected "<?php" but found "%s"';
$data = array($openTag['content']);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php');
}
$phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'yes');
} else {
$phpcsFile->recordMetric($stackPtr, 'PHP short open tag used', 'no');
}
if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO) {
$nextVar = $tokens[$phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true)];
$error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
$data = array($nextVar['content'], $openTag['content'], $nextVar['content']);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'EchoFound', $data);
if ($fix === true) {
if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo ');
} else {
$phpcsFile->fixer->replaceToken($stackPtr, '<?php echo');
}
}
}
}
示例4: 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
if ($tokens[$prev]['code'] !== T_STYLE) {
// The colon is not part of a style definition.
return;
}
if ($tokens[$prev]['content'] === 'progid') {
// Special case for IE filters.
return;
}
if ($tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
$error = 'There must be no space before a colon in a style definition';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
}
}
if ($tokens[$stackPtr + 1]['code'] === T_SEMICOLON) {
// Empty style definition, ignore it.
return;
}
if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after colon in style definition; 0 found';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoneAfter');
if ($fix === true) {
$phpcsFile->fixer->addContent($stackPtr, ' ');
}
} else {
$content = $tokens[$stackPtr + 1]['content'];
if (strpos($content, $phpcsFile->eolChar) === false) {
$length = strlen($content);
if ($length !== 1) {
$error = 'Expected 1 space after colon in style definition; %s found';
$data = array($length);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'After', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr + 1, ' ');
}
}
} else {
$error = 'Expected 1 space after colon in style definition; newline found';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'AfterNewline');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr + 1, ' ');
}
}
}
//end if
}
示例5: 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($next === false) {
return;
}
if ($tokens[$next]['code'] !== T_CLOSE_TAG) {
$found = $tokens[$next]['line'] - $tokens[$stackPtr]['line'] - 1;
if ($found !== 1) {
$error = 'Expected one blank line after closing brace of class definition; %s found';
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterClose', $data);
if ($fix === true) {
if ($found === 0) {
$phpcsFile->fixer->addNewline($stackPtr);
} else {
$nextContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
$phpcsFile->fixer->beginChangeset();
for ($i = $stackPtr + 1; $i < $nextContent - 1; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addNewline($i);
$phpcsFile->fixer->endChangeset();
}
}
}
//end if
}
//end if
// Ignore nested style definitions from here on. The spacing before the closing brace
// (a single blank line) will be enforced by the above check, which ensures there is a
// blank line after the last nested class.
$found = $phpcsFile->findPrevious(T_CLOSE_CURLY_BRACKET, $stackPtr - 1, $tokens[$stackPtr]['bracket_opener']);
if ($found !== false) {
return;
}
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
if ($prev === false) {
return;
}
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Closing brace of class definition must be on new line';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeClose');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($stackPtr);
}
}
}
示例6: 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== 'opacity') {
return;
}
$next = $phpcsFile->findNext(array(T_COLON, T_WHITESPACE), $stackPtr + 1, null, true);
if ($next === false || $tokens[$next]['code'] !== T_DNUMBER && $tokens[$next]['code'] !== T_LNUMBER) {
return;
}
$value = $tokens[$next]['content'];
if ($tokens[$next]['code'] === T_LNUMBER) {
if ($value !== '0' && $value !== '1') {
$error = 'Opacity values must be between 0 and 1';
$phpcsFile->addError($error, $next, 'Invalid');
}
} else {
if (strlen($value) > 3) {
$error = 'Opacity values must have a single value after the decimal point';
$phpcsFile->addError($error, $next, 'DecimalPrecision');
} else {
if ($value === '0.0' || $value === '1.0') {
$error = 'Opacity value does not require decimal point; use %s instead';
$data = array($value[0]);
$fix = $phpcsFile->addFixableError($error, $next, 'PointNotRequired', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($next, $value[0]);
}
} else {
if ($value[0] === '.') {
$error = 'Opacity values must not start with a decimal point; use 0%s instead';
$data = array($value);
$fix = $phpcsFile->addFixableError($error, $next, 'StartWithPoint', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($next, '0' . $value);
}
} else {
if ($value[0] !== '0') {
$error = 'Opacity values must be between 0 and 1';
$phpcsFile->addError($error, $next, 'Invalid');
}
}
}
}
//end if
}
//end if
}
示例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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prevType = $tokens[$stackPtr - 1]['code'];
if (isset(Tokens::$emptyTokens[$prevType]) === false) {
return;
}
$nonSpace = $phpcsFile->findPrevious(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();
}
}
示例8: process
/**
* Processes this sniff, 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 int
*/
public function process(File $phpcsFile, $stackPtr)
{
$found = $phpcsFile->eolChar;
$found = str_replace("\n", '\\n', $found);
$found = str_replace("\r", '\\r', $found);
$phpcsFile->recordMetric($stackPtr, 'EOL char', $found);
if ($found === $this->eolChar) {
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
// Check for single line files without an EOL. This is a very special
// case and the EOL char is set to \n when this happens.
if ($found === '\\n') {
$tokens = $phpcsFile->getTokens();
$lastToken = $phpcsFile->numTokens - 1;
if ($tokens[$lastToken]['line'] === 1 && $tokens[$lastToken]['content'] !== "\n") {
return;
}
}
$error = 'End of line character is invalid; expected "%s" but found "%s"';
$expected = $this->eolChar;
$expected = str_replace("\n", '\\n', $expected);
$expected = str_replace("\r", '\\r', $expected);
$data = array($expected, $found);
// Errors are always reported on line 1, no matter where the first PHP tag is.
$fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data);
if ($fix === true) {
$tokens = $phpcsFile->getTokens();
switch ($this->eolChar) {
case '\\n':
$eolChar = "\n";
break;
case '\\r':
$eolChar = "\r";
break;
case '\\r\\n':
$eolChar = "\r\n";
break;
default:
$eolChar = $this->eolChar;
break;
}
for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
if (isset($tokens[$i + 1]) === false || $tokens[$i + 1]['line'] > $tokens[$i]['line']) {
// Token is the last on a line.
if (isset($tokens[$i]['orig_content']) === true) {
$tokenContent = $tokens[$i]['orig_content'];
} else {
$tokenContent = $tokens[$i]['content'];
}
$newContent = rtrim($tokenContent, "\r\n");
$newContent .= $eolChar;
$phpcsFile->fixer->replaceToken($i, $newContent);
}
}
}
//end if
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}
示例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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// We are only interested in the first token in a multi-line string.
if ($tokens[$stackPtr]['code'] === $tokens[$stackPtr - 1]['code']) {
return;
}
$workingString = $tokens[$stackPtr]['content'];
$lastStringToken = $stackPtr;
$i = $stackPtr + 1;
if (isset($tokens[$i]) === true) {
while ($i < $phpcsFile->numTokens && $tokens[$i]['code'] === $tokens[$stackPtr]['code']) {
$workingString .= $tokens[$i]['content'];
$lastStringToken = $i;
$i++;
}
}
// Check if it's a double quoted string.
if (strpos($workingString, '"') === false) {
return;
}
// Make sure it's not a part of a string started in a previous line.
// If it is, then we have already checked it.
if ($workingString[0] !== '"') {
return;
}
// The use of variables in double quoted strings is not allowed.
if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
$stringTokens = token_get_all('<?php ' . $workingString);
foreach ($stringTokens as $token) {
if (is_array($token) === true && $token[0] === T_VARIABLE) {
$error = 'Variable "%s" not allowed in double quoted string; use concatenation instead';
$data = array($token[1]);
$phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data);
}
}
return;
}
//end if
$allowedChars = array('\\0', '\\1', '\\2', '\\3', '\\4', '\\5', '\\6', '\\7', '\\n', '\\r', '\\f', '\\t', '\\v', '\\x', '\\b', '\\e', '\\u', '\'');
foreach ($allowedChars as $testChar) {
if (strpos($workingString, $testChar) !== false) {
return;
}
}
$error = 'String %s does not require double quotes; use single quotes instead';
$data = array(str_replace("\n", '\\n', $workingString));
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$innerContent = substr($workingString, 1, -1);
$innerContent = str_replace('\\"', '"', $innerContent);
$phpcsFile->fixer->replaceToken($stackPtr, "'{$innerContent}'");
while ($lastStringToken !== $stackPtr) {
$phpcsFile->fixer->replaceToken($lastStringToken, '');
$lastStringToken--;
}
$phpcsFile->fixer->endChangeset();
}
}
示例10: process
/**
* Processes this sniff, 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 (substr($tokens[$stackPtr]['content'], 0, 2) !== '//') {
return;
}
$commentLine = $tokens[$stackPtr]['line'];
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$lastContent]['line'] !== $commentLine) {
return;
}
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}
// Special case for JS files.
if ($tokens[$lastContent]['code'] === T_COMMA || $tokens[$lastContent]['code'] === T_SEMICOLON) {
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $lastContent - 1, null, true);
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}
}
$error = 'Comments may not appear after statements';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
if ($fix === true) {
$phpcsFile->fixer->addNewlineBefore($stackPtr);
}
}
示例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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
// Probably an interface method.
return;
}
$openBrace = $tokens[$stackPtr]['scope_opener'];
$nextContent = $phpcsFile->findNext(T_WHITESPACE, $openBrace + 1, null, true);
if ($nextContent === $tokens[$stackPtr]['scope_closer']) {
// The next bit of content is the closing brace, so this
// is an empty function and should have a blank line
// between the opening and closing braces.
return;
}
$braceLine = $tokens[$openBrace]['line'];
$nextLine = $tokens[$nextContent]['line'];
$found = $nextLine - $braceLine - 1;
if ($found > 0) {
$error = 'Expected 0 blank lines after opening function brace; %s found';
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $openBrace, 'SpacingAfter', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $openBrace + 1; $i < $nextContent; $i++) {
if ($tokens[$i]['line'] === $nextLine) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addNewline($openBrace);
$phpcsFile->fixer->endChangeset();
}
}
}
示例12: process
/**
* Processes this sniff, 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();
// Make sure this file only contains PHP code.
for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['code'] === T_INLINE_HTML && trim($tokens[$i]['content']) !== '') {
return $phpcsFile->numTokens;
}
}
// Find the last non-empty token.
for ($last = $phpcsFile->numTokens - 1; $last > 0; $last--) {
if (trim($tokens[$last]['content']) !== '') {
break;
}
}
if ($tokens[$last]['code'] === T_CLOSE_TAG) {
$error = 'A closing tag is not permitted at the end of a PHP file';
$fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar);
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $last - 1, null, true);
if ($tokens[$prev]['code'] !== T_SEMICOLON && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET) {
$phpcsFile->fixer->addContent($prev, ';');
}
$phpcsFile->fixer->endChangeset();
}
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes');
} else {
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'no');
}
// Ignore the rest of the file.
return $phpcsFile->numTokens;
}
示例13: 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();
$firstContent = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
// If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
if ($tokens[$firstContent]['code'] !== T_OPEN_PARENTHESIS) {
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
return;
}
$end = $phpcsFile->findNext(array(T_SEMICOLON, T_CLOSE_TAG), $stackPtr, null, false);
// If the token before the semi-colon is not a closing parenthesis, then we are not concerned.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $end - 1, null, true);
if ($tokens[$prev]['code'] !== T_CLOSE_PARENTHESIS) {
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
return;
}
// If the parenthesis don't match, then we are not concerned.
if ($tokens[$firstContent]['parenthesis_closer'] !== $prev) {
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'no');
return;
}
$phpcsFile->recordMetric($stackPtr, 'Brackets around echoed strings', 'yes');
if ($phpcsFile->findNext(Tokens::$operators, $stackPtr, $end, false) === false) {
// There are no arithmetic operators in this.
$error = 'Echoed strings should not be bracketed';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'HasBracket');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($firstContent, '');
$phpcsFile->fixer->replaceToken($end - 1, '');
$phpcsFile->fixer->endChangeset();
}
}
}
示例14: 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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// PHP 5.4 introduced a shorthand array declaration syntax, so we need
// to ignore the these type of array declarations because this sniff is
// only dealing with array usage.
if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) {
$openBracket = $stackPtr;
} else {
if (isset($tokens[$stackPtr]['bracket_opener']) === false) {
return;
}
$openBracket = $tokens[$stackPtr]['bracket_opener'];
}
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $openBracket - 1, null, true);
if ($tokens[$prev]['code'] === T_EQUAL) {
return;
}
// Square brackets can not have a space before them.
$prevType = $tokens[$stackPtr - 1]['code'];
if (isset(Tokens::$emptyTokens[$prevType]) === true) {
$nonSpace = $phpcsFile->findPrevious(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);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
}
}
// Open square brackets can't ever have spaces after them.
if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) {
$nextType = $tokens[$stackPtr + 1]['code'];
if (isset(Tokens::$emptyTokens[$nextType]) === true) {
$nonSpace = $phpcsFile->findNext(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);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
}
}
}
}
示例15: 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)
{
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;
}
}
$checkTokens = array(T_WHITESPACE => true, T_INLINE_HTML => true, T_DOC_COMMENT_WHITESPACE => true);
$tokens = $phpcsFile->getTokens();
for ($i = $stackPtr + 1; $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['column'] !== 1 || isset($checkTokens[$tokens[$i]['code']]) === false) {
continue;
}
// If tabs are being converted to spaces, the original content
// should be used instead of the converted content.
if (isset($tokens[$i]['orig_content']) === true) {
$content = $tokens[$i]['orig_content'];
} else {
$content = $tokens[$i]['content'];
}
if ($content[0] === ' ') {
if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE && $content === ' ') {
// Ignore file/class-level DocBlock.
continue;
}
// Space are considered ok if they are proceeded by tabs and not followed
// by tabs, as is the case with standard docblock comments.
$phpcsFile->recordMetric($i, 'Line indent', 'spaces');
$error = 'Tabs must be used to indent lines; spaces are not allowed';
$fix = $phpcsFile->addFixableError($error, $i, 'SpacesUsed');
if ($fix === true) {
$trimmed = ltrim($content, ' ');
$numSpaces = strlen($content) - strlen($trimmed);
if ($numSpaces < $this->tabWidth) {
$numTabs = 1;
$padding = "\t";
} else {
$numTabs = floor($numSpaces / $this->tabWidth);
$remaining = $numSpaces - $numTabs * $this->tabWidth;
$padding = str_repeat("\t", $numTabs) . ($padding = str_repeat(' ', $remaining));
}
$phpcsFile->fixer->replaceToken($i, $padding . $trimmed);
}
} else {
if ($content[0] === "\t") {
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
}
}
//end if
}
//end for
// Ignore the rest of the file.
return $phpcsFile->numTokens + 1;
}