1<?php
2/**
3 * Diff report for PHP_CodeSniffer.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Diff report for PHP_CodeSniffer.
17 *
18 * PHP version 5
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version   Release: @package_version@
26 * @link      http://pear.php.net/package/PHP_CodeSniffer
27 */
28class PHP_CodeSniffer_Reports_Diff implements PHP_CodeSniffer_Report
29{
30
31
32    /**
33     * Generate a partial report for a single processed file.
34     *
35     * Function should return TRUE if it printed or stored data about the file
36     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
37     * its data should be counted in the grand totals.
38     *
39     * @param array                $report      Prepared report data.
40     * @param PHP_CodeSniffer_File $phpcsFile   The file being reported on.
41     * @param boolean              $showSources Show sources?
42     * @param int                  $width       Maximum allowed line width.
43     *
44     * @return boolean
45     */
46    public function generateFileReport(
47        $report,
48        PHP_CodeSniffer_File $phpcsFile,
49        $showSources=false,
50        $width=80
51    ) {
52        $errors = $phpcsFile->getFixableCount();
53        if ($errors === 0) {
54            return false;
55        }
56
57        if (PHP_CODESNIFFER_VERBOSITY > 1) {
58            ob_end_clean();
59            echo "\t*** START FILE FIXING ***".PHP_EOL;
60        }
61
62        if (PHP_CODESNIFFER_CBF === true) {
63            ob_end_clean();
64            $startTime = microtime(true);
65            echo "\t=> Fixing file: $errors/$errors violations remaining";
66        }
67
68        $fixed = $phpcsFile->fixer->fixFile();
69
70        if (PHP_CODESNIFFER_CBF === true) {
71            if ($fixed === false) {
72                echo "\033[31mERROR\033[0m";
73            } else {
74                echo "\033[32mDONE\033[0m";
75            }
76
77            $timeTaken = ((microtime(true) - $startTime) * 1000);
78            if ($timeTaken < 1000) {
79                $timeTaken = round($timeTaken);
80                echo " in {$timeTaken}ms".PHP_EOL;
81            } else {
82                $timeTaken = round(($timeTaken / 1000), 2);
83                echo " in $timeTaken secs".PHP_EOL;
84            }
85
86            ob_start();
87        }
88
89        if (PHP_CODESNIFFER_VERBOSITY > 1) {
90            echo "\t*** END FILE FIXING ***".PHP_EOL;
91            ob_start();
92        }
93
94        if ($fixed === false) {
95            return false;
96        }
97
98        if (PHP_CODESNIFFER_CBF === true) {
99            // Diff without colours.
100            $diff = $phpcsFile->fixer->generateDiff(null, false);
101        } else {
102            $diff = $phpcsFile->fixer->generateDiff();
103        }
104
105        if ($diff === '') {
106            // Nothing to print.
107            return false;
108        }
109
110        echo $diff.PHP_EOL;
111        return true;
112
113    }//end generateFileReport()
114
115
116    /**
117     * Prints all errors and warnings for each file processed.
118     *
119     * @param string  $cachedData    Any partial report data that was returned from
120     *                               generateFileReport during the run.
121     * @param int     $totalFiles    Total number of files processed during the run.
122     * @param int     $totalErrors   Total number of errors found during the run.
123     * @param int     $totalWarnings Total number of warnings found during the run.
124     * @param int     $totalFixable  Total number of problems that can be fixed.
125     * @param boolean $showSources   Show sources?
126     * @param int     $width         Maximum allowed line width.
127     * @param boolean $toScreen      Is the report being printed to screen?
128     *
129     * @return void
130     */
131    public function generate(
132        $cachedData,
133        $totalFiles,
134        $totalErrors,
135        $totalWarnings,
136        $totalFixable,
137        $showSources=false,
138        $width=80,
139        $toScreen=true
140    ) {
141        echo $cachedData;
142        if ($toScreen === true) {
143            echo PHP_EOL;
144        }
145
146    }//end generate()
147
148
149}//end class
150