Skip to content

Commit

Permalink
Merge pull request #39 from cweiske/master
Browse files Browse the repository at this point in the history
Notifysend report to show desktop notifications
  • Loading branch information
gsherwood committed Aug 14, 2012
2 parents 2112f36 + d7315e4 commit 655fb64
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CodeSniffer/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -376,6 +376,7 @@ public function processLongArgument($arg, $pos, $values)
'checkstyle',
'csv',
'emacs',
'notifysend',
'source',
'summary',
'svnblame',
Expand Down Expand Up @@ -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;
Expand Down
189 changes: 189 additions & 0 deletions CodeSniffer/Reports/Notifysend.php
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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>
Expand Down

0 comments on commit 655fb64

Please sign in to comment.