/** * 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. * * @return PHP_CodeSniffer_File * @see processFile() */ private function _processFile($file, $contents) { $stdin = false; $cliValues = $this->cli->getCommandLineValues(); if (empty($cliValues['files']) === true) { $stdin = true; } if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true && $stdin === false) { $startTime = microtime(true); echo 'Processing ' . basename($file) . ' '; if (PHP_CODESNIFFER_VERBOSITY > 1) { echo PHP_EOL; } } $phpcsFile = new PHP_CodeSniffer_File($file, $this->_tokenListeners, $this->ruleset, $this); $phpcsFile->start($contents); if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true && $stdin === false) { $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 = $phpcsFile->getFixableCount(); echo " ({$errors} fixable violations)" . PHP_EOL; } else { $errors = $phpcsFile->getErrorCount(); $warnings = $phpcsFile->getWarningCount(); echo " ({$errors} errors, {$warnings} warnings)" . PHP_EOL; } } return $phpcsFile; }
/** * 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; }
/** * Pre-process and package violations for all files. * * Used by error reports to get a packaged list of all errors in each file. * * @param PHP_CodeSniffer_File $phpcsFile The file that has been processed. * * @return array */ public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile) { $report = array('filename' => $phpcsFile->getFilename(), 'errors' => $phpcsFile->getErrorCount(), 'warnings' => $phpcsFile->getWarningCount(), 'fixable' => $phpcsFile->getFixableCount(), 'messages' => array()); if ($report['errors'] === 0 && $report['warnings'] === 0) { // Prefect score! return $report; } $errors = array(); // Merge errors and warnings. foreach ($phpcsFile->getErrors() as $line => $lineErrors) { if (is_array($lineErrors) === false) { continue; } foreach ($lineErrors as $column => $colErrors) { $newErrors = array(); foreach ($colErrors as $data) { $newErrors[] = array('message' => $data['message'], 'source' => $data['source'], 'severity' => $data['severity'], 'fixable' => $data['fixable'], 'type' => 'ERROR'); } //end foreach $errors[$line][$column] = $newErrors; } //end foreach ksort($errors[$line]); } //end foreach foreach ($phpcsFile->getWarnings() as $line => $lineWarnings) { if (is_array($lineWarnings) === false) { continue; } foreach ($lineWarnings as $column => $colWarnings) { $newWarnings = array(); foreach ($colWarnings as $data) { $newWarnings[] = array('message' => $data['message'], 'source' => $data['source'], 'severity' => $data['severity'], 'fixable' => $data['fixable'], 'type' => 'WARNING'); } //end foreach if (isset($errors[$line]) === false) { $errors[$line] = array(); } if (isset($errors[$line][$column]) === true) { $errors[$line][$column] = array_merge($newWarnings, $errors[$line][$column]); } else { $errors[$line][$column] = $newWarnings; } } //end foreach ksort($errors[$line]); } //end foreach ksort($errors); $report['messages'] = $errors; return $report; }
/** * Run the code sniffs over a signle given file. * * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it * conforms with the standard. * * @param string $file The file to process. * * @return void * @throws PHP_CodeSniffer_Exception If the file could not be processed. */ private function _processFile($file) { $file = realpath($file); if (file_exists($file) === false) { throw new PHP_CodeSniffer_Exception("Source file {$file} does not exist"); } 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->_listeners); $this->_files[] = $phpcsFile; $phpcsFile->start(); 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; } }
/** * Get simple result of processed file * * @param \PHP_CodeSniffer_File $phpcsFile * @return array */ protected function getFileResult($phpcsFile) { $report = array(); if ($phpcsFile && ($phpcsFile->getErrorCount() || $phpcsFile->getWarningCount())) { $report = array('errors' => $phpcsFile->getErrors(), 'warnings' => $phpcsFile->getWarnings()); } return $report; }
/** * 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. * * @return PHP_CodeSniffer_File * @see processFile() */ private function _processFile($file, $contents) { if (PHP_CODESNIFFER_VERBOSITY > 0) { $startTime = microtime(true); echo 'Processing ' . basename($file) . ' '; if (PHP_CODESNIFFER_VERBOSITY > 1) { echo PHP_EOL; } } $phpcsFile = new PHP_CodeSniffer_File($file, $this->_tokenListeners, $this->allowedFileExtensions, $this->ruleset, $this); $phpcsFile->start($contents); 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"; } $errors = $phpcsFile->getErrorCount(); $warnings = $phpcsFile->getWarningCount(); echo " ({$errors} errors, {$warnings} warnings)" . PHP_EOL; } return $phpcsFile; }