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

PHPCSStandards / PHP_CodeSniffer / 13476003750

22 Feb 2025 07:40PM UTC coverage: 77.325% (-0.007%) from 77.332%
13476003750

push

github

jrfnl
PHPBCF: Show `no errors were found` when there are no errors to fix. (#807)

Modifies the output of PHPCBF when the exiting without fixing any errors to show a message indicating that the reasons was due to no coding standards violations in the code base.

0 of 3 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

19179 of 24803 relevant lines covered (77.33%)

76.79 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\Common;
19

20
class Cbf implements Report
21
{
22

23

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

51
                Common::forcePrintStatusMessage("=> Fixing file: $errors/$errors violations remaining", 1, $suppressNewline);
×
52
            }
53

54
            $fixed = $phpcsFile->fixer->fixFile();
×
55
        }
56

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

65
        if ($errors === 0) {
×
66
            return false;
×
67
        }
68

69
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
70
            if ($fixed === false) {
×
71
                Common::forcePrintStatusMessage('ERROR', 0, true);
×
72
            } else {
73
                Common::forcePrintStatusMessage('DONE', 0, true);
×
74
            }
75

76
            $timeTaken = ((microtime(true) - $startTime) * 1000);
×
77
            if ($timeTaken < 1000) {
×
78
                $timeTaken = round($timeTaken);
×
79
                Common::forcePrintStatusMessage(" in {$timeTaken}ms");
×
80
            } else {
81
                $timeTaken = round(($timeTaken / 1000), 2);
×
82
                Common::forcePrintStatusMessage(" in $timeTaken secs");
×
83
            }
84
        }
85

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

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

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

107
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
108
            ob_start();
×
109
        }
110

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

117
        return $fixed;
×
118

119
    }//end generateFileReport()
120

121

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

152
        if (empty($lines) === true) {
×
NEW
153
            if (($totalErrors + $totalWarnings) === 0) {
×
NEW
154
                echo PHP_EOL.'No violations were found'.PHP_EOL;
×
155
            } else {
NEW
156
                echo PHP_EOL.'No fixable errors were found'.PHP_EOL;
×
157
            }
158

UNCOV
159
            return;
×
160
        }
161

162
        $reportFiles = [];
×
163
        $maxLength   = 0;
×
164
        $totalFixed  = 0;
×
165
        $failures    = 0;
×
166

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

178
            $maxLength = max($maxLength, $fileLen);
×
179

180
            $totalFixed += $parts[4];
×
181

182
            if ($parts[3] > 0) {
×
183
                $failures++;
×
184
            }
185
        }
186

187
        $width = min($width, ($maxLength + 21));
×
188
        $width = max($width, 70);
×
189

190
        echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL;
×
191
        echo str_repeat('-', $width).PHP_EOL;
×
192
        echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED  REMAINING'."\033[0m".PHP_EOL;
×
193
        echo str_repeat('-', $width).PHP_EOL;
×
194

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

202
            echo $file.str_repeat(' ', $padding).'  ';
×
203

204
            if ($data['fixable'] > 0) {
×
205
                echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL;
×
206
                continue;
×
207
            }
208

209
            $remaining = ($data['errors'] + $data['warnings']);
×
210

211
            if ($data['fixed'] !== 0) {
×
212
                echo $data['fixed'];
×
213
                echo str_repeat(' ', (7 - strlen((string) $data['fixed'])));
×
214
            } else {
215
                echo '0      ';
×
216
            }
217

218
            if ($remaining !== 0) {
×
219
                echo $remaining;
×
220
            } else {
221
                echo '0';
×
222
            }
223

224
            echo PHP_EOL;
×
225
        }//end foreach
226

227
        echo str_repeat('-', $width).PHP_EOL;
×
228
        echo "\033[1mA TOTAL OF $totalFixed ERROR";
×
229
        if ($totalFixed !== 1) {
×
230
            echo 'S';
×
231
        }
232

233
        $numFiles = count($reportFiles);
×
234
        echo ' WERE FIXED IN '.$numFiles.' FILE';
×
235
        if ($numFiles !== 1) {
×
236
            echo 'S';
×
237
        }
238

239
        echo "\033[0m";
×
240

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

248
            echo "\033[0m";
×
249
        }
250

251
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
252

253
    }//end generate()
254

255

256
}//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