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: 1.5.4
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.
83
public function __construct()
85
$path = PHP_CodeSniffer::getConfigData('notifysend_path');
90
$timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
91
if ($timeout !== null) {
92
$this->timeout = (int) $timeout;
95
$showOk = PHP_CodeSniffer::getConfigData('notifysend_showok');
96
if ($showOk !== null) {
97
$this->showOk = (boolean) $showOk;
100
$this->version = str_replace(
103
exec($this->path . ' --version')
110
* Generate a partial report for a single processed file.
112
* Function should return TRUE if it printed or stored data about the file
113
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
114
* its data should be counted in the grand totals.
116
* @param array $report Prepared report data.
117
* @param boolean $showSources Show sources?
118
* @param int $width Maximum allowed line width.
122
public function generateFileReport(
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 boolean $showSources Show sources?
144
* @param int $width Maximum allowed line width.
145
* @param boolean $toScreen Is the report being printed to screen?
149
public function generate(
158
$msg = $this->generateMessage($totalFiles, $totalErrors, $totalWarnings);
160
if ($this->showOk === true) {
161
$this->notifyAllFine();
164
$this->notifyErrors($msg);
171
* Generate the error message to show to the user.
173
* @param int $totalFiles Total number of files processed during the run.
174
* @param int $totalErrors Total number of errors found during the run.
175
* @param int $totalWarnings Total number of warnings found during the run.
177
* @return string Error message or NULL if no error/warning found.
179
protected function generateMessage($totalFiles, $totalErrors, $totalWarnings)
181
if ($totalErrors === 0 && $totalWarnings === 0) {
187
if ($totalFiles > 1) {
188
$msg .= 'Checked '.$totalFiles.' files'.PHP_EOL;
190
$msg .= $this->_lastCheckedFile.PHP_EOL;
193
if ($totalWarnings > 0) {
194
$msg .= $totalWarnings.' warnings'.PHP_EOL;
197
if ($totalErrors > 0) {
198
$msg .= $totalErrors.' errors'.PHP_EOL;
203
}//end generateMessage()
207
* Tell the user that all is fine and no error/warning has been found.
211
protected function notifyAllFine()
213
$cmd = $this->getBasicCommand();
215
$cmd .= ' "PHP CodeSniffer: Ok"';
216
$cmd .= ' "All fine"';
219
}//end notifyAllFine()
223
* Tell the user that errors/warnings have been found.
225
* @param string $msg Message to display.
229
protected function notifyErrors($msg)
231
$cmd = $this->getBasicCommand();
233
$cmd .= ' "PHP CodeSniffer: Error"';
234
$cmd .= ' '.escapeshellarg(trim($msg));
237
}//end notifyErrors()
241
* Generate and return the basic notify-send command string to execute.
243
* @return string Shell command with common parameters.
245
protected function getBasicCommand()
247
$cmd = escapeshellcmd($this->path);
248
$cmd .= ' --category dev.validate';
249
$cmd .= ' -h int:transient:1';
250
$cmd .= ' -t '.(int) $this->timeout;
251
if (version_compare($this->version, '0.7.3', '>=') === true) {
257
}//end getBasicCommand()