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