Chris@0: Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2012-2014 Christian Weiske Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Notify-send report for PHP_CodeSniffer. Chris@0: * Chris@0: * Supported configuration parameters: Chris@0: * - notifysend_path - Full path to notify-send cli command Chris@0: * - notifysend_timeout - Timeout in milliseconds Chris@0: * - notifysend_showok - Show "ok, all fine" messages (0/1) Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Christian Weiske Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2012-2014 Christian Weiske Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @version Release: @package_version@ Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: class PHP_CodeSniffer_Reports_Notifysend implements PHP_CodeSniffer_Report Chris@0: { Chris@0: Chris@0: /** Chris@0: * Notification timeout in milliseconds. Chris@0: * Chris@0: * @var integer Chris@0: */ Chris@0: protected $timeout = 3000; Chris@0: Chris@0: /** Chris@0: * Path to notify-send command. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $path = 'notify-send'; Chris@0: Chris@0: /** Chris@0: * Show "ok, all fine" messages. Chris@0: * Chris@0: * @var boolean Chris@0: */ Chris@0: protected $showOk = true; Chris@0: Chris@0: /** Chris@0: * Version of installed notify-send executable. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $version = null; Chris@0: Chris@0: /** Chris@0: * A record of the last file checked. Chris@0: * Chris@0: * This is used in case we only checked one file and need to print Chris@0: * the name/path of the file. We wont have access to the checked file list Chris@0: * after the run has been completed. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: private $_lastCheckedFile = ''; Chris@0: Chris@0: Chris@0: /** Chris@0: * Load configuration data. Chris@0: */ Chris@0: public function __construct() Chris@0: { Chris@0: $path = PHP_CodeSniffer::getConfigData('notifysend_path'); Chris@0: if ($path !== null) { Chris@0: $this->path = escapeshellcmd($path); Chris@0: } Chris@0: Chris@0: $timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout'); Chris@0: if ($timeout !== null) { Chris@0: $this->timeout = (int) $timeout; Chris@0: } Chris@0: Chris@0: $showOk = PHP_CodeSniffer::getConfigData('notifysend_showok'); Chris@0: if ($showOk !== null) { Chris@0: $this->showOk = (boolean) $showOk; Chris@0: } Chris@0: Chris@0: $this->version = str_replace( Chris@0: 'notify-send ', Chris@0: '', Chris@0: exec($this->path.' --version') Chris@0: ); Chris@0: Chris@0: }//end __construct() Chris@0: Chris@0: Chris@0: /** Chris@0: * Generate a partial report for a single processed file. Chris@0: * Chris@0: * Function should return TRUE if it printed or stored data about the file Chris@0: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@0: * its data should be counted in the grand totals. Chris@0: * Chris@0: * @param array $report Prepared report data. Chris@0: * @param PHP_CodeSniffer_File $phpcsFile The file being reported on. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * Chris@0: * @return boolean Chris@0: */ Chris@0: public function generateFileReport( Chris@0: $report, Chris@0: PHP_CodeSniffer_File $phpcsFile, Chris@0: $showSources=false, Chris@0: $width=80 Chris@0: ) { Chris@0: // We don't need to print anything, but we want this file counted Chris@0: // in the total number of checked files even if it has no errors. Chris@0: $this->_lastCheckedFile = $report['filename']; Chris@0: return true; Chris@0: Chris@0: }//end generateFileReport() Chris@0: Chris@0: Chris@0: /** Chris@0: * Generates a summary of errors and warnings for each file processed. Chris@0: * Chris@0: * @param string $cachedData Any partial report data that was returned from Chris@0: * generateFileReport during the run. Chris@0: * @param int $totalFiles Total number of files processed during the run. Chris@0: * @param int $totalErrors Total number of errors found during the run. Chris@0: * @param int $totalWarnings Total number of warnings found during the run. Chris@0: * @param int $totalFixable Total number of problems that can be fixed. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * @param boolean $toScreen Is the report being printed to screen? Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function generate( Chris@0: $cachedData, Chris@0: $totalFiles, Chris@0: $totalErrors, Chris@0: $totalWarnings, Chris@0: $totalFixable, Chris@0: $showSources=false, Chris@0: $width=80, Chris@0: $toScreen=true Chris@0: ) { Chris@0: $msg = $this->generateMessage($totalFiles, $totalErrors, $totalWarnings); Chris@0: if ($msg === null) { Chris@0: if ($this->showOk === true) { Chris@0: $this->notifyAllFine(); Chris@0: } Chris@0: } else { Chris@0: $this->notifyErrors($msg); Chris@0: } Chris@0: Chris@0: }//end generate() Chris@0: Chris@0: Chris@0: /** Chris@0: * Generate the error message to show to the user. Chris@0: * Chris@0: * @param int $totalFiles Total number of files processed during the run. Chris@0: * @param int $totalErrors Total number of errors found during the run. Chris@0: * @param int $totalWarnings Total number of warnings found during the run. Chris@0: * Chris@0: * @return string Error message or NULL if no error/warning found. Chris@0: */ Chris@0: protected function generateMessage($totalFiles, $totalErrors, $totalWarnings) Chris@0: { Chris@0: if ($totalErrors === 0 && $totalWarnings === 0) { Chris@0: // Nothing to print. Chris@0: return null; Chris@0: } Chris@0: Chris@0: $msg = ''; Chris@0: if ($totalFiles > 1) { Chris@0: $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL; Chris@0: } else { Chris@0: $msg .= $this->_lastCheckedFile.PHP_EOL; Chris@0: } Chris@0: Chris@0: if ($totalWarnings > 0) { Chris@0: $msg .= $totalWarnings.' warnings'.PHP_EOL; Chris@0: } Chris@0: Chris@0: if ($totalErrors > 0) { Chris@0: $msg .= $totalErrors.' errors'.PHP_EOL; Chris@0: } Chris@0: Chris@0: return $msg; Chris@0: Chris@0: }//end generateMessage() Chris@0: Chris@0: Chris@0: /** Chris@0: * Tell the user that all is fine and no error/warning has been found. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: protected function notifyAllFine() Chris@0: { Chris@0: $cmd = $this->getBasicCommand(); Chris@0: $cmd .= ' -i info'; Chris@0: $cmd .= ' "PHP CodeSniffer: Ok"'; Chris@0: $cmd .= ' "All fine"'; Chris@0: exec($cmd); Chris@0: Chris@0: }//end notifyAllFine() Chris@0: Chris@0: Chris@0: /** Chris@0: * Tell the user that errors/warnings have been found. Chris@0: * Chris@0: * @param string $msg Message to display. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: protected function notifyErrors($msg) Chris@0: { Chris@0: $cmd = $this->getBasicCommand(); Chris@0: $cmd .= ' -i error'; Chris@0: $cmd .= ' "PHP CodeSniffer: Error"'; Chris@0: $cmd .= ' '.escapeshellarg(trim($msg)); Chris@0: exec($cmd); Chris@0: Chris@0: }//end notifyErrors() Chris@0: Chris@0: Chris@0: /** Chris@0: * Generate and return the basic notify-send command string to execute. Chris@0: * Chris@0: * @return string Shell command with common parameters. Chris@0: */ Chris@0: protected function getBasicCommand() Chris@0: { Chris@0: $cmd = $this->path; Chris@0: $cmd .= ' --category dev.validate'; Chris@0: $cmd .= ' -h int:transient:1'; Chris@0: $cmd .= ' -t '.(int) $this->timeout; Chris@0: if (version_compare($this->version, '0.7.3', '>=') === true) { Chris@0: $cmd .= ' -a phpcs'; Chris@0: } Chris@0: Chris@0: return $cmd; Chris@0: Chris@0: }//end getBasicCommand() Chris@0: Chris@0: Chris@0: }//end class