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

PHPCSStandards / PHP_CodeSniffer / 14866218365

06 May 2025 05:43PM UTC coverage: 78.62% (+0.1%) from 78.472%
14866218365

Pull #123

github

web-flow
Merge 0ff1f1188 into 292322bf2
Pull Request #123: Improve handling of disable/enable/ignore directives

77 of 78 new or added lines in 3 files covered. (98.72%)

180 existing lines in 4 files now uncovered.

19622 of 24958 relevant lines covered (78.62%)

86.23 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
use PHP_CodeSniffer\Util\Writers\StatusWriter;
20

21
class Cbf implements Report
22
{
23

24

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

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

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

UNCOV
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();
×
UNCOV
63
            throw new DeepExitException($fixedContent, 1);
×
64
        }
65

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

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

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

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

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

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

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

107
        return $fixed;
×
108

109
    }//end generateFileReport()
110

111

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

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

151
            return;
×
152
        }
153

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

245
    }//end generate()
246

247

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