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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.4/CodeSniffer/Reporting.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
 
 * A class to manage reporting.
4
 
 *
5
 
 * PHP version 5
6
 
 *
7
 
 * @category  PHP
8
 
 * @package   PHP_CodeSniffer
9
 
 * @author    Gabriele Santini <gsantini@sqli.com>
10
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
11
 
 * @copyright 2009-2014 SQLI <www.sqli.com>
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
 
if (is_file(dirname(__FILE__).'/../CodeSniffer.php') === true) {
18
 
    include_once dirname(__FILE__).'/../CodeSniffer.php';
19
 
} else {
20
 
    include_once 'PHP/CodeSniffer.php';
21
 
}
22
 
 
23
 
/**
24
 
 * A class to manage reporting.
25
 
 *
26
 
 * @category  PHP
27
 
 * @package   PHP_CodeSniffer
28
 
 * @author    Gabriele Santini <gsantini@sqli.com>
29
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
30
 
 * @copyright 2009-2014 SQLI <www.sqli.com>
31
 
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
32
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
33
 
 * @version   Release: 1.5.4
34
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
35
 
 */
36
 
class PHP_CodeSniffer_Reporting
37
 
{
38
 
 
39
 
    /**
40
 
     * Total number of files that contain errors or warnings.
41
 
     *
42
 
     * @var int
43
 
     */
44
 
    public $totalFiles = 0;
45
 
 
46
 
    /**
47
 
     * Total number of errors found during the run.
48
 
     *
49
 
     * @var int
50
 
     */
51
 
    public $totalErrors = 0;
52
 
 
53
 
    /**
54
 
     * Total number of warnings found during the run.
55
 
     *
56
 
     * @var int
57
 
     */
58
 
    public $totalWarnings = 0;
59
 
 
60
 
    /**
61
 
     * A list of reports that have written partial report output.
62
 
     *
63
 
     * @var array
64
 
     */
65
 
    private $_cachedReports = array();
66
 
 
67
 
    /**
68
 
     * A cache of report objects.
69
 
     *
70
 
     * @var array
71
 
     */
72
 
    private $_reports = array();
73
 
 
74
 
    /**
75
 
     * A cache of opened tmp files.
76
 
     *
77
 
     * @var array
78
 
     */
79
 
    private $_tmpFiles = array();
80
 
 
81
 
 
82
 
    /**
83
 
     * Produce the appropriate report object based on $type parameter.
84
 
     *
85
 
     * @param string $type The type of the report.
86
 
     *
87
 
     * @return PHP_CodeSniffer_Report
88
 
     * @throws PHP_CodeSniffer_Exception If report is not available.
89
 
     */
90
 
    public function factory($type)
91
 
    {
92
 
        $type = ucfirst($type);
93
 
        if (isset($this->_reports[$type]) === true) {
94
 
            return $this->_reports[$type];
95
 
        }
96
 
 
97
 
        $filename        = $type.'.php';
98
 
        $reportClassName = 'PHP_CodeSniffer_Reports_'.$type;
99
 
        if (class_exists($reportClassName, true) === false) {
100
 
            throw new PHP_CodeSniffer_Exception('Report type "'.$type.'" not found.');
101
 
        }
102
 
 
103
 
        $reportClass = new $reportClassName();
104
 
        if (false === ($reportClass instanceof PHP_CodeSniffer_Report)) {
105
 
            throw new PHP_CodeSniffer_Exception('Class "'.$reportClassName.'" must implement the "PHP_CodeSniffer_Report" interface.');
106
 
        }
107
 
 
108
 
        $this->_reports[$type] = $reportClass;
109
 
        return $this->_reports[$type];
110
 
 
111
 
    }//end factory()
112
 
 
113
 
 
114
 
    /**
115
 
     * Actually generates the report.
116
 
     * 
117
 
     * @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
118
 
     * @param array                $cliValues An array of command line arguments.
119
 
     * 
120
 
     * @return void
121
 
     */
122
 
    public function cacheFileReport(PHP_CodeSniffer_File $phpcsFile, array $cliValues)
123
 
    {
124
 
        if (isset($cliValues['reports']) === false) {
125
 
            // This happens during unit testing, or any time someone just wants
126
 
            // the error data and not the printed report.
127
 
            return;
128
 
        }
129
 
 
130
 
        $reportData  = $this->prepareFileReport($phpcsFile);
131
 
        $errorsShown = false;
132
 
 
133
 
        foreach ($cliValues['reports'] as $report => $output) {
134
 
            $reportClass = self::factory($report);
135
 
 
136
 
            ob_start();
137
 
            $result = $reportClass->generateFileReport($reportData, $cliValues['showSources'], $cliValues['reportWidth']);
138
 
            if ($result === true) {
139
 
                $errorsShown = true;
140
 
            }
141
 
 
142
 
            $generatedReport = ob_get_contents();
143
 
            ob_end_clean();
144
 
 
145
 
            if ($generatedReport === '') {
146
 
                continue;
147
 
            }
148
 
 
149
 
            if ($output === null && $cliValues['reportFile'] !== null) {
150
 
                $output = $cliValues['reportFile'];
151
 
            }
152
 
 
153
 
            if ($output === null) {
154
 
                // Using a temp file.
155
 
                if (isset($this->_tmpFiles[$report]) === false) {
156
 
                    $this->_tmpFiles[$report] = tmpfile();
157
 
                }
158
 
 
159
 
                fwrite($this->_tmpFiles[$report], $generatedReport);
160
 
            } else {
161
 
                $flags = FILE_APPEND;
162
 
                if (in_array($report, $this->_cachedReports) === false) {
163
 
                    $this->_cachedReports[] = $report;
164
 
                    $flags = null;
165
 
                }
166
 
 
167
 
                file_put_contents($output, $generatedReport, $flags);
168
 
            }
169
 
        }//end foreach
170
 
 
171
 
        if ($errorsShown === true) {
172
 
            $this->totalFiles++;
173
 
            $this->totalErrors   += $reportData['errors'];
174
 
            $this->totalWarnings += $reportData['warnings'];
175
 
        }
176
 
 
177
 
    }//end cacheFileReport()
178
 
 
179
 
 
180
 
    /**
181
 
     * Actually generates the report.
182
 
     * 
183
 
     * @param string  $report      Report type.
184
 
     * @param boolean $showSources Show sources?
185
 
     * @param string  $reportFile  Report file to generate.
186
 
     * @param integer $reportWidth Report max width.
187
 
     * 
188
 
     * @return integer
189
 
     */
190
 
    public function printReport(
191
 
        $report,
192
 
        $showSources,
193
 
        $reportFile='',
194
 
        $reportWidth=80
195
 
    ) {
196
 
        $reportClass = self::factory($report);
197
 
 
198
 
        if ($reportFile !== null) {
199
 
            $filename = $reportFile;
200
 
            $toScreen = false;
201
 
            ob_start();
202
 
 
203
 
            if (file_exists($filename) === true) {
204
 
                $reportCache = file_get_contents($filename);
205
 
            } else {
206
 
                $reportCache = '';
207
 
            }
208
 
        } else {
209
 
            if (isset($this->_tmpFiles[$report]) === true) {
210
 
                $data        = stream_get_meta_data($this->_tmpFiles[$report]);
211
 
                $filename    = $data['uri'];
212
 
                $reportCache = file_get_contents($filename);
213
 
                fclose($this->_tmpFiles[$report]);
214
 
            } else {
215
 
                $reportCache = '';
216
 
                $filename    = null;
217
 
            }
218
 
 
219
 
            $toScreen = true;
220
 
        }
221
 
 
222
 
        $reportClass->generate(
223
 
            $reportCache,
224
 
            $this->totalFiles,
225
 
            $this->totalErrors,
226
 
            $this->totalWarnings,
227
 
            $showSources,
228
 
            $reportWidth,
229
 
            $toScreen
230
 
        );
231
 
 
232
 
        if ($reportFile !== null) {
233
 
            $generatedReport = ob_get_contents();
234
 
            ob_end_clean();
235
 
 
236
 
            if (PHP_CODESNIFFER_VERBOSITY > 0) {
237
 
                echo $generatedReport;
238
 
            }
239
 
 
240
 
            $generatedReport = trim($generatedReport);
241
 
            file_put_contents($reportFile, $generatedReport.PHP_EOL);
242
 
        } else if ($filename !== null && file_exists($filename) === true) {
243
 
            unlink($filename);
244
 
        }
245
 
 
246
 
        return ($this->totalErrors + $this->totalWarnings);
247
 
 
248
 
    }//end printReport()
249
 
 
250
 
 
251
 
    /**
252
 
     * Pre-process and package violations for all files.
253
 
     *
254
 
     * Used by error reports to get a packaged list of all errors in each file.
255
 
     *
256
 
     * @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
257
 
     *
258
 
     * @return array
259
 
     */
260
 
    public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
261
 
    {
262
 
        $report = array(
263
 
                   'filename' => $phpcsFile->getFilename(),
264
 
                   'errors'   => $phpcsFile->getErrorCount(),
265
 
                   'warnings' => $phpcsFile->getWarningCount(),
266
 
                   'messages' => array(),
267
 
                  );
268
 
 
269
 
        if ($report['errors'] === 0 && $report['warnings'] === 0) {
270
 
            // Prefect score!
271
 
            return $report;
272
 
        }
273
 
 
274
 
        $errors = array();
275
 
 
276
 
        // Merge errors and warnings.
277
 
        foreach ($phpcsFile->getErrors() as $line => $lineErrors) {
278
 
            if (is_array($lineErrors) === false) {
279
 
                continue;
280
 
            }
281
 
 
282
 
            foreach ($lineErrors as $column => $colErrors) {
283
 
                $newErrors = array();
284
 
                foreach ($colErrors as $data) {
285
 
                    $newErrors[] = array(
286
 
                                    'message'  => $data['message'],
287
 
                                    'source'   => $data['source'],
288
 
                                    'severity' => $data['severity'],
289
 
                                    'type'     => 'ERROR',
290
 
                                   );
291
 
                }//end foreach
292
 
 
293
 
                $errors[$line][$column] = $newErrors;
294
 
            }//end foreach
295
 
 
296
 
            ksort($errors[$line]);
297
 
        }//end foreach
298
 
 
299
 
        foreach ($phpcsFile->getWarnings() as $line => $lineWarnings) {
300
 
            if (is_array($lineWarnings) === false) {
301
 
                continue;
302
 
            }
303
 
 
304
 
            foreach ($lineWarnings as $column => $colWarnings) {
305
 
                $newWarnings = array();
306
 
                foreach ($colWarnings as $data) {
307
 
                    $newWarnings[] = array(
308
 
                                      'message'  => $data['message'],
309
 
                                      'source'   => $data['source'],
310
 
                                      'severity' => $data['severity'],
311
 
                                      'type'     => 'WARNING',
312
 
                                     );
313
 
                }//end foreach
314
 
 
315
 
                if (isset($errors[$line]) === false) {
316
 
                    $errors[$line] = array();
317
 
                }
318
 
 
319
 
                if (isset($errors[$line][$column]) === true) {
320
 
                    $errors[$line][$column] = array_merge(
321
 
                        $newWarnings,
322
 
                        $errors[$line][$column]
323
 
                    );
324
 
                } else {
325
 
                    $errors[$line][$column] = $newWarnings;
326
 
                }
327
 
            }//end foreach
328
 
 
329
 
            ksort($errors[$line]);
330
 
        }//end foreach
331
 
 
332
 
        ksort($errors);
333
 
        $report['messages'] = $errors;
334
 
        return $report;
335
 
 
336
 
    }//end prepareFileReport()
337
 
 
338
 
 
339
 
}//end class
340
 
 
341
 
?>