本文整理汇总了PHP中PHP_CodeSniffer_File类的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File类的具体用法?PHP PHP_CodeSniffer_File怎么用?PHP PHP_CodeSniffer_File使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PHP_CodeSniffer_File类的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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]["content"];
$recommendations = array("!=" => "!==", "==" => "===");
$phpcsFile->addWarning($this->getReqPrefix("WRN.JS.3.13.3") . "Use '" . $recommendations[$content] . "' instead of '{$content}'", $stackPtr);
}
示例2: 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$fileName = $phpcsFile->getFilename();
$matches = array();
if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) {
// Not an actions file.
return;
}
$ownClass = $matches[2];
$tokens = $phpcsFile->getTokens();
$typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr + 2, null, false, true);
$typeName = trim($tokens[$typeName]['content'], " '");
switch (strtolower($tokens[$stackPtr + 1]['content'])) {
case 'includesystem':
$included = strtolower($typeName);
break;
case 'includeasset':
$included = strtolower($typeName) . 'assettype';
break;
case 'includewidget':
$included = strtolower($typeName) . 'widgettype';
break;
default:
return;
}
if ($included === strtolower($ownClass)) {
$error = "You do not need to include \"%s\" from within the system's own actions file";
$data = array($ownClass);
$phpcsFile->addError($error, $stackPtr, 'NotRequired', $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(PHP_CodeSniffer_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 (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === 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');
}
}
示例4: 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
// We are only interested if this is the first open tag.
if ($stackPtr !== 0) {
if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) {
return;
}
}
$found = $phpcsFile->eolChar;
$found = str_replace("\n", '\\n', $found);
$found = str_replace("\r", '\\r', $found);
if ($found !== $this->eolChar) {
// 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);
$phpcsFile->addError($error, $stackPtr, 'InvalidEOLChar', $data);
}
}
示例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(PHP_CodeSniffer_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();
}
}
}
示例6: processFunctionCall
/**
* Processes this function call.
*
* @param PHP_CodeSniffer_File $phpcsFile
* The file being scanned.
* @param int $stackPtr
* The position of the function call in the stack.
* @param int $openBracket
* The position of the opening parenthesis in the stack.
* @param int $closeBracket
* The position of the closing parenthesis in the stack.
* @param Drupal_Sniffs_Semantics_FunctionCallSniff $sniff
* Can be used to retreive the function's arguments with the getArgument()
* method.
*
* @return void
*/
public function processFunctionCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $closeBracket, Drupal_Sniffs_Semantics_FunctionCallSniff $sniff)
{
$tokens = $phpcsFile->getTokens();
// We assume that the sequence '#default_value' => variable_get(...)
// indicates a variable that the module owns.
$arrow = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true);
if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) {
return;
}
$arrayKey = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $arrow - 1, null, true);
if ($arrayKey === false || $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING || substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value') {
return;
}
$argument = $sniff->getArgument(1);
// Variable name is not a literal string, so we return early.
if ($argument === false || $tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
$moduleName = DrupalPractice_Project::getName($phpcsFile);
if ($moduleName === false) {
return;
}
$variableName = substr($tokens[$argument['start']]['content'], 1, -1);
if (strpos($variableName, $moduleName) !== 0) {
$warning = 'All variables defined by your module must be prefixed with your module\'s name to avoid name collisions with others. Expected start with "%s" but found "%s"';
$data = array($moduleName, $variableName);
$phpcsFile->addWarning($warning, $argument['start'], 'VariableName', $data);
}
}
示例7: 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$start = $tokens[$stackPtr]['scope_opener'];
$end = $tokens[$stackPtr]['scope_closer'];
$properties = array();
$wantedTokens = array(T_PROPERTY, T_OPEN_CURLY_BRACKET);
$next = $phpcsFile->findNext($wantedTokens, $start + 1, $end);
while ($next !== false && $next < $end) {
// Skip nested objects.
if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) {
$next = $tokens[$next]['bracket_closer'];
} else {
$propName = $tokens[$next]['content'];
if (isset($properties[$propName]) === true) {
$line = $tokens[$properties[$propName]]['line'];
$error = "Duplicate property definition found for \"{$propName}\"; previously defined on line {$line}";
$phpcsFile->addError($error, $next);
}
$properties[$propName] = $next;
}
//end if
$next = $phpcsFile->findNext($wantedTokens, $next + 1, $end);
}
//end while
}
示例8: 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$utils = Security_Sniffs_UtilsFactory::getInstance();
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] == 'preg_replace') {
$s = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
$closer = $tokens[$s]['parenthesis_closer'];
$s = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $s + 1, $closer, true);
if ($tokens[$s]['code'] == T_CONSTANT_ENCAPSED_STRING) {
$pattern = $tokens[$s]['content'];
if (substr($pattern, 1, 1) === '/') {
// $pattern is a regex
if (preg_match('/(\\/|\\))\\w*e\\w*"$/', $pattern)) {
$phpcsFile->addWarning("Usage of preg_replace with /e modifier is not recommended.", $stackPtr, 'PregReplaceE');
$s = $phpcsFile->findNext(array(T_COMMA, T_WHITESPACE, T_COMMENT, T_DOC_COMMENT), $s + 1, $closer, true);
if ($utils::is_token_user_input($tokens[$s])) {
$phpcsFile->addError("User input and /e modifier found in preg_replace, remote code execution possible.", $stackPtr, 'PregReplaceUserInputE');
}
}
} else {
$phpcsFile->addWarning("Weird usage of preg_replace, please check manually for /e modifier.", $stackPtr, 'PregReplaceWeird');
}
} elseif ($tokens[$s]['code'] == T_VARIABLE && $utils::is_token_user_input($tokens[$s])) {
$phpcsFile->addError("User input found in preg_replace, /e modifier could be used for malicious intent.", $stackPtr, 'PregReplaceUserInput');
} else {
$phpcsFile->addWarning("Dynamic usage of preg_replace, please check manually for /e modifier or user input.", $stackPtr, 'PregReplaceDyn');
}
}
}
示例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(PHP_CodeSniffer_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(PHP_CodeSniffer_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');
}
}
示例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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
// Ignore the ELSE in ELSE IF. We'll process the IF part later.
if ($tokens[$stackPtr]['code'] === T_ELSE && $tokens[$stackPtr + 2]['code'] === T_IF) {
return;
}
if ($tokens[$stackPtr]['code'] === T_WHILE) {
// This could be from a DO WHILE, which doesn't have an opening brace.
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
$brace = $tokens[$lastContent];
if (isset($brace['scope_condition']) === true) {
$condition = $tokens[$brace['scope_condition']];
if ($condition['code'] === T_DO) {
return;
}
}
}
}
// This is a control structure without an opening brace,
// so it is an inline statement.
if ($this->error === true) {
$phpcsFile->addError('Inline control structures are not allowed', $stackPtr);
} else {
$phpcsFile->addWarning('Inline control structures are discouraged', $stackPtr);
}
return;
}
//end if
}
示例11: 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(PHP_CodeSniffer_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->replaceToken($last, '');
}
$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;
}
示例12: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param PHP_CodeSniffer_File $phpcsFile File where the token was found
* @param int $stackPtr Position in the stack where the token was found
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
if ($open !== false && $phpcsFile->getTokensAsString($open, 2) == '()') {
$phpcsFile->addError('Parentheses should not be used in calls to class constructors without parameters', $stackPtr);
}
}
示例13: process
/**
* Run whenever a '.' is encountered
*
* @param PHP_CodeSniffer_File $file The file being scanned.
* @param int $stack_ix The position of the current token in the stack
*/
public function process(PHP_CodeSniffer_File $file, $stack_ix)
{
$tokens = $file->getTokens();
if ($tokens[$stack_ix - 1]['code'] !== T_WHITESPACE || $tokens[$stack_ix + 1]['code'] !== T_WHITESPACE) {
$file->addError('Concat operator must be surrounded by spaces', $stack_ix, 'ConcatUnspaced');
}
}
示例14: 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$lastLine = $tokens[$stackPtr]['line'];
$end = $tokens[$stackPtr]['bracket_closer'];
$endLine = $tokens[$end]['line'];
// Do not check nested style definitions as, for example, in @media style rules.
$nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $stackPtr + 1, $end);
if ($nested !== false) {
return;
}
$foundColon = false;
$foundString = false;
for ($i = $stackPtr + 1; $i <= $end; $i++) {
if ($tokens[$i]['line'] !== $lastLine) {
// We changed lines.
if ($foundColon === false && $foundString !== false) {
// We didn't find a colon on the previous line.
$error = 'No style definition found on line; check for missing colon';
$phpcsFile->addError($error, $foundString, 'Found');
}
$foundColon = false;
$foundString = false;
$lastLine = $tokens[$i]['line'];
}
if ($tokens[$i]['code'] === T_STRING) {
$foundString = $i;
} else {
if ($tokens[$i]['code'] === T_COLON) {
$foundColon = $i;
}
}
}
//end for
}
示例15: processMemberVar
/**
* Processes class member variables.
*
* @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 processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
if (empty($memberProps) === true) {
return;
}
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
$isPublic = $memberProps['scope'] === 'private' ? false : true;
$scope = $memberProps['scope'];
$scopeSpecified = $memberProps['scope_specified'];
// If it's a private member, it must have an underscore on the front.
if ($isPublic === false && $memberName[0] !== '_') {
$error = 'Private member variable "%s" must be prefixed with an underscore';
$data = array($memberName);
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
return;
}
// If it's not a private member, it must not have an underscore on the front.
if ($isPublic === true && $scopeSpecified === true && $memberName[0] === '_') {
$error = '%s member variable "%s" must not be prefixed with an underscore';
$data = array(ucfirst($scope), $memberName);
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
return;
}
}