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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 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\ExitCode;
19
use PHP_CodeSniffer\Util\Timing;
20
use PHP_CodeSniffer\Util\Writers\StatusWriter;
21

22
class Cbf implements Report
23
{
24

25

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

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, ExitCode::OKAY);
×
65
        }
66

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

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

78
            StatusWriter::forceWrite(' in '.Timing::getHumanReadableDuration(Timing::getDurationSince($startTime)));
×
79
        }
80

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

90
            $newContent = $phpcsFile->fixer->getContents();
×
91
            file_put_contents($newFilename, $newContent);
×
92

93
            if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
94
                if ($newFilename === $report['filename']) {
×
95
                    StatusWriter::forceWrite('=> File was overwritten', 1);
×
96
                } else {
97
                    StatusWriter::forceWrite('=> Fixed file written to '.basename($newFilename), 1);
×
98
                }
99
            }
100
        }
101

102
        $errorCount   = $phpcsFile->getErrorCount();
×
103
        $warningCount = $phpcsFile->getWarningCount();
×
104
        $fixableCount = $phpcsFile->getFixableCount();
×
105
        $fixedCount   = ($errors - $fixableCount);
×
106
        echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL;
×
107

108
        return $fixed;
×
109

110
    }//end generateFileReport()
111

112

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

143
        if (empty($lines) === true) {
×
144
            if (($totalErrors + $totalWarnings) === 0) {
×
145
                StatusWriter::writeNewline();
×
146
                StatusWriter::write('No violations were found');
×
147
            } else {
148
                StatusWriter::writeNewline();
×
149
                StatusWriter::write('No fixable errors were found');
×
150
            }
151

152
            return;
×
153
        }
154

155
        $reportFiles = [];
×
156
        $maxLength   = 0;
×
157
        $totalFixed  = 0;
×
158
        $failures    = 0;
×
159

160
        foreach ($lines as $line) {
×
161
            $parts   = explode('>>', $line);
×
162
            $fileLen = strlen($parts[0]);
×
163
            $reportFiles[$parts[0]] = [
×
164
                'errors'   => $parts[1],
×
165
                'warnings' => $parts[2],
×
166
                'fixable'  => $parts[3],
×
167
                'fixed'    => $parts[4],
×
168
                'strlen'   => $fileLen,
×
169
            ];
170

171
            $maxLength = max($maxLength, $fileLen);
×
172

173
            $totalFixed += $parts[4];
×
174

175
            if ($parts[3] > 0) {
×
176
                $failures++;
×
177
            }
178
        }
179

180
        $width = min($width, ($maxLength + 21));
×
181
        $width = max($width, 70);
×
182

183
        echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL;
×
184
        echo str_repeat('-', $width).PHP_EOL;
×
185
        echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED  REMAINING'."\033[0m".PHP_EOL;
×
186
        echo str_repeat('-', $width).PHP_EOL;
×
187

188
        foreach ($reportFiles as $file => $data) {
×
189
            $padding = ($width - 18 - $data['strlen']);
×
190
            if ($padding < 0) {
×
191
                $file    = '...'.substr($file, (($padding * -1) + 3));
×
192
                $padding = 0;
×
193
            }
194

195
            echo $file.str_repeat(' ', $padding).'  ';
×
196

197
            if ($data['fixable'] > 0) {
×
198
                echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL;
×
199
                continue;
×
200
            }
201

202
            $remaining = ($data['errors'] + $data['warnings']);
×
203

204
            if ($data['fixed'] !== 0) {
×
205
                echo $data['fixed'];
×
206
                echo str_repeat(' ', (7 - strlen((string) $data['fixed'])));
×
207
            } else {
208
                echo '0      ';
×
209
            }
210

211
            if ($remaining !== 0) {
×
212
                echo $remaining;
×
213
            } else {
214
                echo '0';
×
215
            }
216

217
            echo PHP_EOL;
×
218
        }//end foreach
219

220
        echo str_repeat('-', $width).PHP_EOL;
×
221
        echo "\033[1mA TOTAL OF $totalFixed ERROR";
×
222
        if ($totalFixed !== 1) {
×
223
            echo 'S';
×
224
        }
225

226
        $numFiles = count($reportFiles);
×
227
        echo ' WERE FIXED IN '.$numFiles.' FILE';
×
228
        if ($numFiles !== 1) {
×
229
            echo 'S';
×
230
        }
231

232
        echo "\033[0m";
×
233

234
        if ($failures > 0) {
×
235
            echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
×
236
            echo "\033[1mPHPCBF FAILED TO FIX $failures FILE";
×
237
            if ($failures !== 1) {
×
238
                echo 'S';
×
239
            }
240

241
            echo "\033[0m";
×
242
        }
243

244
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
245

246
    }//end generate()
247

248

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