Chris@4: Chris@4: * @author Greg Sherwood Chris@4: * @copyright 2012-2014 Christian Weiske Chris@4: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@4: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@4: */ Chris@4: Chris@4: namespace PHP_CodeSniffer\Reports; Chris@4: Chris@4: use PHP_CodeSniffer\Config; Chris@4: use PHP_CodeSniffer\Files\File; Chris@4: Chris@4: class Notifysend implements Report Chris@4: { Chris@4: Chris@4: /** Chris@4: * Notification timeout in milliseconds. Chris@4: * Chris@4: * @var integer Chris@4: */ Chris@4: protected $timeout = 3000; Chris@4: Chris@4: /** Chris@4: * Path to notify-send command. Chris@4: * Chris@4: * @var string Chris@4: */ Chris@4: protected $path = 'notify-send'; Chris@4: Chris@4: /** Chris@4: * Show "ok, all fine" messages. Chris@4: * Chris@4: * @var boolean Chris@4: */ Chris@4: protected $showOk = true; Chris@4: Chris@4: /** Chris@4: * Version of installed notify-send executable. Chris@4: * Chris@4: * @var string Chris@4: */ Chris@4: protected $version = null; Chris@4: Chris@4: Chris@4: /** Chris@4: * Load configuration data. Chris@4: */ Chris@4: public function __construct() Chris@4: { Chris@4: $path = Config::getExecutablePath('notifysend'); Chris@4: if ($path !== null) { Chris@4: $this->path = escapeshellcmd($path); Chris@4: } Chris@4: Chris@4: $timeout = Config::getConfigData('notifysend_timeout'); Chris@4: if ($timeout !== null) { Chris@4: $this->timeout = (int) $timeout; Chris@4: } Chris@4: Chris@4: $showOk = Config::getConfigData('notifysend_showok'); Chris@4: if ($showOk !== null) { Chris@4: $this->showOk = (boolean) $showOk; Chris@4: } Chris@4: Chris@4: $this->version = str_replace( Chris@4: 'notify-send ', Chris@4: '', Chris@4: exec($this->path.' --version') Chris@4: ); Chris@4: Chris@4: }//end __construct() Chris@4: Chris@4: Chris@4: /** Chris@4: * Generate a partial report for a single processed file. Chris@4: * Chris@4: * Function should return TRUE if it printed or stored data about the file Chris@4: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@4: * its data should be counted in the grand totals. Chris@4: * Chris@4: * @param array $report Prepared report data. Chris@4: * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. Chris@4: * @param bool $showSources Show sources? Chris@4: * @param int $width Maximum allowed line width. Chris@4: * Chris@4: * @return bool Chris@4: */ Chris@4: public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) Chris@4: { Chris@4: echo $report['filename'].PHP_EOL; Chris@4: Chris@4: // We want this file counted in the total number Chris@4: // of checked files even if it has no errors. Chris@4: return true; Chris@4: Chris@4: }//end generateFileReport() Chris@4: Chris@4: Chris@4: /** Chris@4: * Generates a summary of errors and warnings for each file processed. Chris@4: * Chris@4: * @param string $cachedData Any partial report data that was returned from Chris@4: * generateFileReport during the run. Chris@4: * @param int $totalFiles Total number of files processed during the run. Chris@4: * @param int $totalErrors Total number of errors found during the run. Chris@4: * @param int $totalWarnings Total number of warnings found during the run. Chris@4: * @param int $totalFixable Total number of problems that can be fixed. Chris@4: * @param bool $showSources Show sources? Chris@4: * @param int $width Maximum allowed line width. Chris@4: * @param bool $interactive Are we running in interactive mode? Chris@4: * @param bool $toScreen Is the report being printed to screen? Chris@4: * Chris@4: * @return void Chris@4: */ Chris@4: public function generate( Chris@4: $cachedData, Chris@4: $totalFiles, Chris@4: $totalErrors, Chris@4: $totalWarnings, Chris@4: $totalFixable, Chris@4: $showSources=false, Chris@4: $width=80, Chris@4: $interactive=false, Chris@4: $toScreen=true Chris@4: ) { Chris@4: $checkedFiles = explode(PHP_EOL, trim($cachedData)); Chris@4: Chris@4: $msg = $this->generateMessage($checkedFiles, $totalErrors, $totalWarnings); Chris@4: if ($msg === null) { Chris@4: if ($this->showOk === true) { Chris@4: $this->notifyAllFine(); Chris@4: } Chris@4: } else { Chris@4: $this->notifyErrors($msg); Chris@4: } Chris@4: Chris@4: }//end generate() Chris@4: Chris@4: Chris@4: /** Chris@4: * Generate the error message to show to the user. Chris@4: * Chris@4: * @param string[] $checkedFiles The files checked during the run. Chris@4: * @param int $totalErrors Total number of errors found during the run. Chris@4: * @param int $totalWarnings Total number of warnings found during the run. Chris@4: * Chris@4: * @return string Error message or NULL if no error/warning found. Chris@4: */ Chris@4: protected function generateMessage($checkedFiles, $totalErrors, $totalWarnings) Chris@4: { Chris@4: if ($totalErrors === 0 && $totalWarnings === 0) { Chris@4: // Nothing to print. Chris@4: return null; Chris@4: } Chris@4: Chris@4: $totalFiles = count($checkedFiles); Chris@4: Chris@4: $msg = ''; Chris@4: if ($totalFiles > 1) { Chris@4: $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL; Chris@4: } else { Chris@4: $msg .= $checkedFiles[0].PHP_EOL; Chris@4: } Chris@4: Chris@4: if ($totalWarnings > 0) { Chris@4: $msg .= $totalWarnings.' warnings'.PHP_EOL; Chris@4: } Chris@4: Chris@4: if ($totalErrors > 0) { Chris@4: $msg .= $totalErrors.' errors'.PHP_EOL; Chris@4: } Chris@4: Chris@4: return $msg; Chris@4: Chris@4: }//end generateMessage() Chris@4: Chris@4: Chris@4: /** Chris@4: * Tell the user that all is fine and no error/warning has been found. Chris@4: * Chris@4: * @return void Chris@4: */ Chris@4: protected function notifyAllFine() Chris@4: { Chris@4: $cmd = $this->getBasicCommand(); Chris@4: $cmd .= ' -i info'; Chris@4: $cmd .= ' "PHP CodeSniffer: Ok"'; Chris@4: $cmd .= ' "All fine"'; Chris@4: exec($cmd); Chris@4: Chris@4: }//end notifyAllFine() Chris@4: Chris@4: Chris@4: /** Chris@4: * Tell the user that errors/warnings have been found. Chris@4: * Chris@4: * @param string $msg Message to display. Chris@4: * Chris@4: * @return void Chris@4: */ Chris@4: protected function notifyErrors($msg) Chris@4: { Chris@4: $cmd = $this->getBasicCommand(); Chris@4: $cmd .= ' -i error'; Chris@4: $cmd .= ' "PHP CodeSniffer: Error"'; Chris@4: $cmd .= ' '.escapeshellarg(trim($msg)); Chris@4: exec($cmd); Chris@4: Chris@4: }//end notifyErrors() Chris@4: Chris@4: Chris@4: /** Chris@4: * Generate and return the basic notify-send command string to execute. Chris@4: * Chris@4: * @return string Shell command with common parameters. Chris@4: */ Chris@4: protected function getBasicCommand() Chris@4: { Chris@4: $cmd = $this->path; Chris@4: $cmd .= ' --category dev.validate'; Chris@4: $cmd .= ' -h int:transient:1'; Chris@4: $cmd .= ' -t '.(int) $this->timeout; Chris@4: if (version_compare($this->version, '0.7.3', '>=') === true) { Chris@4: $cmd .= ' -a phpcs'; Chris@4: } Chris@4: Chris@4: return $cmd; Chris@4: Chris@4: }//end getBasicCommand() Chris@4: Chris@4: Chris@4: }//end class