/** * Run the coding style check * * @return int */ public function process() { $this->setDefaultValues(); \PHP_CodeSniffer_Reporting::startTiming(); $phpcs = new \PHP_CodeSniffer_CLI(); $phpcs->checkRequirements(); $values = $phpcs->getCommandLineValues(); foreach ($this->defaultValues as $k => $v) { if (empty($values[$k])) { $values[$k] = $v; } } return $phpcs->process($values); }
/** * 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; }
/** * Run the code sniffs over a single given file. * * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it * conforms with the standard. Returns the processed file object, or NULL * if no file was processed due to error. * * @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 * @throws PHP_CodeSniffer_Exception If the file could not be processed. * @see _processFile() */ public function processFile($file, $contents = null, $restrictions = array()) { if ($contents === null && file_exists($file) === false) { throw new PHP_CodeSniffer_Exception("Source file {$file} does not exist"); } $filePath = realpath($file); if ($filePath === false) { $filePath = $file; } // Before we go and spend time tokenizing this file, just check // to see if there is a tag up top to indicate that the whole // file should be ignored. It must be on one of the first two lines. $firstContent = $contents; if ($contents === null && is_readable($filePath) === true) { $handle = fopen($filePath, 'r'); if ($handle !== false) { $firstContent = fgets($handle); $firstContent .= fgets($handle); fclose($handle); if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) { // We are ignoring the whole file. if (PHP_CODESNIFFER_VERBOSITY > 0) { echo 'Ignoring ' . basename($filePath) . PHP_EOL; } return null; } } } //end if try { $phpcsFile = $this->_processFile($file, $contents, $restrictions); } catch (Exception $e) { $trace = $e->getTrace(); $filename = $trace[0]['args'][0]; if (is_object($filename) === true && get_class($filename) === 'PHP_CodeSniffer_File') { $filename = $filename->getFilename(); } else { if (is_numeric($filename) === true) { // See if we can find the PHP_CodeSniffer_File object. foreach ($trace as $data) { if (isset($data['args'][0]) === true && $data['args'][0] instanceof PHP_CodeSniffer_File === true) { $filename = $data['args'][0]->getFilename(); } } } else { if (is_string($filename) === false) { $filename = (string) $filename; } } } $error = 'An error occurred during processing; checking has been aborted. The error message was: ' . $e->getMessage(); $phpcsFile = new PHP_CodeSniffer_File($filename, $this->_tokenListeners, $this->allowedFileExtensions, $this->ruleset, $restrictions, $this); $phpcsFile->addError($error, null); } //end try $cliValues = $this->cli->getCommandLineValues(); if (PHP_CODESNIFFER_INTERACTIVE === false) { // Cache the report data for this file so we can unset it to save memory. $this->reporting->cacheFileReport($phpcsFile, $cliValues); return $phpcsFile; } /* 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 = $phpcsFile->getErrorCount() + $phpcsFile->getWarningCount(); if ($numErrors === 0) { continue; } $reportClass = $this->reporting->factory('full'); $reportData = $this->reporting->prepareFileReport($phpcsFile); $reportClass->generateFileReport($reportData, $cliValues['showSources'], $cliValues['reportWidth']); 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); break; 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. $this->populateTokenListeners(); $phpcsFile = $this->_processFile($file, $contents, $restrictions); break; } } //end while return $phpcsFile; }
<?php // Bootstrap the application. require __DIR__ . '/../src/bootstrap.php'; error_reporting(E_ALL | E_STRICT); // Set up the command line interface. $phpcs = new PHP_CodeSniffer_CLI(); $phpcs->checkRequirements(); // Add the Drupal standard. $values = $phpcs->getCommandLineValues(); $standard = dirname(__DIR__) . '/src/Drupal'; if (!isset($values['standard'])) { $values['standard'] = $standard; } else { $values['standard'] .= ',' . $standard; } // Process width PHP CodeSnifer. $numErrors = $phpcs->process($values); if ($numErrors === 0) { exit(0); } else { exit(1); }
/** * CodeSnifferShell::_process() * * @return int Exit */ protected function _process() { include_once 'PHP/CodeSniffer/CLI.php'; $phpcs = new PHP_CodeSniffer_CLI(); $phpcs->checkRequirements(); $cliValues = $phpcs->getCommandLineValues(); if ($this->params['fix']) { // Override some of the command line settings that might be used and stop us // gettting a diff file. $diffFile = TMP . 'phpcbf-fixed.diff'; $cliValues['generator'] = ''; $cliValues['explain'] = false; $cliValues['reports'] = ['diff' => $diffFile]; if (file_exists($diffFile) === true) { unlink($diffFile); } } $numErrors = $phpcs->process($cliValues); $exit = 0; if ($this->params['fix']) { if (file_exists($diffFile) === false) { // Nothing to fix. if ($numErrors === 0) { // And no errors reported. $exit = 0; } else { // Errors we can't fix. $exit = 2; } } else { $cmd = "cd / && patch -p0 -ui \"{$diffFile}\" && cd \"" . APP . "\""; $output = []; $retVal = null; exec($cmd, $output, $retVal); unlink($diffFile); if ($retVal === 0) { // Everything went well. $filesPatched = count($output); echo "Patched {$filesPatched} files\n"; $exit = 1; } else { print_r($output); echo "Returned: {$retVal}\n"; $exit = 3; } } } if ($numErrors !== 0) { $this->err('An error occured during processing.'); } return $exit; }