annotate vendor/squizlabs/php_codesniffer/src/Reports/Notifysend.php @ 19:fa3358dc1485 tip

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