本文整理汇总了PHP中PHP_CodeSniffer\Files\File::findPrevious方法的典型用法代码示例。如果您正苦于以下问题:PHP File::findPrevious方法的具体用法?PHP File::findPrevious怎么用?PHP File::findPrevious使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer\Files\File
的用法示例。
在下文中一共展示了File::findPrevious方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
}
示例2: 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();
if ($tokens[$stackPtr]['content'][1] === '_') {
$error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
}
// Detect multiple properties defined at the same time. Throw an error
// for this, but also only process the first property in the list so we don't
// repeat errors.
$find = Tokens::$scopeModifiers;
$find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
$prev = $phpcsFile->findPrevious($find, $stackPtr - 1);
if ($tokens[$prev]['code'] === T_VARIABLE) {
return;
}
if ($tokens[$prev]['code'] === T_VAR) {
$error = 'The var keyword must not be used to declare a property';
$phpcsFile->addError($error, $stackPtr, 'VarUsed');
}
$next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), $stackPtr + 1);
if ($tokens[$next]['code'] === T_VARIABLE) {
$error = 'There must not be more than one property declared per statement';
$phpcsFile->addError($error, $stackPtr, 'Multiple');
}
$modifier = $phpcsFile->findPrevious(Tokens::$scopeModifiers, $stackPtr);
if ($modifier === false || $tokens[$modifier]['line'] !== $tokens[$stackPtr]['line']) {
$error = 'Visibility must be declared on property "%s"';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
}
}
示例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();
// Ignore this.something and other uses of "this" that are not
// direct assignments.
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($tokens[$next]['code'] !== T_SEMICOLON) {
if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
return;
}
}
// Something must be assigned to "this".
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$prev]['code'] !== T_EQUAL) {
return;
}
// A variable needs to be assigned to "this".
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $prev - 1, null, true);
if ($tokens[$prev]['code'] !== T_STRING) {
return;
}
// We can only assign "this" to a var called "self".
if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') {
$error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"';
$phpcsFile->addError($error, $prev, 'NotSelf');
}
}
示例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();
// Find the content of each class definition name.
$classNames = array();
$next = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $stackPtr + 1);
if ($next === false) {
// No class definitions in the file.
return;
}
// Save the class names in a "scope",
// to prevent false positives with @media blocks.
$scope = 'main';
$find = array(T_CLOSE_CURLY_BRACKET, T_OPEN_CURLY_BRACKET, T_COMMENT, T_OPEN_TAG);
while ($next !== false) {
$prev = $phpcsFile->findPrevious($find, $next - 1);
// Check if an inner block was closed.
$beforePrev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $prev - 1, null, true);
if ($beforePrev !== false && $tokens[$beforePrev]['code'] === T_CLOSE_CURLY_BRACKET) {
$scope = 'main';
}
// Create a sorted name for the class so we can compare classes
// even when the individual names are all over the place.
$name = '';
for ($i = $prev + 1; $i < $next; $i++) {
$name .= $tokens[$i]['content'];
}
$name = trim($name);
$name = str_replace("\n", ' ', $name);
$name = preg_replace('|[\\s]+|', ' ', $name);
$name = str_replace(', ', ',', $name);
$names = explode(',', $name);
sort($names);
$name = implode(',', $names);
if ($name[0] === '@') {
// Media block has its own "scope".
$scope = $name;
} else {
if (isset($classNames[$scope][$name]) === true) {
$first = $classNames[$scope][$name];
$error = 'Duplicate class definition found; first defined on line %s';
$data = array($tokens[$first]['line']);
$phpcsFile->addError($error, $next, 'Found', $data);
} else {
$classNames[$scope][$name] = $next;
}
}
$next = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $next + 1);
}
//end while
}
示例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 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();
// Check if the next non whitespace token is a string.
$index = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($tokens[$index]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
// Make sure it is the only thing in the square brackets.
$next = $phpcsFile->findNext(T_WHITESPACE, $index + 1, null, true);
if ($tokens[$next]['code'] !== T_CLOSE_SQUARE_BRACKET) {
return;
}
// Allow indexes that have dots in them because we can't write
// them in dot notation.
$content = trim($tokens[$index]['content'], '"\' ');
if (strpos($content, '.') !== false) {
return;
}
// Also ignore reserved words.
if ($content === 'super') {
return;
}
// Token before the opening square bracket cannot be a var name.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$prev]['code'] === T_STRING) {
$error = 'Object indexes must be written in dot notation';
$phpcsFile->addError($error, $prev, 'Found');
}
}
示例7: 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;
}
示例8: 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();
}
}
示例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();
$find = Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;
$commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT) {
$phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing');
$phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'no');
return;
}
$phpcsFile->recordMetric($stackPtr, 'Class has doc comment', 'yes');
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
$phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle');
return;
}
if ($tokens[$commentEnd]['line'] !== $tokens[$stackPtr]['line'] - 1) {
$error = 'There must be no blank lines after the class comment';
$phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
}
$commentStart = $tokens[$commentEnd]['comment_opener'];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
$error = '%s tag is not allowed in class comment';
$data = array($tokens[$tag]['content']);
$phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
}
}
示例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(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();
}
}
}
示例11: 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, '');
}
}
}
}
示例12: 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();
$find = Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;
$commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
// Inline comments might just be closing comments for
// control structures or functions instead of function comments
// using the wrong comment type. If there is other code on the line,
// assume they relate to that code.
$prev = $phpcsFile->findPrevious($find, $commentEnd - 1, null, true);
if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) {
$commentEnd = $prev;
}
}
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT) {
$phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
$phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no');
return;
} else {
$phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes');
}
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
$phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle');
return;
}
if ($tokens[$commentEnd]['line'] !== $tokens[$stackPtr]['line'] - 1) {
$error = 'There must be no blank lines after the function comment';
$phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
}
$commentStart = $tokens[$commentEnd]['comment_opener'];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] === '@see') {
// Make sure the tag isn't empty.
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
$error = 'Content missing for @see tag in function comment';
$phpcsFile->addError($error, $tag, 'EmptySees');
}
}
}
$this->processReturn($phpcsFile, $stackPtr, $commentStart);
$this->processThrows($phpcsFile, $stackPtr, $commentStart);
$this->processParams($phpcsFile, $stackPtr, $commentStart);
}
示例13: processVariable
/**
* 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
*/
protected function processVariable(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');
$phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS', 'http_response_header', 'HTTP_RAW_POST_DATA', 'php_errormsg');
// If it's a php reserved var, then its ok.
if (in_array($varName, $phpReservedVars) === true) {
return;
}
$objOperator = $phpcsFile->findNext(array(T_WHITESPACE), $stackPtr + 1, null, true);
if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
// Check to see if we are using a variable from an object.
$var = $phpcsFile->findNext(array(T_WHITESPACE), $objOperator + 1, null, true);
if ($tokens[$var]['code'] === T_STRING) {
$bracket = $phpcsFile->findNext(array(T_WHITESPACE), $var + 1, null, true);
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
$objVarName = $tokens[$var]['content'];
// There is no way for us to know if the var is public or
// private, so we have to ignore a leading underscore if there is
// one and just check the main part of the variable name.
$originalVarName = $objVarName;
if (substr($objVarName, 0, 1) === '_') {
$objVarName = substr($objVarName, 1);
}
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
$error = 'Variable "%s" is not in valid camel caps format';
$data = array($originalVarName);
$phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
}
}
//end if
}
//end if
}
//end if
// There is no way for us to know if the var is public or private,
// so we have to ignore a leading underscore if there is one and just
// check the main part of the variable name.
$originalVarName = $varName;
if (substr($varName, 0, 1) === '_') {
$objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true);
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
// The variable lives within a class, and is referenced like
// this: MyClass::$_variable, so we don't know its scope.
$inClass = true;
} else {
$inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE, T_TRAIT));
}
if ($inClass === true) {
$varName = substr($varName, 1);
}
}
if (Common::isCamelCaps($varName, false, true, false) === false) {
$error = 'Variable "%s" is not in valid camel caps format';
$data = array($originalVarName);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
}
}
示例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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Equal sign can't be the last thing on the line.
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($next === false) {
// Bad assignment.
return;
}
if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
$error = 'Multi-line assignments must have the equal sign on the second line';
$phpcsFile->addError($error, $stackPtr, 'EqualSignLine');
return;
}
// Make sure it is the first thing on the line, otherwise we ignore it.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, false, true);
if ($prev === false) {
// Bad assignment.
return;
}
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
return;
}
// Find the required indent based on the ident of the previous line.
$assignmentIndent = 0;
$prevLine = $tokens[$prev]['line'];
for ($i = $prev - 1; $i >= 0; $i--) {
if ($tokens[$i]['line'] !== $prevLine) {
$i++;
break;
}
}
if ($tokens[$i]['code'] === T_WHITESPACE) {
$assignmentIndent = strlen($tokens[$i]['content']);
}
// Find the actual indent.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1);
$expectedIndent = $assignmentIndent + $this->indent;
$foundIndent = strlen($tokens[$prev]['content']);
if ($foundIndent !== $expectedIndent) {
$error = 'Multi-line assignment not indented correctly; expected %s spaces but found %s';
$data = array($expectedIndent, $foundIndent);
$phpcsFile->addError($error, $stackPtr, 'Indent', $data);
}
}
示例15: 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 it is an API function. We know this by the doc comment.
$commentEnd = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr);
$commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $commentEnd - 1);
$comment = $phpcsFile->getTokensAsString($commentStart, $commentEnd - $commentStart);
if (strpos($comment, '* @api') === false) {
return;
}
// Find all the vars passed in as we are only interested in comparisons
// to NULL for these specific variables.
$foundVars = array();
$open = $tokens[$stackPtr]['parenthesis_opener'];
$close = $tokens[$stackPtr]['parenthesis_closer'];
for ($i = $open + 1; $i < $close; $i++) {
if ($tokens[$i]['code'] === T_VARIABLE) {
$foundVars[$tokens[$i]['content']] = true;
}
}
if (empty($foundVars) === true) {
return;
}
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
for ($i = $start + 1; $i < $end; $i++) {
if ($tokens[$i]['code'] !== T_VARIABLE || isset($foundVars[$tokens[$i]['content']]) === false) {
continue;
}
$operator = $phpcsFile->findNext(T_WHITESPACE, $i + 1, null, true);
if ($tokens[$operator]['code'] !== T_IS_IDENTICAL && $tokens[$operator]['code'] !== T_IS_NOT_IDENTICAL) {
continue;
}
$nullValue = $phpcsFile->findNext(T_WHITESPACE, $operator + 1, null, true);
if ($tokens[$nullValue]['code'] !== T_NULL) {
continue;
}
$error = 'Values submitted via Ajax requests should not be compared directly to NULL; use empty() instead';
$phpcsFile->addWarning($error, $nullValue, 'Found');
}
//end for
}