PHP_CodeSniffer_File PHP Class Code Examples

This page contains top rated real world PHP examples of class PHP_CodeSniffer_File extracted from open source projects. You can rate examples to help us improve the quality of examples

Example #1
Show file File: NonceVerificationSniff.php Project: sampath004/WordPress-Coding-Standards
0
 /**
  * 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)
 {
     // Merge any custom functions with the defaults, if we haven't already.
     if (!self::$addedCustomFunctions) {
         self::$nonceVerificationFunctions = array_merge(self::$nonceVerificationFunctions, array_flip($this->customNonceVerificationFunctions));
         self::$addedCustomFunctions = true;
     }
     $this->init($phpcsFile);
     $tokens = $phpcsFile->getTokens();
     $instance = $tokens[$stackPtr];
     $superglobals = array_merge($this->errorForSuperGlobals, $this->warnForSuperGlobals);
     if (!in_array($instance['content'], $superglobals)) {
         return;
     }
     if ($this->has_whitelist_comment('CSRF', $stackPtr)) {
         return;
     }
     if ($this->is_assignment($stackPtr)) {
         return;
     }
     if ($this->is_only_sanitized($stackPtr)) {
         return;
     }
     if ($this->has_nonce_check($stackPtr)) {
         return;
     }
     // If we're still here, no nonce-verification function was found.
     $severity = in_array($instance['content'], $this->errorForSuperGlobals) ? 0 : 'warning';
     $phpcsFile->addError('Processing form data without nonce verification.', $stackPtr, 'NoNonceVerification', array(), $severity);
 }
Example #2
Show file File: CharacterBeforePHPOpeningTagSniff.php Project: jpena88/mediawiki-dokku-deploy
0
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     // do nothing if opening tag is the first character
     if ($stackPtr == 0) {
         return;
     }
     $tokens = $phpcsFile->getTokens();
     $isNotFirstOpenTag = $phpcsFile->findPrevious(array(T_OPEN_TAG), $stackPtr - 1, null, false);
     // some other character beginning file
     if ($isNotFirstOpenTag === false) {
         $validShebang = false;
         // a shebang is allowed on first line only if
         // it is followed by a php open tag on next line
         if ($stackPtr == 1 && $tokens[1]['line'] == 2) {
             // the php tag is the second token and it is on second line
             // so the first token is on the first line
             // check if it is valid shebang
             // T_HASHBANG is a token used in HHVM >=3.5, <3.7 (T103119)
             if (in_array($tokens[0]['type'], array('T_INLINE_HTML', 'T_HASHBANG')) && substr($tokens[0]['content'], 0, 2) == '#!') {
                 $validShebang = true;
             }
         }
         if (!$validShebang) {
             $error = 'Extra character found before first <?';
             $phpcsFile->addError($error, $stackPtr, 'Found');
         }
     }
 }
Example #3
Show file File: FunctionOpeningBraceSpaceSniff.php Project: itliuchang/test
0
 /**
  * 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();
         }
     }
 }
Example #4
Show file File: PostStatementCommentSniff.php Project: felix021/myenv
0
 /**
  * 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();
     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);
     }
 }
Example #5
Show file File: ValidClassNameSniff.php Project: SebFav/ApplicationInternet2
0
 /**
  * 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();
     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);
     $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
     // Check for camel caps format.
     $valid = PHP_CodeSniffer::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);
     }
 }
Example #6
Show file File: AssignThisSniff.php Project: altesien/FinalProject
0
 /**
  * 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();
     // 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');
     }
 }
Example #7
Show file File: LineEndingsSniff.php Project: rrsc/freemed
0
 /**
  * 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);
     }
 }
Example #8
Show file File: Notifysend.php Project: itliuchang/test
0
 /**
  * Generate a partial report for a single processed file.
  *
  * Function should return TRUE if it printed or stored data about the file
  * and FALSE if it ignored the file. Returning TRUE indicates that the file and
  * its data should be counted in the grand totals.
  *
  * @param array                $report      Prepared report data.
  * @param PHP_CodeSniffer_File $phpcsFile   The file being reported on.
  * @param boolean              $showSources Show sources?
  * @param int                  $width       Maximum allowed line width.
  *
  * @return boolean
  */
 public function generateFileReport($report, PHP_CodeSniffer_File $phpcsFile, $showSources = false, $width = 80)
 {
     // We don't need to print anything, but we want this file counted
     // in the total number of checked files even if it has no errors.
     $this->_lastCheckedFile = $report['filename'];
     return true;
 }
Example #9
Show file File: ValidClassNameSniff.php Project: chrux/WordPress-Coding-Standards
0
 /**
  * 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();
     if (isset($tokens[$stackPtr]['scope_opener']) === false) {
         $error = 'Possible parse error: ';
         $error .= $tokens[$stackPtr]['content'];
         $error .= ' missing opening or closing brace';
         $phpcsFile->addWarning($error, $stackPtr);
         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);
     $name = trim($phpcsFile->getTokensAsString($nameStart, $nameEnd - $nameStart));
     // Check for camel caps format.
     $valid = PHP_CodeSniffer::isCamelCaps(str_replace('_', '', $name), true, true, false);
     if ($valid === false) {
         $type = ucfirst($tokens[$stackPtr]['content']);
         $error = "{$type} name \"{$name}\" is not in camel caps format";
         $phpcsFile->addError($error, $stackPtr);
     }
 }
Example #10
Show file File: LowercaseStyleDefinitionSniff.php Project: SebFav/ApplicationInternet2
0
 /**
  * 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();
     $start = $stackPtr + 1;
     $end = $tokens[$stackPtr]['bracket_closer'] - 1;
     $inStyle = null;
     for ($i = $start; $i <= $end; $i++) {
         // Skip nested definitions as they are checked individually.
         if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
             $i = $tokens[$i]['bracket_closer'];
             continue;
         }
         if ($tokens[$i]['code'] === T_STYLE) {
             $inStyle = $tokens[$i]['content'];
         }
         if ($tokens[$i]['code'] === T_SEMICOLON) {
             $inStyle = null;
         }
         if ($inStyle === 'progid') {
             // Special case for IE filters.
             continue;
         }
         if ($tokens[$i]['code'] === T_STYLE || $inStyle !== null && $tokens[$i]['code'] === T_STRING) {
             $expected = strtolower($tokens[$i]['content']);
             if ($expected !== $tokens[$i]['content']) {
                 $error = 'Style definitions must be lowercase; expected %s but found %s';
                 $data = array($expected, $tokens[$i]['content']);
                 $phpcsFile->addError($error, $i, 'FoundUpper', $data);
             }
         }
     }
     //end for
 }
Example #11
Show file File: DisallowMultipleStatementsSniff.php Project: aodkrisda/PHP-Digital-Format-Convert-Epub-Mobi-PDF
0
 /**
  * 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();
     $prev = $phpcsFile->findPrevious(T_SEMICOLON, $stackPtr - 1);
     if ($prev === false) {
         return;
     }
     // Ignore multiple statements in a FOR condition.
     if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
         foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) {
             if (isset($tokens[$bracket]['parenthesis_owner']) === false) {
                 // Probably a closure sitting inside a function call.
                 continue;
             }
             $owner = $tokens[$bracket]['parenthesis_owner'];
             if ($tokens[$owner]['code'] === T_FOR) {
                 return;
             }
         }
     }
     if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
         $error = 'Each PHP statement must be on a line by itself';
         $phpcsFile->addError($error, $stackPtr, 'SameLine');
         return;
     }
 }
Example #12
Show file File: DeprecatedFunctionsSniff.php Project: dariogs/moosh
0
 /**
  * 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();
     $ignore = array(T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_FUNCTION, T_CONST, T_USE, T_NS_SEPARATOR);
     $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
     if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
         // Not a call to a PHP function.
         return;
     }
     $function = strtolower($tokens[$stackPtr]['content']);
     $pattern = null;
     if ($this->patternMatch === true) {
         $count = 0;
         $pattern = preg_replace($this->forbiddenFunctionNames, $this->forbiddenFunctionNames, $function, 1, $count);
         if ($count === 0) {
             return;
         }
         // Remove the pattern delimiters and modifier.
         $pattern = substr($pattern, 1, -2);
     } else {
         if (in_array($function, $this->forbiddenFunctionNames) === false) {
             return;
         }
     }
     $this->addError($phpcsFile, $stackPtr, $function, $pattern);
 }
Example #13
Show file File: SuperglobalSniff.php Project: ffuenf/coding-standard
0
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $var = $phpcsFile->getTokens()[$stackPtr]['content'];
     if (in_array($var, $this->superglobals)) {
         $phpcsFile->addError('Direct use of %s Superglobal detected.', $stackPtr, 'SuperglobalUsage', array($var));
     }
 }
Example #14
Show file File: EchoedStringsSniff.php Project: lunnlew/Norma_Code
0
 /**
  * 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();
     $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(PHP_CodeSniffer_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();
         }
     }
 }
Example #15
Show file File: FunctionCallSignatureSniff.php Project: itliuchang/test
0
 /**
  * Processes single-line calls.
  *
  * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
  * @param int                  $stackPtr    The position of the current token
  *                                          in the stack passed in $tokens.
  * @param int                  $openBracket The position of the opening bracket
  *                                          in the stack passed in $tokens.
  * @param array                $tokens      The stack of tokens that make up
  *                                          the file.
  *
  * @return void
  */
 public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
 {
     // If the first argument is on a new line, this is a multi-line
     // function call, even if there is only one argument.
     $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $openBracket + 1, null, true);
     if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
         return true;
     }
     $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
     $end = $phpcsFile->findEndOfStatement($openBracket + 1);
     while ($tokens[$end]['code'] === T_COMMA) {
         // If the next bit of code is not on the same line, this is a
         // multi-line function call.
         $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $end + 1, $closeBracket, true);
         if ($next === false) {
             return false;
         }
         if ($tokens[$next]['line'] !== $tokens[$end]['line']) {
             return true;
         }
         $end = $phpcsFile->findEndOfStatement($next);
     }
     // We've reached the last argument, so see if the next content
     // (should be the close bracket) is also on the same line.
     $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $end + 1, $closeBracket, true);
     if ($next !== false && $tokens[$next]['line'] !== $tokens[$end]['line']) {
         return true;
     }
     return false;
 }
Example #16
Show file File: NewIniDirectivesSniff.php Project: dariogs/moosh
0
 /**
  * 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();
     $ignore = array(T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_FUNCTION, T_CONST);
     $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
     if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
         // Not a call to a PHP function.
         return;
     }
     $function = strtolower($tokens[$stackPtr]['content']);
     if ($function != 'ini_get' && $function != 'ini_set') {
         return;
     }
     $iniToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr, null);
     $filteredToken = str_replace(array('"', "'"), array("", ""), $tokens[$iniToken]['content']);
     if (in_array($filteredToken, array_keys($this->newIniDirectives)) === false) {
         return;
     }
     $error = '';
     foreach ($this->newIniDirectives[$filteredToken] as $version => $present) {
         if (!is_null(PHP_CodeSniffer::getConfigData('testVersion')) && version_compare(PHP_CodeSniffer::getConfigData('testVersion'), $version) <= 0) {
             if ($present === true) {
                 $error .= " not available before version " . $version;
             }
         }
     }
     if (strlen($error) > 0) {
         $error = "INI directive '" . $filteredToken . "' is" . $error;
         $phpcsFile->addWarning($error, $stackPtr);
     }
 }
Example #17
Show file File: ValidVariableNameSniff.php Project: xMyThoLoGyx/centremedicaletp3
0
 /**
  * Processes the variable found within a double quoted string.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param integer $stackPtr The position of the double quoted string.
  * @return void
  */
 protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS');
     if (preg_match_all('|[^\\\\]\\$([a-zA-Z0-9_]+)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
         foreach ($matches[1] as $varName) {
             // If it's a php reserved var, then its ok.
             if (in_array($varName, $phpReservedVars) === true) {
                 continue;
             }
             // 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) === '_') {
                 if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
                     $varName = substr($varName, 1);
                 }
             }
             if ($this->_isValidVar($varName) === false) {
                 $error = 'Variable "%s" is not in valid camel caps format';
                 $data = array($originalVarName);
                 $phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
             }
         }
     }
 }
Example #18
Show file File: CodeSniffer.php Project: Flesh192/magento
0
 /**
  * Process the sniffs for a single file.
  *
  * Does raw processing only. No interactive support or error checking.
  *
  * @param string $file         The file to process.
  * @param string $contents     The contents to parse. If NULL, the content
  *                             is taken from the file system.
  * @param array  $restrictions The sniff codes to restrict the
  *                             violations to.
  *
  * @return PHP_CodeSniffer_File
  * @see    processFile()
  */
 private function _processFile($file, $contents, $restrictions)
 {
     if (PHP_CODESNIFFER_VERBOSITY > 0) {
         $startTime = time();
         echo 'Processing ' . basename($file) . ' ';
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo PHP_EOL;
         }
     }
     $phpcsFile = new PHP_CodeSniffer_File($file, $this->_tokenListeners, $this->allowedFileExtensions, $this->ruleset, $restrictions, $this);
     $phpcsFile->start($contents);
     $phpcsFile->cleanUp();
     if (PHP_CODESNIFFER_VERBOSITY > 0) {
         $timeTaken = time() - $startTime;
         if ($timeTaken === 0) {
             echo 'DONE in < 1 second';
         } else {
             if ($timeTaken === 1) {
                 echo 'DONE in 1 second';
             } else {
                 echo "DONE in {$timeTaken} seconds";
             }
         }
         $errors = $phpcsFile->getErrorCount();
         $warnings = $phpcsFile->getWarningCount();
         echo " ({$errors} errors, {$warnings} warnings)" . PHP_EOL;
     }
     return $phpcsFile;
 }
Example #19
Show file File: ClosingTagSniff.php Project: felix021/myenv
0
 /**
  * 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;
 }
Example #20
Show file File: SpaceUnaryOperatorSniff.php Project: ipalindromi/Cuda-PHP-Code-Standards
0
 /**
  * Processes this test, when one of its tokens is encountered.
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // Check decrement / increment.
     if ($tokens[$stackPtr]['code'] === T_DEC || $tokens[$stackPtr]['code'] === T_INC) {
         $modifyLeft = substr($tokens[$stackPtr - 1]['content'], 0, 1) === '$' || $tokens[$stackPtr + 1]['content'] === ';';
         if ($modifyLeft === true && $tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
             $error = 'There must not be a single space before a unary operator statement';
             $phpcsFile->addError($error, $stackPtr, 'IncDecLeft');
             return;
         }
         if ($modifyLeft === false && !in_array(substr($tokens[$stackPtr + 1]['content'], 0, 1), array('$', ','))) {
             $error = 'A unary operator statement must not be followed by a single space';
             $phpcsFile->addError($error, $stackPtr, 'IncDecRight');
             return;
         }
     }
     // Check "!" operator.
     if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
         $error = 'A unary operator statement must not be followed by a space';
         $phpcsFile->addError($error, $stackPtr, 'BooleanNot');
         return;
     }
     // Find the last syntax item to determine if this is an unary operator.
     $lastSyntaxItem = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, $tokens[$stackPtr]['column'] * -1, true, null, true);
     $operatorSuffixAllowed = in_array($tokens[$lastSyntaxItem]['code'], array(T_LNUMBER, T_DNUMBER, T_CLOSE_PARENTHESIS, T_CLOSE_CURLY_BRACKET, T_CLOSE_SQUARE_BRACKET, T_VARIABLE, T_STRING));
     // Check plus / minus value assignments or comparisons.
     if ($tokens[$stackPtr]['code'] === T_MINUS || $tokens[$stackPtr]['code'] === T_PLUS) {
         if ($operatorSuffixAllowed === false && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
             $error = 'A unary operator statement must not be followed by a space';
             $phpcsFile->addError($error, $stackPtr);
         }
     }
 }
Example #21
Show file File: WarnSqlFunctionsSniff.php Project: POETGroup/poet-coding-standard
0
 /**
  * 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();
     $content = $tokens[$stackPtr]['content'];
     // Only check for functions using the $DB variable.
     if ($content !== '$DB') {
         return;
     }
     // Find the name of the function.
     $functionPtr = $phpcsFile->findNext(T_STRING, $stackPtr, null, false, null, true);
     if ($functionPtr === false) {
         return;
         // Failed to find the function call.
     }
     // Add a warning for functions that take raw sql.
     $function = $tokens[$functionPtr]['content'];
     $sqlPos = strrpos($function, '_sql');
     if ($sqlPos !== false && $sqlPos + 4 === strlen($function)) {
         $this->_addWarning($function, $phpcsFile, $stackPtr);
         return;
     }
     $menuPos = strrpos($function, '_sql_menu');
     if ($menuPos !== false && $menuPos + 9 === strlen($function)) {
         $this->_addWarning($function, $phpcsFile, $stackPtr);
         return;
     }
     if ($function === 'execute') {
         $this->_addWarning($function, $phpcsFile, $stackPtr);
         return;
     }
 }
Example #22
Show file File: ComparisonSniff.php Project: kingsj/core
0
 /**
  * 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);
 }
Example #23
Show file File: ArrayBracketSpacingSniff.php Project: master3395/CBPPlatform
0
 /**
  * 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(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // No bracket can have space before them.
     $prevType = $tokens[$stackPtr - 1]['code'];
     if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
         $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_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);
         $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeBracket', $data);
     }
     if ($tokens[$stackPtr]['type'] === 'T_OPEN_SQUARE_BRACKET') {
         // Open brackets can't have spaces on after them either.
         $nextType = $tokens[$stackPtr + 1]['code'];
         if (in_array($nextType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
             $nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_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);
             $phpcsFile->addError($error, $stackPtr, 'SpaceAfterBracket', $data);
         }
     }
 }
Example #24
Show file File: SuperGlobalInputUsageSniff.php Project: chrux/WordPress-Coding-Standards
0
 /**
  * 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();
     // Check for global input variable
     if (!in_array($tokens[$stackPtr]['content'], array('$_GET', '$_POST', '$_REQUEST'))) {
         return;
     }
     $varName = $tokens[$stackPtr]['content'];
     // If we're overriding a superglobal with an assignment, no need to test
     $semicolon_position = $phpcsFile->findNext(array(T_SEMICOLON), $stackPtr + 1, null, null, null, true);
     $assignment_position = $phpcsFile->findNext(array(T_EQUAL), $stackPtr + 1, null, null, null, true);
     if ($semicolon_position !== false && $assignment_position !== false && $assignment_position < $semicolon_position) {
         return;
     }
     // Check for whitelisting comment
     $currentLine = $tokens[$stackPtr]['line'];
     $nextPtr = $stackPtr;
     while (isset($tokens[$nextPtr + 1]['line']) && $tokens[$nextPtr + 1]['line'] == $currentLine) {
         $nextPtr++;
         // Do nothing, we just want the last token of the line
     }
     $is_whitelisted = $tokens[$nextPtr]['code'] === T_COMMENT && preg_match('#input var okay#i', $tokens[$nextPtr]['content']) > 0;
     if (!$is_whitelisted) {
         $phpcsFile->addWarning('Detected access of super global var %s, probably need manual inspection.', $stackPtr, null, array($varName));
     }
 }
Example #25
Show file File: EndFileNoNewlineSniff.php Project: SebFav/ApplicationInternet2
0
 /**
  * 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;
         }
     }
     // Skip to the end of the file.
     $tokens = $phpcsFile->getTokens();
     $stackPtr = $phpcsFile->numTokens - 1;
     if ($phpcsFile->tokenizerType === 'JS') {
         $stackPtr--;
     } else {
         if ($phpcsFile->tokenizerType === 'CSS') {
             $stackPtr -= 2;
         }
     }
     $eolCharLen = strlen($phpcsFile->eolChar);
     $lastChars = substr($tokens[$stackPtr]['content'], $eolCharLen * -1);
     if ($lastChars === $phpcsFile->eolChar) {
         $error = 'File must not end with a newline character';
         $phpcsFile->addError($error, $stackPtr, 'Found');
     }
 }
Example #26
Show file File: SwitchCaseControlStructureSniff.php Project: kingsj/core
0
 /**
  * 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 ($tokens[$stackPtr]['code'] === T_CASE && isset($tokens[$stackPtr]['scope_closer'])) {
         //			$internalCase = $phpcsFile->findNext(T_CASE, $stackPtr + 1, $tokens[$stackPtr]['scope_closer']);
         //			if ($internalCase !== false) {
         //				$comment = $phpcsFile->findNext(T_COMMENT, $stackPtr + 1, $internalCase - 1);
         //				if ($comment === false) {
         //					$phpcsFile->addError($this->getReqPrefix('REQ.PHP.2.5.12') . '"case" has not break and has not any comment', $stackPtr);
         //				}
         //			}
         $switch = $phpcsFile->findPrevious(T_SWITCH, $stackPtr - 1);
         if ($switch !== false) {
             $nextCase = $phpcsFile->findNext(array(T_CASE, T_DEFAULT), $stackPtr + 1, $tokens[$switch]['scope_closer']);
             if ($nextCase !== false) {
                 $prevBreak = $phpcsFile->findPrevious(T_BREAK, $nextCase - 1, $stackPtr);
                 if ($prevBreak !== false) {
                     $breakWS = $phpcsFile->findNext(T_WHITESPACE, $prevBreak + 1, $nextCase - 1);
                     if ($breakWS !== false) {
                         $str = $phpcsFile->getTokensAsString($breakWS, $nextCase - $breakWS - 1);
                         if (!preg_match("/^\n\n[ ]*\$/Ss", $str)) {
                             $breakWS = false;
                         }
                     }
                     if ($breakWS === false) {
                         $phpcsFile->addError($this->getReqPrefix('REQ.PHP.2.5.14') . '"case" must has empty line between current "case" and previous "break"', $stackPtr);
                     }
                 }
             }
         }
     } elseif ($tokens[$stackPtr]['code'] === T_DEFAULT) {
     }
 }
Example #27
Show file File: ColonSpacingSniff.php Project: master3395/CBPPlatform
0
 /**
  * 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();
     $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true);
     if ($tokens[$prev]['code'] !== T_STYLE) {
         // The colon is not part of a style definition.
         return;
     }
     if ($tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
         $error = 'There must be no space before a colon in a style definition';
         $phpcsFile->addError($error, $stackPtr, 'Before');
     }
     if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
         $error = 'Expected 1 space after colon in style definition; 0 found';
         $phpcsFile->addError($error, $stackPtr, 'NoneAfter');
     } 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);
                 $phpcsFile->addError($error, $stackPtr, 'After', $data);
             }
         } else {
             $error = 'Expected 1 space after colon in style definition; newline found';
             $phpcsFile->addError($error, $stackPtr, 'AfterNewline');
         }
     }
     //end if
 }
Example #28
Show file File: FileExtensionSniff.php Project: viniciusferreira/PHP_CodeSniffer
0
 /**
  * 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();
     // Make sure this is the first PHP open tag so we don't process
     // the same file twice.
     $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1);
     if ($prevOpenTag !== false) {
         return;
     }
     $fileName = $phpcsFile->getFileName();
     $extension = substr($fileName, strrpos($fileName, '.'));
     $nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), $stackPtr);
     if ($extension === '.php') {
         if ($nextClass !== false) {
             $error = '%s found in ".php" file; use ".inc" extension instead';
             $data = array(ucfirst($tokens[$nextClass]['content']));
             $phpcsFile->addError($error, $stackPtr, 'ClassFound', $data);
         }
     } else {
         if ($extension === '.inc') {
             if ($nextClass === false) {
                 $error = 'No interface or class found in ".inc" file; use ".php" extension instead';
                 $phpcsFile->addError($error, $stackPtr, 'NoClass');
             }
         }
     }
 }