comparison vendor/squizlabs/php_codesniffer/src/Reports/Notifysend.php @ 17:129ea1e6d783

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