• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 14516416464

17 Apr 2025 01:09PM UTC coverage: 77.945% (+0.3%) from 77.666%
14516416464

push

github

web-flow
Merge pull request #1010 from PHPCSStandards/phpcs-4.0/feature/sq-1612-stdout-vs-stderr

(Nearly) All status, debug, and progress output is now sent to STDERR instead of STDOUT

63 of 457 new or added lines in 18 files covered. (13.79%)

1 existing line in 1 file now uncovered.

19455 of 24960 relevant lines covered (77.94%)

78.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Reports/Cbf.php
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
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
 */
13

14
namespace PHP_CodeSniffer\Reports;
15

16
use PHP_CodeSniffer\Exceptions\DeepExitException;
17
use PHP_CodeSniffer\Files\File;
18
use PHP_CodeSniffer\Util\Timing;
19
use PHP_CodeSniffer\Util\Writers\StatusWriter;
20

21
class Cbf implements Report
22
{
23

24

25
    /**
26
     * Generate a partial report for a single processed file.
27
     *
28
     * Function should return TRUE if it printed or stored data about the file
29
     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
30
     * its data should be counted in the grand totals.
31
     *
32
     * @param array<string, string|int|array> $report      Prepared report data.
33
     *                                                     See the {@see Report} interface for a detailed specification.
34
     * @param \PHP_CodeSniffer\Files\File     $phpcsFile   The file being reported on.
35
     * @param bool                            $showSources Show sources?
36
     * @param int                             $width       Maximum allowed line width.
37
     *
38
     * @return bool
39
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
40
     */
41
    public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
×
42
    {
43
        $errors = $phpcsFile->getFixableCount();
×
44
        if ($errors !== 0) {
×
45
            if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
46
                ob_end_clean();
×
47
                $startTime = microtime(true);
×
NEW
48
                $newlines  = 0;
×
49
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
×
NEW
50
                    $newlines = 1;
×
51
                }
52

NEW
53
                StatusWriter::forceWrite("=> Fixing file: $errors/$errors violations remaining", 1, $newlines);
×
54
            }
55

56
            $fixed = $phpcsFile->fixer->fixFile();
×
57
        }
58

59
        if ($phpcsFile->config->stdin === true) {
×
60
            // Replacing STDIN, so output current file to STDOUT
61
            // even if nothing was fixed. Exit here because we
62
            // can't process any more than 1 file in this setup.
63
            $fixedContent = $phpcsFile->fixer->getContents();
×
64
            throw new DeepExitException($fixedContent, 1);
×
65
        }
66

67
        if ($errors === 0) {
×
68
            return false;
×
69
        }
70

71
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
72
            if ($fixed === false) {
×
NEW
73
                StatusWriter::forceWrite('ERROR', 0, 0);
×
74
            } else {
NEW
75
                StatusWriter::forceWrite('DONE', 0, 0);
×
76
            }
77

78
            $timeTaken = ((microtime(true) - $startTime) * 1000);
×
79
            if ($timeTaken < 1000) {
×
80
                $timeTaken = round($timeTaken);
×
NEW
81
                StatusWriter::forceWrite(" in {$timeTaken}ms");
×
82
            } else {
83
                $timeTaken = round(($timeTaken / 1000), 2);
×
NEW
84
                StatusWriter::forceWrite(" in $timeTaken secs");
×
85
            }
86
        }
87

88
        if ($fixed === true) {
×
89
            // The filename in the report may be truncated due to a basepath setting
90
            // but we are using it for writing here and not display,
91
            // so find the correct path if basepath is in use.
92
            $newFilename = $report['filename'].$phpcsFile->config->suffix;
×
93
            if ($phpcsFile->config->basepath !== null) {
×
94
                $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename;
×
95
            }
96

97
            $newContent = $phpcsFile->fixer->getContents();
×
98
            file_put_contents($newFilename, $newContent);
×
99

100
            if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
101
                if ($newFilename === $report['filename']) {
×
NEW
102
                    StatusWriter::forceWrite('=> File was overwritten', 1);
×
103
                } else {
NEW
104
                    StatusWriter::forceWrite('=> Fixed file written to '.basename($newFilename), 1);
×
105
                }
106
            }
107
        }
108

109
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
110
            ob_start();
×
111
        }
112

113
        $errorCount   = $phpcsFile->getErrorCount();
×
114
        $warningCount = $phpcsFile->getWarningCount();
×
115
        $fixableCount = $phpcsFile->getFixableCount();
×
116
        $fixedCount   = ($errors - $fixableCount);
×
117
        echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL;
×
118

119
        return $fixed;
×
120

121
    }//end generateFileReport()
122

123

124
    /**
125
     * Prints a summary of fixed files.
126
     *
127
     * @param string $cachedData    Any partial report data that was returned from
128
     *                              generateFileReport during the run.
129
     * @param int    $totalFiles    Total number of files processed during the run.
130
     * @param int    $totalErrors   Total number of errors found during the run.
131
     * @param int    $totalWarnings Total number of warnings found during the run.
132
     * @param int    $totalFixable  Total number of problems that can be fixed.
133
     * @param bool   $showSources   Show sources?
134
     * @param int    $width         Maximum allowed line width.
135
     * @param bool   $interactive   Are we running in interactive mode?
136
     * @param bool   $toScreen      Is the report being printed to screen?
137
     *
138
     * @return void
139
     */
140
    public function generate(
×
141
        $cachedData,
142
        $totalFiles,
143
        $totalErrors,
144
        $totalWarnings,
145
        $totalFixable,
146
        $showSources=false,
147
        $width=80,
148
        $interactive=false,
149
        $toScreen=true
150
    ) {
151
        $lines = explode(PHP_EOL, $cachedData);
×
152
        array_pop($lines);
×
153

154
        if (empty($lines) === true) {
×
155
            if (($totalErrors + $totalWarnings) === 0) {
×
NEW
156
                StatusWriter::writeNewline();
×
NEW
157
                StatusWriter::write('No violations were found');
×
158
            } else {
NEW
159
                StatusWriter::writeNewline();
×
NEW
160
                StatusWriter::write('No fixable errors were found');
×
161
            }
162

163
            return;
×
164
        }
165

166
        $reportFiles = [];
×
167
        $maxLength   = 0;
×
168
        $totalFixed  = 0;
×
169
        $failures    = 0;
×
170

171
        foreach ($lines as $line) {
×
172
            $parts   = explode('>>', $line);
×
173
            $fileLen = strlen($parts[0]);
×
174
            $reportFiles[$parts[0]] = [
×
175
                'errors'   => $parts[1],
×
176
                'warnings' => $parts[2],
×
177
                'fixable'  => $parts[3],
×
178
                'fixed'    => $parts[4],
×
179
                'strlen'   => $fileLen,
×
180
            ];
×
181

182
            $maxLength = max($maxLength, $fileLen);
×
183

184
            $totalFixed += $parts[4];
×
185

186
            if ($parts[3] > 0) {
×
187
                $failures++;
×
188
            }
189
        }
190

191
        $width = min($width, ($maxLength + 21));
×
192
        $width = max($width, 70);
×
193

194
        echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL;
×
195
        echo str_repeat('-', $width).PHP_EOL;
×
196
        echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED  REMAINING'."\033[0m".PHP_EOL;
×
197
        echo str_repeat('-', $width).PHP_EOL;
×
198

199
        foreach ($reportFiles as $file => $data) {
×
200
            $padding = ($width - 18 - $data['strlen']);
×
201
            if ($padding < 0) {
×
202
                $file    = '...'.substr($file, (($padding * -1) + 3));
×
203
                $padding = 0;
×
204
            }
205

206
            echo $file.str_repeat(' ', $padding).'  ';
×
207

208
            if ($data['fixable'] > 0) {
×
209
                echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL;
×
210
                continue;
×
211
            }
212

213
            $remaining = ($data['errors'] + $data['warnings']);
×
214

215
            if ($data['fixed'] !== 0) {
×
216
                echo $data['fixed'];
×
217
                echo str_repeat(' ', (7 - strlen((string) $data['fixed'])));
×
218
            } else {
219
                echo '0      ';
×
220
            }
221

222
            if ($remaining !== 0) {
×
223
                echo $remaining;
×
224
            } else {
225
                echo '0';
×
226
            }
227

228
            echo PHP_EOL;
×
229
        }//end foreach
230

231
        echo str_repeat('-', $width).PHP_EOL;
×
232
        echo "\033[1mA TOTAL OF $totalFixed ERROR";
×
233
        if ($totalFixed !== 1) {
×
234
            echo 'S';
×
235
        }
236

237
        $numFiles = count($reportFiles);
×
238
        echo ' WERE FIXED IN '.$numFiles.' FILE';
×
239
        if ($numFiles !== 1) {
×
240
            echo 'S';
×
241
        }
242

243
        echo "\033[0m";
×
244

245
        if ($failures > 0) {
×
246
            echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
×
247
            echo "\033[1mPHPCBF FAILED TO FIX $failures FILE";
×
248
            if ($failures !== 1) {
×
249
                echo 'S';
×
250
            }
251

252
            echo "\033[0m";
×
253
        }
254

255
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
256

257
        if ($toScreen === true && $interactive === false) {
×
258
            Timing::printRunTime();
×
259
        }
260

261
    }//end generate()
262

263

264
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc