Example #1
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(File $phpcsFile, $stackPtr)
 {
     if ($this->phpPath === null) {
         $this->phpPath = Config::getExecutablePath('php');
         if ($this->phpPath === null) {
             // PHP_BINARY is available in PHP 5.4+.
             if (defined('PHP_BINARY') === true) {
                 $this->phpPath = PHP_BINARY;
             } else {
                 return;
             }
         }
     }
     $fileName = $phpcsFile->getFilename();
     $cmd = $this->phpPath . " -l \"{$fileName}\" 2>&1";
     $output = shell_exec($cmd);
     $matches = array();
     if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/', trim($output), $matches) === 1) {
         $error = trim($matches[1]);
         $line = (int) $matches[2];
         $phpcsFile->addErrorOnLine("PHP syntax error: {$error}", $line, 'PHPSyntax');
     }
     // Ignore the rest of the file.
     return $phpcsFile->numTokens + 1;
 }
Example #2
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
  * @throws PHP_CodeSniffer_Exception If jslint.js could not be run
  */
 public function process(File $phpcsFile, $stackPtr)
 {
     $lintPath = Config::getExecutablePath('gjslint');
     if ($lintPath === null) {
         return;
     }
     $fileName = $phpcsFile->getFilename();
     $cmd = "{$lintPath} --nosummary --notime --unix_mode \"{$fileName}\"";
     $msg = exec($cmd, $output, $retval);
     if (is_array($output) === false) {
         return;
     }
     foreach ($output as $finding) {
         $matches = array();
         $numMatches = preg_match('/^(.*):([0-9]+):\\(.*?([0-9]+)\\)(.*)$/', $finding, $matches);
         if ($numMatches === 0) {
             continue;
         }
         // Skip error codes we are ignoring.
         $code = $matches[3];
         if (in_array($code, $this->ignoreCodes) === true) {
             continue;
         }
         $line = (int) $matches[2];
         $error = trim($matches[4]);
         $message = 'gjslint says: (%s) %s';
         $data = array($code, $error);
         if (in_array($code, $this->errorCodes) === true) {
             $phpcsFile->addErrorOnLine($message, $line, 'ExternalToolError', $data);
         } else {
             $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool', $data);
         }
     }
     //end foreach
     // Ignore the rest of the file.
     return $phpcsFile->numTokens + 1;
 }
Example #3
0
 /**
  * Processes a single file, including checking and fixing.
  *
  * @param \PHP_CodeSniffer\Files\File $file The file to be processed.
  *
  * @return void
  */
 private function processFile($file)
 {
     if (PHP_CODESNIFFER_VERBOSITY > 0) {
         $startTime = microtime(true);
         echo 'Processing ' . basename($file->path) . ' ';
         if (PHP_CODESNIFFER_VERBOSITY > 1) {
             echo PHP_EOL;
         }
     }
     try {
         $file->process();
         if (PHP_CODESNIFFER_VERBOSITY > 0) {
             $timeTaken = (microtime(true) - $startTime) * 1000;
             if ($timeTaken < 1000) {
                 $timeTaken = round($timeTaken);
                 echo "DONE in {$timeTaken}ms";
             } else {
                 $timeTaken = round($timeTaken / 1000, 2);
                 echo "DONE in {$timeTaken} secs";
             }
             if (PHP_CODESNIFFER_CBF === true) {
                 $errors = $file->getFixableCount();
                 echo " ({$errors} fixable violations)" . PHP_EOL;
             } else {
                 $errors = $file->getErrorCount();
                 $warnings = $file->getWarningCount();
                 echo " ({$errors} errors, {$warnings} warnings)" . PHP_EOL;
             }
         }
     } catch (\Exception $e) {
         $error = 'An error occurred during processing; checking has been aborted. The error message was: ' . $e->getMessage();
         $file->addErrorOnLine($error, 1, 'Internal.Exception');
     }
     //end try
     $this->reporter->cacheFileReport($file, $this->config);
     // Clean up the file to save (a lot of) memory.
     $file->cleanUp();
     if ($this->config->interactive === true) {
         /*
             Running interactively.
             Print the error report for the current file and then wait for user input.
         */
         // Get current violations and then clear the list to make sure
         // we only print violations for a single file each time.
         $numErrors = null;
         while ($numErrors !== 0) {
             $numErrors = $file->getErrorCount() + $file->getWarningCount();
             if ($numErrors === 0) {
                 continue;
             }
             $this->reporter->printReport('full');
             echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
             $input = fgets(STDIN);
             $input = trim($input);
             switch ($input) {
                 case 's':
                     break 2;
                 case 'q':
                     exit(0);
                 default:
                     // Repopulate the sniffs because some of them save their state
                     // and only clear it when the file changes, but we are rechecking
                     // the same file.
                     $file->ruleset->populateTokenListeners();
                     $file->reloadContent();
                     $file->process();
                     $this->reporter->cacheFileReport($file, $this->config);
                     break;
             }
         }
         //end while
     }
     //end if
 }