Returns the number of fixable errors/warnings raised.
public getFixableCount ( ) : integer | ||
return | integer |
/** * 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) { $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); $errors = $phpcsFile->getFixableCount(); if ($errors !== 0) { if (empty($cliValues['files']) === false) { ob_end_clean(); $errors = $phpcsFile->getFixableCount(); $startTime = microtime(true); echo "\t=> Fixing file: {$errors}/{$errors} violations remaining"; } $fixed = $phpcsFile->fixer->fixFile(); } if (empty($cliValues['files']) === true) { // Replacing STDIN, so output current file to STDOUT // even if nothing was fixed. Exit here because we // can't process any more than 1 file in this setup. echo $phpcsFile->fixer->getContents(); ob_end_flush(); exit(1); } if ($errors === 0) { return false; } if ($fixed === false) { echo 'ERROR'; } else { echo 'DONE'; } $timeTaken = (microtime(true) - $startTime) * 1000; if ($timeTaken < 1000) { $timeTaken = round($timeTaken); echo " in {$timeTaken}ms" . PHP_EOL; } else { $timeTaken = round($timeTaken / 1000, 2); echo " in {$timeTaken} secs" . PHP_EOL; } if ($fixed === true) { $newFilename = $report['filename'] . $cliValues['phpcbf-suffix']; $newContent = $phpcsFile->fixer->getContents(); file_put_contents($newFilename, $newContent); if ($newFilename === $report['filename']) { echo "\t=> File was overwritten" . PHP_EOL; } else { echo "\t=> Fixed file written to " . basename($newFilename) . PHP_EOL; } } ob_start(); return $fixed; }
/** * Attempt to fix the file by processing it until no fixes are made. * * @return boolean */ public function fixFile() { $fixable = $this->_currentFile->getFixableCount(); if ($fixable === 0) { // Nothing to fix. return false; } $this->enabled = true; $loops = 0; while ($loops < 50) { ob_start(); if ($loops === 0) { // First time through, don't reparse the file, saving time. $contents = null; } else { // Only needed once file content has changed. $contents = $this->getContents(); /* @ob_end_clean(); $debugContent = str_replace("\n", "\033[30;1m\\n\n\033[0m", $contents); $debugContent = str_replace("\t", "\033[30;1m»\t\033[0m", $debugContent); $debugContent = str_replace(' ', "\033[30;1m·\033[0m", $debugContent); echo $debugContent; */ } $this->_currentFile->refreshTokenListeners(); $this->_currentFile->start($contents); ob_end_clean(); /* Possibly useful as a fail-safe, but may mask problems with the actual fixes being performed. $newContents = $this->getContents(); if ($newContents === $contents) { break; } */ $loops++; if ($this->_numFixes === 0) { // Nothing left to do. break; } else { if (PHP_CODESNIFFER_VERBOSITY > 1) { echo "\tFixed {$this->_numFixes} violations, starting over" . PHP_EOL; } } } //end while $this->enabled = false; if ($this->_numFixes > 0) { if (PHP_CODESNIFFER_VERBOSITY > 1) { @ob_end_clean(); echo "\tReached maximum number of loops with {$this->_numFixes} violations left unfixed" . PHP_EOL; ob_start(); } return false; } return true; }
/** * 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) { $errors = $phpcsFile->getFixableCount(); if ($errors === 0) { return false; } if (PHP_CODESNIFFER_VERBOSITY > 1) { ob_end_clean(); echo "\t*** START FILE FIXING ***" . PHP_EOL; } if (PHP_CODESNIFFER_CBF === true) { ob_end_clean(); $startTime = microtime(true); echo "\t=> Fixing file: {$errors}/{$errors} violations remaining"; } $fixed = $phpcsFile->fixer->fixFile(); if (PHP_CODESNIFFER_CBF === true) { if ($fixed === false) { echo "[31mERROR[0m"; } else { echo "[32mDONE[0m"; } $timeTaken = (microtime(true) - $startTime) * 1000; if ($timeTaken < 1000) { $timeTaken = round($timeTaken); echo " in {$timeTaken}ms" . PHP_EOL; } else { $timeTaken = round($timeTaken / 1000, 2); echo " in {$timeTaken} secs" . PHP_EOL; } ob_start(); } if (PHP_CODESNIFFER_VERBOSITY > 1) { echo "\t*** END FILE FIXING ***" . PHP_EOL; ob_start(); } if ($fixed === false) { return false; } if (PHP_CODESNIFFER_CBF === true) { // Diff without colours. $diff = $phpcsFile->fixer->generateDiff(null, false); } else { $diff = $phpcsFile->fixer->generateDiff(); } if ($diff === '') { // Nothing to print. return false; } echo $diff . PHP_EOL; return true; }
/** * 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; }
/** * Attempt to fix the file by processing it until no fixes are made. * * @return boolean */ public function fixFile() { $fixable = $this->_currentFile->getFixableCount(); if ($fixable === 0) { // Nothing to fix. return false; } $stdin = false; $cliValues = $this->_currentFile->phpcs->cli->getCommandLineValues(); if (empty($cliValues['files']) === true) { $stdin = true; } $this->enabled = true; $this->_loops = 0; while ($this->_loops < 50) { ob_start(); // Only needed once file content has changed. $contents = $this->getContents(); if (PHP_CODESNIFFER_VERBOSITY > 2) { @ob_end_clean(); echo '---START FILE CONTENT---' . PHP_EOL; $lines = explode($this->_currentFile->eolChar, $contents); $max = strlen(count($lines)); foreach ($lines as $lineNum => $line) { $lineNum++; echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT) . '|' . $line . PHP_EOL; } echo '--- END FILE CONTENT ---' . PHP_EOL; ob_start(); } $this->_currentFile->refreshTokenListeners(); $this->_currentFile->start($contents); ob_end_clean(); $this->_loops++; if (PHP_CODESNIFFER_CBF === true && $stdin === false) { echo "\r" . str_repeat(' ', 80) . "\r"; echo "\t=> Fixing file: {$this->_numFixes}/{$fixable} violations remaining [made {$this->_loops} pass"; if ($this->_loops > 1) { echo 'es'; } echo ']... '; } if ($this->_numFixes === 0) { // Nothing left to do. break; } else { if (PHP_CODESNIFFER_VERBOSITY > 1) { echo "\t* fixed {$this->_numFixes} violations, starting loop " . ($this->_loops + 1) . ' *' . PHP_EOL; } } } //end while $this->enabled = false; if ($this->_numFixes > 0) { if (PHP_CODESNIFFER_VERBOSITY > 1) { @ob_end_clean(); echo "\t*** Reached maximum number of loops with {$this->_numFixes} violations left unfixed ***" . PHP_EOL; ob_start(); } return false; } return true; }
/** * Attempt to fix the file by processing it until no fixes are made. * * @return boolean */ public function fixFile() { $fixable = $this->_currentFile->getFixableCount(); if ($fixable === 0) { // Nothing to fix. return false; } $this->enabled = true; $loops = 0; while ($loops < 50) { ob_start(); // Only needed once file content has changed. $contents = $this->getContents(); /* Useful for debugging fixed contents. @ob_end_clean(); $debugContent = PHP_CodeSniffer::prepareForOutput($contents); echo $debugContent; ob_start(); */ $this->_currentFile->refreshTokenListeners(); $this->_currentFile->start($contents); ob_end_clean(); /* Possibly useful as a fail-safe, but may mask problems with the actual fixes being performed. $newContents = $this->getContents(); if ($newContents === $contents) { break; } */ $loops++; if (PHP_CODESNIFFER_CBF === true) { echo "\r" . str_repeat(' ', 80) . "\r"; echo "\t=> Fixing file: {$this->_numFixes}/{$fixable} violations remaining [made {$loops} pass"; if ($loops > 1) { echo 'es'; } echo ']... '; } if ($this->_numFixes === 0) { // Nothing left to do. break; } else { if (PHP_CODESNIFFER_VERBOSITY > 1) { echo "\t* fixed {$this->_numFixes} violations, starting loop " . ($loops + 1) . ' *' . PHP_EOL; } } } //end while $this->enabled = false; if ($this->_numFixes > 0) { if (PHP_CODESNIFFER_VERBOSITY > 1) { @ob_end_clean(); echo "\t*** Reached maximum number of loops with {$this->_numFixes} violations left unfixed ***" . PHP_EOL; ob_start(); } return false; } return true; }
/** * 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; }