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

PHPCSStandards / PHP_CodeSniffer / 14523511255

17 Apr 2025 07:39PM UTC coverage: 77.921% (-0.1%) from 78.02%
14523511255

Pull #1020

github

web-flow
Merge 121c58cd4 into f78bb64bd
Pull Request #1020: Tokenizer/PHP: namespaced names as single token, mirroring PHP 8.0+

203 of 211 new or added lines in 25 files covered. (96.21%)

6 existing lines in 3 files now uncovered.

19389 of 24883 relevant lines covered (77.92%)

85.62 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
                ob_end_clean();
×
46
                $startTime = microtime(true);
×
47
                $newlines  = 0;
×
48
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
×
49
                    $newlines = 1;
×
50
                }
51

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

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

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

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

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

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

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

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

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

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

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

118
        return $fixed;
×
119

120
    }//end generateFileReport()
121

122

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

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

162
            return;
×
163
        }
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

254
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
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

© 2026 Coveralls, Inc