Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Notifysend report to show desktop notifications #39

Merged
merged 4 commits into from over 1 year ago

2 participants

Christian Weiske Greg Sherwood
Christian Weiske

A new Codesniffer report that shows a short popup message on your desktop. Very useful when phpcs is automatically executed when saving a file.

Greg Sherwood
Owner

I have no way I can currently test this and I'm not sure what conventions are used in these messages currently.

Can I trust that you have tested this thoroughly?
Are there version or OS requirements that need to be considered for notify-send (needed for the PHPCS docs)?
Should the path to notify-send actually be a PHP_CodeSniffer config setting?
Anything else I should be aware of?

Christian Weiske
Greg Sherwood
Owner

The config stuff in PHPCS is very basic. You just go ahead and use config vars in the code and handle defaults. So something like:

$path = PHP_CodeSniffer::getConfigData('notifysend_path');
if ($path === null) {
// Try default path, or maybe throw an exception telling them to set the path.
}

Same would go for the timeout:
$timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
if ($timeout === null) {
$timeout = 3000;
}

You set those config vars like this:
phpcs --config-set notifysend_path /path/to/notify-send
phpcs --config-set notifysend_timeout 5000

Christian Weiske

I've implemented the configuration options now.

Screenshots:
all fine
error

Greg Sherwood gsherwood merged commit 655fb64 into from August 13, 2012
Greg Sherwood gsherwood closed this August 13, 2012
Greg Sherwood
Owner

Found a linux box at work to try this one and it worked great. Thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
6  CodeSniffer/CLI.php
@@ -357,7 +357,7 @@ public function processLongArgument($arg, $pos, $values)
357 357
                         $report = substr($arg, 7);
358 358
                         $output = null;
359 359
                     } else {
360  
-                        
  360
+
361 361
                         $report = substr($arg, 7, ($split - 7));
362 362
                         $output = substr($arg, ($split + 1));
363 363
                         if ($output === false) {
@@ -376,6 +376,7 @@ public function processLongArgument($arg, $pos, $values)
376 376
                                      'checkstyle',
377 377
                                      'csv',
378 378
                                      'emacs',
  379
+                                     'notifysend',
379 380
                                      'source',
380 381
                                      'summary',
381 382
                                      'svnblame',
@@ -711,7 +712,8 @@ public function printUsage()
711 712
         echo '        <generator>   The name of a doc generator to use'.PHP_EOL;
712 713
         echo '                      (forces doc generation instead of checking)'.PHP_EOL;
713 714
         echo '        <report>      Print either the "full", "xml", "checkstyle", "csv", "emacs"'.PHP_EOL;
714  
-        echo '                      "source", "summary", "svnblame", "gitblame" or "hgblame" report'.PHP_EOL;
  715
+        echo '                      "source", "summary", "svnblame", "gitblame", "hgblame" or'.PHP_EOL;
  716
+        echo '                      "notifysend" report'.PHP_EOL;
715 717
         echo '                      (the "full" report is printed by default)'.PHP_EOL;
716 718
         echo '        <reportfile>  Write the report to the specified file path'.PHP_EOL;
717 719
         echo '        <reportWidth> How many columns wide screen reports should be printed'.PHP_EOL;
189  CodeSniffer/Reports/Notifysend.php
... ...
@@ -0,0 +1,189 @@
  1
+<?php
  2
+/**
  3
+ * Summary report for PHP_CodeSniffer.
  4
+ *
  5
+ * PHP version 5
  6
+ *
  7
+ * @category  PHP
  8
+ * @package   PHP_CodeSniffer
  9
+ * @author    Christian Weiske <christian.weiske@netresearch.de>
  10
+ * @copyright 2012 Christian Weiske
  11
+ * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  12
+ * @link      http://pear.php.net/package/PHP_CodeSniffer
  13
+ */
  14
+
  15
+/**
  16
+ * Summary report for PHP_CodeSniffer that can be used with notify-send.
  17
+ *
  18
+ * Supported configuration parameters:
  19
+ * - notifysend_path    - Full path to notify-send cli command
  20
+ * - notifysend_timeout - Timeout in milliseconds
  21
+ * - notifysend_showok  - Show "ok, all fine" messages (0/1)
  22
+ *
  23
+ * @category  PHP
  24
+ * @package   PHP_CodeSniffer
  25
+ * @author    Christian Weiske <christian.weiske@netresearch.de>
  26
+ * @copyright 2012 Christian Weiske
  27
+ * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  28
+ * @version   Release: @package_version@
  29
+ * @link      http://pear.php.net/package/PHP_CodeSniffer
  30
+ */
  31
+class PHP_CodeSniffer_Reports_Notifysend
  32
+    implements PHP_CodeSniffer_Report
  33
+{
  34
+    /**
  35
+     * Notification timeout in milliseconds
  36
+     *
  37
+     * @var integer
  38
+     */
  39
+    protected $timeout = 3000;
  40
+
  41
+    /**
  42
+     * Path to notify-send command
  43
+     *
  44
+     * @var string
  45
+     */
  46
+    protected $path = 'notify-send';
  47
+
  48
+    /**
  49
+     * Show "ok, all fine" messages
  50
+     *
  51
+     * @var boolean
  52
+     */
  53
+    protected $showOk = true;
  54
+
  55
+
  56
+    /**
  57
+     * Load configuration data
  58
+     */
  59
+    public function __construct()
  60
+    {
  61
+        $path = PHP_CodeSniffer::getConfigData('notifysend_path');
  62
+        if ($path !== null) {
  63
+            $this->path = $path;
  64
+        }
  65
+
  66
+        $timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
  67
+        if ($timeout !== null) {
  68
+            $this->timeout = (int)$timeout;
  69
+        }
  70
+
  71
+        $showOk = PHP_CodeSniffer::getConfigData('notifysend_showok');
  72
+        if ($showOk !== null) {
  73
+            $this->showOk = (boolean)$showOk;
  74
+        }
  75
+    }
  76
+
  77
+
  78
+    /**
  79
+     * Generates a summary of errors and warnings for each file processed.
  80
+     *
  81
+     * If verbose output is enabled, results are shown for all files, even if
  82
+     * they have no errors or warnings. If verbose output is disabled, we only
  83
+     * show files that have at least one warning or error.
  84
+     *
  85
+     * @param array   $report      Prepared report.
  86
+     * @param boolean $showSources Show sources?
  87
+     * @param int     $width       Maximum allowed line width.
  88
+     * @param boolean $toScreen    Is the report being printed to screen?
  89
+     *
  90
+     * @return string
  91
+     */
  92
+    public function generate(
  93
+        $report, $showSources = false, $width = 80,$toScreen = true
  94
+    ) {
  95
+        $msg = $this->generateMessage($report);
  96
+        if ($msg === null) {
  97
+            if ($this->showOk) {
  98
+                $this->notifyAllFine();
  99
+            }
  100
+            return 0;
  101
+        }
  102
+
  103
+        $this->notifyErrors($msg);
  104
+
  105
+        return ($report['totals']['errors'] + $report['totals']['warnings']);
  106
+    }
  107
+
  108
+    /**
  109
+     * Generate the error message to show to the user
  110
+     *
  111
+     * @param array $report CS report data
  112
+     *
  113
+     * @return string Error message or NULL if no error/warning found
  114
+     */
  115
+    protected function generateMessage($report)
  116
+    {
  117
+        $allErrors   = $report['totals']['errors'];
  118
+        $allWarnings = $report['totals']['warnings'];
  119
+
  120
+        if ($allErrors == 0 && $allWarnings == 0) {
  121
+            // Nothing to print.
  122
+            return null;
  123
+        }
  124
+
  125
+        $msg = '';
  126
+        if (count($report['files']) > 1) {
  127
+            $msg .= 'Checked ' . count($report['files']) . ' files' . PHP_EOL;
  128
+        } else {
  129
+            $msg .= key($report['files']) . PHP_EOL;
  130
+        }
  131
+        if ($allWarnings > 0) {
  132
+            $msg .= $allWarnings . ' warnings' . PHP_EOL;
  133
+        }
  134
+        if ($allErrors > 0) {
  135
+            $msg .= $allErrors . ' errors' . PHP_EOL;
  136
+        }
  137
+
  138
+        return $msg;
  139
+    }
  140
+
  141
+    /**
  142
+     * Tell the user that all is fine and no error/warning has been found.
  143
+     *
  144
+     * @return void
  145
+     */
  146
+    protected function notifyAllFine()
  147
+    {
  148
+        exec(
  149
+            $this->getBasicCommand()
  150
+            . ' -i info'
  151
+            . ' "PHP CodeSniffer: Ok"'
  152
+            . ' "All fine"'
  153
+        );
  154
+    }
  155
+
  156
+
  157
+    /**
  158
+     * Tell the user that errors/warnings have been found.
  159
+     *
  160
+     * @param string $msg Message to display
  161
+     *
  162
+     * @return void
  163
+     */
  164
+    protected function notifyErrors($msg)
  165
+    {
  166
+        exec(
  167
+            $this->getBasicCommand()
  168
+            . ' -i error'
  169
+            . ' "PHP CodeSniffer: Error"'
  170
+            . ' ' . escapeshellarg(trim($msg))
  171
+        );
  172
+    }
  173
+
  174
+
  175
+    /**
  176
+     * Generate and return the basic notify-send command string to execute
  177
+     *
  178
+     * @return string Shell command with common parameters
  179
+     */
  180
+    protected function getBasicCommand()
  181
+    {
  182
+        return escapeshellcmd($this->path)
  183
+            . ' --category dev.validate'
  184
+            . ' -a phpcs'
  185
+            . ' -t ' . (int) $this->timeout;
  186
+    }
  187
+}
  188
+
  189
+?>
3  package.xml
@@ -220,6 +220,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
220 220
      <file baseinstalldir="PHP" name="Hgblame.php" role="php">
221 221
       <tasks:replace from="@package_version@" to="version" type="package-info" />
222 222
      </file>
  223
+     <file baseinstalldir="PHP" name="Notifysend.php" role="php">
  224
+      <tasks:replace from="@package_version@" to="version" type="package-info" />
  225
+     </file>
223 226
      <file baseinstalldir="PHP" name="Source.php" role="php">
224 227
       <tasks:replace from="@package_version@" to="version" type="package-info" />
225 228
      </file>
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.