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

PHPCSStandards / PHP_CodeSniffer / 14434600573

14 Apr 2025 12:01AM UTC coverage: 78.902%. Remained the same
14434600573

Pull #967

github

web-flow
Merge e2f817c09 into 7925a214c
Pull Request #967: PrintProgressDotsTest: fix up param names in data sets

24963 of 31638 relevant lines covered (78.9%)

68.08 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

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
                ob_end_clean();
×
46
                $startTime = microtime(true);
×
47
                echo "\t=> Fixing file: $errors/$errors violations remaining";
×
48
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
×
49
                    echo PHP_EOL;
×
50
                }
51
            }
52

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

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

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

68
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
69
            if ($fixed === false) {
×
70
                echo 'ERROR';
×
71
            } else {
72
                echo 'DONE';
×
73
            }
74

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

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

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

97
            if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
98
                if ($newFilename === $report['filename']) {
×
99
                    echo "\t=> File was overwritten".PHP_EOL;
×
100
                } else {
101
                    echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL;
×
102
                }
103
            }
104
        }
105

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

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

116
        return $fixed;
×
117

118
    }//end generateFileReport()
119

120

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

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

158
            return;
×
159
        }
160

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
        if ($toScreen === true && $interactive === false) {
×
253
            Timing::printRunTime();
×
254
        }
255

256
    }//end generate()
257

258

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

© 2025 Coveralls, Inc