Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Notify-send report for PHP_CodeSniffer.
|
Chris@0
|
4 *
|
Chris@0
|
5 * PHP version 5
|
Chris@0
|
6 *
|
Chris@0
|
7 * @category PHP
|
Chris@0
|
8 * @package PHP_CodeSniffer
|
Chris@0
|
9 * @author Christian Weiske <christian.weiske@netresearch.de>
|
Chris@0
|
10 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@0
|
11 * @copyright 2012-2014 Christian Weiske
|
Chris@0
|
12 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@0
|
13 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@0
|
14 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
15 */
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Notify-send report for PHP_CodeSniffer.
|
Chris@0
|
19 *
|
Chris@0
|
20 * Supported configuration parameters:
|
Chris@0
|
21 * - notifysend_path - Full path to notify-send cli command
|
Chris@0
|
22 * - notifysend_timeout - Timeout in milliseconds
|
Chris@0
|
23 * - notifysend_showok - Show "ok, all fine" messages (0/1)
|
Chris@0
|
24 *
|
Chris@0
|
25 * @category PHP
|
Chris@0
|
26 * @package PHP_CodeSniffer
|
Chris@0
|
27 * @author Christian Weiske <christian.weiske@netresearch.de>
|
Chris@0
|
28 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@0
|
29 * @copyright 2012-2014 Christian Weiske
|
Chris@0
|
30 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@0
|
31 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@0
|
32 * @version Release: @package_version@
|
Chris@0
|
33 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
34 */
|
Chris@0
|
35 class PHP_CodeSniffer_Reports_Notifysend implements PHP_CodeSniffer_Report
|
Chris@0
|
36 {
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Notification timeout in milliseconds.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var integer
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $timeout = 3000;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Path to notify-send command.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var string
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $path = 'notify-send';
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Show "ok, all fine" messages.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @var boolean
|
Chris@0
|
56 */
|
Chris@0
|
57 protected $showOk = true;
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Version of installed notify-send executable.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @var string
|
Chris@0
|
63 */
|
Chris@0
|
64 protected $version = null;
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * A record of the last file checked.
|
Chris@0
|
68 *
|
Chris@0
|
69 * This is used in case we only checked one file and need to print
|
Chris@0
|
70 * the name/path of the file. We wont have access to the checked file list
|
Chris@0
|
71 * after the run has been completed.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @var string
|
Chris@0
|
74 */
|
Chris@0
|
75 private $_lastCheckedFile = '';
|
Chris@0
|
76
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Load configuration data.
|
Chris@0
|
80 */
|
Chris@0
|
81 public function __construct()
|
Chris@0
|
82 {
|
Chris@0
|
83 $path = PHP_CodeSniffer::getConfigData('notifysend_path');
|
Chris@0
|
84 if ($path !== null) {
|
Chris@0
|
85 $this->path = escapeshellcmd($path);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 $timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
|
Chris@0
|
89 if ($timeout !== null) {
|
Chris@0
|
90 $this->timeout = (int) $timeout;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 $showOk = PHP_CodeSniffer::getConfigData('notifysend_showok');
|
Chris@0
|
94 if ($showOk !== null) {
|
Chris@0
|
95 $this->showOk = (boolean) $showOk;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 $this->version = str_replace(
|
Chris@0
|
99 'notify-send ',
|
Chris@0
|
100 '',
|
Chris@0
|
101 exec($this->path.' --version')
|
Chris@0
|
102 );
|
Chris@0
|
103
|
Chris@0
|
104 }//end __construct()
|
Chris@0
|
105
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Generate a partial report for a single processed file.
|
Chris@0
|
109 *
|
Chris@0
|
110 * Function should return TRUE if it printed or stored data about the file
|
Chris@0
|
111 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
|
Chris@0
|
112 * its data should be counted in the grand totals.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param array $report Prepared report data.
|
Chris@0
|
115 * @param PHP_CodeSniffer_File $phpcsFile The file being reported on.
|
Chris@0
|
116 * @param boolean $showSources Show sources?
|
Chris@0
|
117 * @param int $width Maximum allowed line width.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @return boolean
|
Chris@0
|
120 */
|
Chris@0
|
121 public function generateFileReport(
|
Chris@0
|
122 $report,
|
Chris@0
|
123 PHP_CodeSniffer_File $phpcsFile,
|
Chris@0
|
124 $showSources=false,
|
Chris@0
|
125 $width=80
|
Chris@0
|
126 ) {
|
Chris@0
|
127 // We don't need to print anything, but we want this file counted
|
Chris@0
|
128 // in the total number of checked files even if it has no errors.
|
Chris@0
|
129 $this->_lastCheckedFile = $report['filename'];
|
Chris@0
|
130 return true;
|
Chris@0
|
131
|
Chris@0
|
132 }//end generateFileReport()
|
Chris@0
|
133
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Generates a summary of errors and warnings for each file processed.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @param string $cachedData Any partial report data that was returned from
|
Chris@0
|
139 * generateFileReport during the run.
|
Chris@0
|
140 * @param int $totalFiles Total number of files processed during the run.
|
Chris@0
|
141 * @param int $totalErrors Total number of errors found during the run.
|
Chris@0
|
142 * @param int $totalWarnings Total number of warnings found during the run.
|
Chris@0
|
143 * @param int $totalFixable Total number of problems that can be fixed.
|
Chris@0
|
144 * @param boolean $showSources Show sources?
|
Chris@0
|
145 * @param int $width Maximum allowed line width.
|
Chris@0
|
146 * @param boolean $toScreen Is the report being printed to screen?
|
Chris@0
|
147 *
|
Chris@0
|
148 * @return void
|
Chris@0
|
149 */
|
Chris@0
|
150 public function generate(
|
Chris@0
|
151 $cachedData,
|
Chris@0
|
152 $totalFiles,
|
Chris@0
|
153 $totalErrors,
|
Chris@0
|
154 $totalWarnings,
|
Chris@0
|
155 $totalFixable,
|
Chris@0
|
156 $showSources=false,
|
Chris@0
|
157 $width=80,
|
Chris@0
|
158 $toScreen=true
|
Chris@0
|
159 ) {
|
Chris@0
|
160 $msg = $this->generateMessage($totalFiles, $totalErrors, $totalWarnings);
|
Chris@0
|
161 if ($msg === null) {
|
Chris@0
|
162 if ($this->showOk === true) {
|
Chris@0
|
163 $this->notifyAllFine();
|
Chris@0
|
164 }
|
Chris@0
|
165 } else {
|
Chris@0
|
166 $this->notifyErrors($msg);
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 }//end generate()
|
Chris@0
|
170
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Generate the error message to show to the user.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @param int $totalFiles Total number of files processed during the run.
|
Chris@0
|
176 * @param int $totalErrors Total number of errors found during the run.
|
Chris@0
|
177 * @param int $totalWarnings Total number of warnings found during the run.
|
Chris@0
|
178 *
|
Chris@0
|
179 * @return string Error message or NULL if no error/warning found.
|
Chris@0
|
180 */
|
Chris@0
|
181 protected function generateMessage($totalFiles, $totalErrors, $totalWarnings)
|
Chris@0
|
182 {
|
Chris@0
|
183 if ($totalErrors === 0 && $totalWarnings === 0) {
|
Chris@0
|
184 // Nothing to print.
|
Chris@0
|
185 return null;
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 $msg = '';
|
Chris@0
|
189 if ($totalFiles > 1) {
|
Chris@0
|
190 $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL;
|
Chris@0
|
191 } else {
|
Chris@0
|
192 $msg .= $this->_lastCheckedFile.PHP_EOL;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 if ($totalWarnings > 0) {
|
Chris@0
|
196 $msg .= $totalWarnings.' warnings'.PHP_EOL;
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 if ($totalErrors > 0) {
|
Chris@0
|
200 $msg .= $totalErrors.' errors'.PHP_EOL;
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 return $msg;
|
Chris@0
|
204
|
Chris@0
|
205 }//end generateMessage()
|
Chris@0
|
206
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * Tell the user that all is fine and no error/warning has been found.
|
Chris@0
|
210 *
|
Chris@0
|
211 * @return void
|
Chris@0
|
212 */
|
Chris@0
|
213 protected function notifyAllFine()
|
Chris@0
|
214 {
|
Chris@0
|
215 $cmd = $this->getBasicCommand();
|
Chris@0
|
216 $cmd .= ' -i info';
|
Chris@0
|
217 $cmd .= ' "PHP CodeSniffer: Ok"';
|
Chris@0
|
218 $cmd .= ' "All fine"';
|
Chris@0
|
219 exec($cmd);
|
Chris@0
|
220
|
Chris@0
|
221 }//end notifyAllFine()
|
Chris@0
|
222
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * Tell the user that errors/warnings have been found.
|
Chris@0
|
226 *
|
Chris@0
|
227 * @param string $msg Message to display.
|
Chris@0
|
228 *
|
Chris@0
|
229 * @return void
|
Chris@0
|
230 */
|
Chris@0
|
231 protected function notifyErrors($msg)
|
Chris@0
|
232 {
|
Chris@0
|
233 $cmd = $this->getBasicCommand();
|
Chris@0
|
234 $cmd .= ' -i error';
|
Chris@0
|
235 $cmd .= ' "PHP CodeSniffer: Error"';
|
Chris@0
|
236 $cmd .= ' '.escapeshellarg(trim($msg));
|
Chris@0
|
237 exec($cmd);
|
Chris@0
|
238
|
Chris@0
|
239 }//end notifyErrors()
|
Chris@0
|
240
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * Generate and return the basic notify-send command string to execute.
|
Chris@0
|
244 *
|
Chris@0
|
245 * @return string Shell command with common parameters.
|
Chris@0
|
246 */
|
Chris@0
|
247 protected function getBasicCommand()
|
Chris@0
|
248 {
|
Chris@0
|
249 $cmd = $this->path;
|
Chris@0
|
250 $cmd .= ' --category dev.validate';
|
Chris@0
|
251 $cmd .= ' -h int:transient:1';
|
Chris@0
|
252 $cmd .= ' -t '.(int) $this->timeout;
|
Chris@0
|
253 if (version_compare($this->version, '0.7.3', '>=') === true) {
|
Chris@0
|
254 $cmd .= ' -a phpcs';
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 return $cmd;
|
Chris@0
|
258
|
Chris@0
|
259 }//end getBasicCommand()
|
Chris@0
|
260
|
Chris@0
|
261
|
Chris@0
|
262 }//end class
|