1<?php
2/**
3 * CBF report for PHP_CodeSniffer.
4 *
5 * This report implements the various auto-fixing features of the
6 * PHPCBF script and is not intended (or allowed) to be selected as a
7 * report from the command line.
8 *
9 * PHP version 5
10 *
11 * @category  PHP
12 * @package   PHP_CodeSniffer
13 * @author    Greg Sherwood <gsherwood@squiz.net>
14 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
15 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
16 * @link      http://pear.php.net/package/PHP_CodeSniffer
17 */
18
19/**
20 * CBF report for PHP_CodeSniffer.
21 *
22 * This report implements the various auto-fixing features of the
23 * PHPCBF script and is not intended (or allowed) to be selected as a
24 * report from the command line.
25 *
26 * PHP version 5
27 *
28 * @category  PHP
29 * @package   PHP_CodeSniffer
30 * @author    Greg Sherwood <gsherwood@squiz.net>
31 * @copyright 2006-2012 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: @package_version@
34 * @link      http://pear.php.net/package/PHP_CodeSniffer
35 */
36class PHP_CodeSniffer_Reports_Cbf implements PHP_CodeSniffer_Report
37{
38
39
40    /**
41     * Generate a partial report for a single processed file.
42     *
43     * Function should return TRUE if it printed or stored data about the file
44     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
45     * its data should be counted in the grand totals.
46     *
47     * @param array                $report      Prepared report data.
48     * @param PHP_CodeSniffer_File $phpcsFile   The file being reported on.
49     * @param boolean              $showSources Show sources?
50     * @param int                  $width       Maximum allowed line width.
51     *
52     * @return boolean
53     */
54    public function generateFileReport(
55        $report,
56        PHP_CodeSniffer_File $phpcsFile,
57        $showSources=false,
58        $width=80
59    ) {
60        $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues();
61        $errors    = $phpcsFile->getFixableCount();
62        if ($errors !== 0) {
63            if (empty($cliValues['files']) === false) {
64                ob_end_clean();
65                $errors    = $phpcsFile->getFixableCount();
66                $startTime = microtime(true);
67                echo "\t=> Fixing file: $errors/$errors violations remaining";
68            }
69
70            $fixed = $phpcsFile->fixer->fixFile();
71        }
72
73        if (empty($cliValues['files']) === true) {
74            // Replacing STDIN, so output current file to STDOUT
75            // even if nothing was fixed. Exit here because we
76            // can't process any more than 1 file in this setup.
77            echo $phpcsFile->fixer->getContents();
78            ob_end_flush();
79            exit(1);
80        }
81
82        if ($errors === 0) {
83            return false;
84        }
85
86        if ($fixed === false) {
87            echo 'ERROR';
88        } else {
89            echo 'DONE';
90        }
91
92        $timeTaken = ((microtime(true) - $startTime) * 1000);
93        if ($timeTaken < 1000) {
94            $timeTaken = round($timeTaken);
95            echo " in {$timeTaken}ms".PHP_EOL;
96        } else {
97            $timeTaken = round(($timeTaken / 1000), 2);
98            echo " in $timeTaken secs".PHP_EOL;
99        }
100
101        if ($fixed === true) {
102            $newFilename = $report['filename'].$cliValues['phpcbf-suffix'];
103            $newContent  = $phpcsFile->fixer->getContents();
104            file_put_contents($newFilename, $newContent);
105
106            if ($newFilename === $report['filename']) {
107                echo "\t=> File was overwritten".PHP_EOL;
108            } else {
109                echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL;
110            }
111        }
112
113        ob_start();
114
115        return $fixed;
116
117    }//end generateFileReport()
118
119
120    /**
121     * Prints all errors and warnings for each file processed.
122     *
123     * @param string  $cachedData    Any partial report data that was returned from
124     *                               generateFileReport during the run.
125     * @param int     $totalFiles    Total number of files processed during the run.
126     * @param int     $totalErrors   Total number of errors found during the run.
127     * @param int     $totalWarnings Total number of warnings found during the run.
128     * @param int     $totalFixable  Total number of problems that can be fixed.
129     * @param boolean $showSources   Show sources?
130     * @param int     $width         Maximum allowed line width.
131     * @param boolean $toScreen      Is the report being printed to screen?
132     *
133     * @return void
134     */
135    public function generate(
136        $cachedData,
137        $totalFiles,
138        $totalErrors,
139        $totalWarnings,
140        $totalFixable,
141        $showSources=false,
142        $width=80,
143        $toScreen=true
144    ) {
145        echo $cachedData;
146        echo "Fixed $totalFiles files".PHP_EOL;
147
148    }//end generate()
149
150
151}//end class
152