本文整理汇总了PHP中PHP_CodeSniffer\Files\File::addWarning方法的典型用法代码示例。如果您正苦于以下问题:PHP File::addWarning方法的具体用法?PHP File::addWarning怎么用?PHP File::addWarning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer\Files\File
的用法示例。
在下文中一共展示了File::addWarning方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
$token = $tokens[$stackPtr];
// Skip for-loop without body.
if (isset($token['parenthesis_opener']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$goodCondition = false;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if (isset(Tokens::$emptyTokens[$code]) === true) {
continue;
} else {
if ($code !== T_TRUE && $code !== T_FALSE) {
$goodCondition = true;
}
}
}
if ($goodCondition === false) {
$error = 'Avoid IF statements that are always true or false';
$phpcsFile->addWarning($error, $stackPtr, 'Found');
}
}
示例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();
// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// Detect start and end of this function definition.
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$nestingLevel = 0;
// Find the maximum nesting level of any token in the function.
for ($i = $start + 1; $i < $end; $i++) {
$level = $tokens[$i]['level'];
if ($nestingLevel < $level) {
$nestingLevel = $level;
}
}
// We subtract the nesting level of the function itself.
$nestingLevel = $nestingLevel - $tokens[$stackPtr]['level'] - 1;
if ($nestingLevel > $this->absoluteNestingLevel) {
$error = 'Function\'s nesting level (%s) exceeds allowed maximum of %s';
$data = array($nestingLevel, $this->absoluteNestingLevel);
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
} else {
if ($nestingLevel > $this->nestingLevel) {
$warning = 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function';
$data = array($nestingLevel, $this->nestingLevel);
$phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data);
}
}
}
示例3: 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);
}
}
示例4: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
* @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) {
$error = 'Possible parse error: %s missing opening or closing brace';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
return;
}
// Determine the name of the class or interface. Note that we cannot
// simply look for the first T_STRING because a class name
// starting with the number will be multiple tokens.
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $opener, true);
$nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
if ($nameEnd === false) {
$name = $tokens[$nameStart]['content'];
} else {
$name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
}
// Check for camel caps format.
$valid = Common::isCamelCaps($name, true, true, false);
if ($valid === false) {
$type = ucfirst($tokens[$stackPtr]['content']);
$error = '%s name "%s" is not in camel caps format';
$data = array($type, $name);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
$phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes');
}
}
示例5: 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)
{
$this->currentFile = $phpcsFile;
$tokens = $phpcsFile->getTokens();
// Ignore abstract methods.
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
return;
}
// Detect start and end of this function definition.
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
// Predicate nodes for PHP.
$find = array(T_CASE => true, T_DEFAULT => true, T_CATCH => true, T_IF => true, T_FOR => true, T_FOREACH => true, T_WHILE => true, T_DO => true, T_ELSEIF => true);
$complexity = 1;
// Iterate from start to end and count predicate nodes.
for ($i = $start + 1; $i < $end; $i++) {
if (isset($find[$tokens[$i]['code']]) === true) {
$complexity++;
}
}
if ($complexity > $this->absoluteComplexity) {
$error = 'Function\'s cyclomatic complexity (%s) exceeds allowed maximum of %s';
$data = array($complexity, $this->absoluteComplexity);
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
} else {
if ($complexity > $this->complexity) {
$warning = 'Function\'s cyclomatic complexity (%s) exceeds %s; consider refactoring the function';
$data = array($complexity, $this->complexity);
$phpcsFile->addWarning($warning, $stackPtr, 'TooHigh', $data);
}
}
}
示例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();
$token = $tokens[$stackPtr];
// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
return;
}
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];
$parts = array(0, 0, 0);
$index = 0;
for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];
if ($code === T_SEMICOLON) {
++$index;
} else {
if (isset(Tokens::$emptyTokens[$code]) === false) {
++$parts[$index];
}
}
}
if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
$error = 'This FOR loop can be simplified to a WHILE loop';
$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
}
}
示例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();
$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);
}
}
示例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();
$token = $tokens[$stackPtr];
// Skip for-loop without body.
if (isset($token['scope_opener']) === false) {
return;
}
// Find incrementors for outer loop.
$outer = $this->findIncrementers($tokens, $token);
// Skip if empty.
if (count($outer) === 0) {
return;
}
// Find nested for loops.
$start = ++$token['scope_opener'];
$end = --$token['scope_closer'];
for (; $start <= $end; ++$start) {
if ($tokens[$start]['code'] !== T_FOR) {
continue;
}
$inner = $this->findIncrementers($tokens, $tokens[$start]);
$diff = array_intersect($outer, $inner);
if (count($diff) !== 0) {
$error = 'Loop incrementor (%s) jumbling with inner loop';
$data = array(join(', ', $diff));
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
}
}
}
示例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)
{
$error = 'Silencing errors is forbidden';
if ($this->error === true) {
$error = 'Silencing errors is forbidden';
$phpcsFile->addError($error, $stackPtr, 'Forbidden');
} else {
$error = 'Silencing errors is discouraged';
$phpcsFile->addWarning($error, $stackPtr, 'Discouraged');
}
}
示例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();
if (empty($tokens[$stackPtr]['conditions']) === true) {
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($functionName === null) {
return;
}
// Special exception for __autoload as it needs to be global.
if ($functionName !== '__autoload') {
$error = 'Consider putting global function "%s" in a static class';
$data = array($functionName);
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
}
}
}
示例11: processTokenWithinScope
/**
* Processes the token in the specified PHP_CodeSniffer_File.
*
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this
* token was found.
* @param int $stackPtr The position where the token was found.
* @param array $currScope The current scope opener token.
*
* @return void
*/
protected final function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
if ($this->currentFile !== $phpcsFile) {
$this->currentFile = $phpcsFile;
$this->functionOpen = false;
$this->endFunction = -1;
}
$tokens = $phpcsFile->getTokens();
if ($stackPtr > $this->endFunction) {
$this->functionOpen = false;
}
if ($tokens[$stackPtr]['code'] === T_FUNCTION && $this->functionOpen === false) {
$this->functionOpen = true;
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
// If the function is abstract, or is in an interface,
// then set the end of the function to it's closing semicolon.
if ($methodProps['is_abstract'] === true || $tokens[$currScope]['code'] === T_INTERFACE) {
$this->endFunction = $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr);
} else {
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
$error = 'Possible parse error: non-abstract method defined as abstract';
$phpcsFile->addWarning($error, $stackPtr, 'Internal.ParseError.NonAbstractDefinedAbstract');
return;
}
$this->endFunction = $tokens[$stackPtr]['scope_closer'];
}
}
//end if
if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING || $tokens[$stackPtr]['code'] === T_HEREDOC) {
// Check to see if this string has a variable in it.
$pattern = '|(?<!\\\\)(?:\\\\{2})*\\${?[a-zA-Z0-9_]+}?|';
if (preg_match($pattern, $tokens[$stackPtr]['content']) !== 0) {
$this->processVariableInString($phpcsFile, $stackPtr);
}
return;
}
if ($this->functionOpen === true) {
if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
$this->processVariable($phpcsFile, $stackPtr);
}
} else {
// What if we assign a member variable to another?
// ie. private $_count = $this->_otherCount + 1;.
$this->processMemberVar($phpcsFile, $stackPtr);
}
}
示例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();
$functionName = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true);
while ($functionName !== false) {
// Check if this is really a function.
$bracket = $phpcsFile->findNext(T_WHITESPACE, $functionName + 1, null, true);
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
// Not a function call.
$functionName = $phpcsFile->findNext(T_STRING, $functionName + 1, null, false, null, true);
continue;
}
$error = 'The result of a function call should be assigned to a variable before being returned';
$phpcsFile->addWarning($error, $stackPtr, 'NotAssigned');
break;
}
}
示例13: 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
}
示例14: 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();
$content = $tokens[$stackPtr]['content'];
$matches = array();
preg_match('/(?:\\A|[^\\p{L}]+)todo([^\\p{L}]+(.*)|\\Z)/ui', $content, $matches);
if (empty($matches) === false) {
// Clear whitespace and some common characters not required at
// the end of a to-do message to make the warning more informative.
$type = 'CommentFound';
$todoMessage = trim($matches[1]);
$todoMessage = trim($todoMessage, '-:[](). ');
$error = 'Comment refers to a TODO task';
$data = array($todoMessage);
if ($todoMessage !== '') {
$type = 'TaskFound';
$error .= ' "%s"';
}
$phpcsFile->addWarning($error, $stackPtr, $type, $data);
}
}
示例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(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$namespace = '';
$findTokens = array(T_CLASS, T_INTERFACE, T_NAMESPACE, T_CLOSE_TAG);
$stackPtr = $phpcsFile->findNext($findTokens, $stackPtr + 1);
while ($stackPtr !== false) {
if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
// We can stop here. The sniff will continue from the next open
// tag when PHPCS reaches that token, if there is one.
return;
}
// Keep track of what namespace we are in.
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
$nsEnd = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE), $stackPtr + 1, null, true);
$namespace = trim($phpcsFile->getTokensAsString($stackPtr + 1, $nsEnd - $stackPtr - 1));
$stackPtr = $nsEnd;
} else {
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = $tokens[$nameToken]['content'];
if ($namespace !== '') {
$name = $namespace . '\\' . $name;
}
$compareName = strtolower($name);
if (isset($this->foundClasses[$compareName]) === true) {
$type = strtolower($tokens[$stackPtr]['content']);
$file = $this->foundClasses[$compareName]['file'];
$line = $this->foundClasses[$compareName]['line'];
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
$data = array($type, $name, $file, $line);
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
} else {
$this->foundClasses[$compareName] = array('file' => $phpcsFile->getFilename(), 'line' => $tokens[$stackPtr]['line']);
}
}
//end if
$stackPtr = $phpcsFile->findNext($findTokens, $stackPtr + 1);
}
//end while
}