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

PHPCSStandards / PHP_CodeSniffer / 14777403124

01 May 2025 02:46PM UTC coverage: 78.471% (+0.02%) from 78.454%
14777403124

Pull #1068

github

web-flow
Merge 3d54d4c60 into 19d4bb855
Pull Request #1068: Config: remove more test specific conditions

5 of 9 new or added lines in 1 file covered. (55.56%)

2 existing lines in 1 file now uncovered.

19566 of 24934 relevant lines covered (78.47%)

86.19 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\Writers\StatusWriter;
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
                $newlines  = 0;
×
47
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
×
48
                    $newlines = 1;
×
49
                }
50

51
                StatusWriter::forceWrite("=> Fixing file: $errors/$errors violations remaining", 1, $newlines);
×
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
                StatusWriter::forceWrite('ERROR', 0, 0);
×
72
            } else {
73
                StatusWriter::forceWrite('DONE', 0, 0);
×
74
            }
75

76
            $timeTaken = ((microtime(true) - $startTime) * 1000);
×
77
            if ($timeTaken < 1000) {
×
78
                $timeTaken = round($timeTaken);
×
79
                StatusWriter::forceWrite(" in {$timeTaken}ms");
×
80
            } else {
81
                $timeTaken = round(($timeTaken / 1000), 2);
×
82
                StatusWriter::forceWrite(" 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
                    StatusWriter::forceWrite('=> File was overwritten', 1);
×
101
                } else {
102
                    StatusWriter::forceWrite('=> Fixed file written to '.basename($newFilename), 1);
×
103
                }
104
            }
105
        }
106

107
        $errorCount   = $phpcsFile->getErrorCount();
×
108
        $warningCount = $phpcsFile->getWarningCount();
×
109
        $fixableCount = $phpcsFile->getFixableCount();
×
110
        $fixedCount   = ($errors - $fixableCount);
×
111
        echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL;
×
112

113
        return $fixed;
×
114

115
    }//end generateFileReport()
116

117

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

148
        if (empty($lines) === true) {
×
149
            if (($totalErrors + $totalWarnings) === 0) {
×
150
                StatusWriter::writeNewline();
×
151
                StatusWriter::write('No violations were found');
×
152
            } else {
153
                StatusWriter::writeNewline();
×
154
                StatusWriter::write('No fixable errors were found');
×
155
            }
156

157
            return;
×
158
        }
159

160
        $reportFiles = [];
×
161
        $maxLength   = 0;
×
162
        $totalFixed  = 0;
×
163
        $failures    = 0;
×
164

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

176
            $maxLength = max($maxLength, $fileLen);
×
177

178
            $totalFixed += $parts[4];
×
179

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

185
        $width = min($width, ($maxLength + 21));
×
186
        $width = max($width, 70);
×
187

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

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

200
            echo $file.str_repeat(' ', $padding).'  ';
×
201

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

207
            $remaining = ($data['errors'] + $data['warnings']);
×
208

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

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

222
            echo PHP_EOL;
×
223
        }//end foreach
224

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

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

237
        echo "\033[0m";
×
238

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

246
            echo "\033[0m";
×
247
        }
248

249
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
250

251
    }//end generate()
252

253

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