Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Notifysend report to show desktop notifications #39

Merged
merged 4 commits into from

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
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
View
@@ -357,7 +357,7 @@ public function processLongArgument($arg, $pos, $values)
$report = substr($arg, 7);
$output = null;
} else {
-
+
$report = substr($arg, 7, ($split - 7));
$output = substr($arg, ($split + 1));
if ($output === false) {
@@ -376,6 +376,7 @@ public function processLongArgument($arg, $pos, $values)
'checkstyle',
'csv',
'emacs',
+ 'notifysend',
'source',
'summary',
'svnblame',
@@ -711,7 +712,8 @@ public function printUsage()
echo ' <generator> The name of a doc generator to use'.PHP_EOL;
echo ' (forces doc generation instead of checking)'.PHP_EOL;
echo ' <report> Print either the "full", "xml", "checkstyle", "csv", "emacs"'.PHP_EOL;
- echo ' "source", "summary", "svnblame", "gitblame" or "hgblame" report'.PHP_EOL;
+ echo ' "source", "summary", "svnblame", "gitblame", "hgblame" or'.PHP_EOL;
+ echo ' "notifysend" report'.PHP_EOL;
echo ' (the "full" report is printed by default)'.PHP_EOL;
echo ' <reportfile> Write the report to the specified file path'.PHP_EOL;
echo ' <reportWidth> How many columns wide screen reports should be printed'.PHP_EOL;
189 CodeSniffer/Reports/Notifysend.php
View
@@ -0,0 +1,189 @@
+<?php
+/**
+ * Summary report for PHP_CodeSniffer.
+ *
+ * PHP version 5
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Christian Weiske <christian.weiske@netresearch.de>
+ * @copyright 2012 Christian Weiske
+ * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ */
+
+/**
+ * Summary report for PHP_CodeSniffer that can be used with notify-send.
+ *
+ * Supported configuration parameters:
+ * - notifysend_path - Full path to notify-send cli command
+ * - notifysend_timeout - Timeout in milliseconds
+ * - notifysend_showok - Show "ok, all fine" messages (0/1)
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Christian Weiske <christian.weiske@netresearch.de>
+ * @copyright 2012 Christian Weiske
+ * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ */
+class PHP_CodeSniffer_Reports_Notifysend
+ implements PHP_CodeSniffer_Report
+{
+ /**
+ * Notification timeout in milliseconds
+ *
+ * @var integer
+ */
+ protected $timeout = 3000;
+
+ /**
+ * Path to notify-send command
+ *
+ * @var string
+ */
+ protected $path = 'notify-send';
+
+ /**
+ * Show "ok, all fine" messages
+ *
+ * @var boolean
+ */
+ protected $showOk = true;
+
+
+ /**
+ * Load configuration data
+ */
+ public function __construct()
+ {
+ $path = PHP_CodeSniffer::getConfigData('notifysend_path');
+ if ($path !== null) {
+ $this->path = $path;
+ }
+
+ $timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
+ if ($timeout !== null) {
+ $this->timeout = (int)$timeout;
+ }
+
+ $showOk = PHP_CodeSniffer::getConfigData('notifysend_showok');
+ if ($showOk !== null) {
+ $this->showOk = (boolean)$showOk;
+ }
+ }
+
+
+ /**
+ * Generates a summary of errors and warnings for each file processed.
+ *
+ * If verbose output is enabled, results are shown for all files, even if
+ * they have no errors or warnings. If verbose output is disabled, we only
+ * show files that have at least one warning or error.
+ *
+ * @param array $report Prepared report.
+ * @param boolean $showSources Show sources?
+ * @param int $width Maximum allowed line width.
+ * @param boolean $toScreen Is the report being printed to screen?
+ *
+ * @return string
+ */
+ public function generate(
+ $report, $showSources = false, $width = 80,$toScreen = true
+ ) {
+ $msg = $this->generateMessage($report);
+ if ($msg === null) {
+ if ($this->showOk) {
+ $this->notifyAllFine();
+ }
+ return 0;
+ }
+
+ $this->notifyErrors($msg);
+
+ return ($report['totals']['errors'] + $report['totals']['warnings']);
+ }
+
+ /**
+ * Generate the error message to show to the user
+ *
+ * @param array $report CS report data
+ *
+ * @return string Error message or NULL if no error/warning found
+ */
+ protected function generateMessage($report)
+ {
+ $allErrors = $report['totals']['errors'];
+ $allWarnings = $report['totals']['warnings'];
+
+ if ($allErrors == 0 && $allWarnings == 0) {
+ // Nothing to print.
+ return null;
+ }
+
+ $msg = '';
+ if (count($report['files']) > 1) {
+ $msg .= 'Checked ' . count($report['files']) . ' files' . PHP_EOL;
+ } else {
+ $msg .= key($report['files']) . PHP_EOL;
+ }
+ if ($allWarnings > 0) {
+ $msg .= $allWarnings . ' warnings' . PHP_EOL;
+ }
+ if ($allErrors > 0) {
+ $msg .= $allErrors . ' errors' . PHP_EOL;
+ }
+
+ return $msg;
+ }
+
+ /**
+ * Tell the user that all is fine and no error/warning has been found.
+ *
+ * @return void
+ */
+ protected function notifyAllFine()
+ {
+ exec(
+ $this->getBasicCommand()
+ . ' -i info'
+ . ' "PHP CodeSniffer: Ok"'
+ . ' "All fine"'
+ );
+ }
+
+
+ /**
+ * Tell the user that errors/warnings have been found.
+ *
+ * @param string $msg Message to display
+ *
+ * @return void
+ */
+ protected function notifyErrors($msg)
+ {
+ exec(
+ $this->getBasicCommand()
+ . ' -i error'
+ . ' "PHP CodeSniffer: Error"'
+ . ' ' . escapeshellarg(trim($msg))
+ );
+ }
+
+
+ /**
+ * Generate and return the basic notify-send command string to execute
+ *
+ * @return string Shell command with common parameters
+ */
+ protected function getBasicCommand()
+ {
+ return escapeshellcmd($this->path)
+ . ' --category dev.validate'
+ . ' -a phpcs'
+ . ' -t ' . (int) $this->timeout;
+ }
+}
+
+?>
3  package.xml
View
@@ -220,6 +220,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="Hgblame.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
+ <file baseinstalldir="PHP" name="Notifysend.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file baseinstalldir="PHP" name="Source.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Something went wrong with that request. Please try again.