3
* Notify-send report for PHP_CodeSniffer.
8
* @package PHP_CodeSniffer
9
* @author Christian Weiske <christian.weiske@netresearch.de>
10
* @author Greg Sherwood <gsherwood@squiz.net>
11
* @copyright 2012-2014 Christian Weiske
12
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
13
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Notify-send report for PHP_CodeSniffer.
20
* Supported configuration parameters:
21
* - notifysend_path - Full path to notify-send cli command
22
* - notifysend_timeout - Timeout in milliseconds
23
* - notifysend_showok - Show "ok, all fine" messages (0/1)
26
* @package PHP_CodeSniffer
27
* @author Christian Weiske <christian.weiske@netresearch.de>
28
* @author Greg Sherwood <gsherwood@squiz.net>
29
* @copyright 2012-2014 Christian Weiske
30
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
31
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
32
* @version Release: 2.3.3
33
* @link http://pear.php.net/package/PHP_CodeSniffer
35
class PHP_CodeSniffer_Reports_Notifysend implements PHP_CodeSniffer_Report
39
* Notification timeout in milliseconds.
43
protected $timeout = 3000;
46
* Path to notify-send command.
50
protected $path = 'notify-send';
53
* Show "ok, all fine" messages.
57
protected $showOk = true;
60
* Version of installed notify-send executable.
64
protected $version = null;
67
* A record of the last file checked.
69
* This is used in case we only checked one file and need to print
70
* the name/path of the file. We wont have access to the checked file list
71
* after the run has been completed.
75
private $_lastCheckedFile = '';
79
* Load configuration data.
81
public function __construct()
83
$path = PHP_CodeSniffer::getConfigData('notifysend_path');
88
$timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
89
if ($timeout !== null) {
90
$this->timeout = (int) $timeout;
93
$showOk = PHP_CodeSniffer::getConfigData('notifysend_showok');
94
if ($showOk !== null) {
95
$this->showOk = (boolean) $showOk;
98
$this->version = str_replace(
101
exec($this->path.' --version')
108
* Generate a partial report for a single processed file.
110
* Function should return TRUE if it printed or stored data about the file
111
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
112
* its data should be counted in the grand totals.
114
* @param array $report Prepared report data.
115
* @param PHP_CodeSniffer_File $phpcsFile The file being reported on.
116
* @param boolean $showSources Show sources?
117
* @param int $width Maximum allowed line width.
121
public function generateFileReport(
123
PHP_CodeSniffer_File $phpcsFile,
127
// We don't need to print anything, but we want this file counted
128
// in the total number of checked files even if it has no errors.
129
$this->_lastCheckedFile = $report['filename'];
132
}//end generateFileReport()
136
* Generates a summary of errors and warnings for each file processed.
138
* @param string $cachedData Any partial report data that was returned from
139
* generateFileReport during the run.
140
* @param int $totalFiles Total number of files processed during the run.
141
* @param int $totalErrors Total number of errors found during the run.
142
* @param int $totalWarnings Total number of warnings found during the run.
143
* @param int $totalFixable Total number of problems that can be fixed.
144
* @param boolean $showSources Show sources?
145
* @param int $width Maximum allowed line width.
146
* @param boolean $toScreen Is the report being printed to screen?
150
public function generate(
160
$msg = $this->generateMessage($totalFiles, $totalErrors, $totalWarnings);
162
if ($this->showOk === true) {
163
$this->notifyAllFine();
166
$this->notifyErrors($msg);
173
* Generate the error message to show to the user.
175
* @param int $totalFiles Total number of files processed during the run.
176
* @param int $totalErrors Total number of errors found during the run.
177
* @param int $totalWarnings Total number of warnings found during the run.
179
* @return string Error message or NULL if no error/warning found.
181
protected function generateMessage($totalFiles, $totalErrors, $totalWarnings)
183
if ($totalErrors === 0 && $totalWarnings === 0) {
189
if ($totalFiles > 1) {
190
$msg .= 'Checked '.$totalFiles.' files'.PHP_EOL;
192
$msg .= $this->_lastCheckedFile.PHP_EOL;
195
if ($totalWarnings > 0) {
196
$msg .= $totalWarnings.' warnings'.PHP_EOL;
199
if ($totalErrors > 0) {
200
$msg .= $totalErrors.' errors'.PHP_EOL;
205
}//end generateMessage()
209
* Tell the user that all is fine and no error/warning has been found.
213
protected function notifyAllFine()
215
$cmd = $this->getBasicCommand();
217
$cmd .= ' "PHP CodeSniffer: Ok"';
218
$cmd .= ' "All fine"';
221
}//end notifyAllFine()
225
* Tell the user that errors/warnings have been found.
227
* @param string $msg Message to display.
231
protected function notifyErrors($msg)
233
$cmd = $this->getBasicCommand();
235
$cmd .= ' "PHP CodeSniffer: Error"';
236
$cmd .= ' '.escapeshellarg(trim($msg));
239
}//end notifyErrors()
243
* Generate and return the basic notify-send command string to execute.
245
* @return string Shell command with common parameters.
247
protected function getBasicCommand()
249
$cmd = escapeshellcmd($this->path);
250
$cmd .= ' --category dev.validate';
251
$cmd .= ' -h int:transient:1';
252
$cmd .= ' -t '.(int) $this->timeout;
253
if (version_compare($this->version, '0.7.3', '>=') === true) {
259
}//end getBasicCommand()