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