annotate vendor/squizlabs/php_codesniffer/src/Reports/Notifysend.php @ 4:a9cd425dd02b

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