~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.4/CodeSniffer/Reports/Notifysend.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

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