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

PHPCSStandards / PHP_CodeSniffer / 9756730629

02 Jul 2024 07:25AM UTC coverage: 73.743%. Remained the same
9756730629

push

github

jrfnl
CS/QA: remove unused `foreach` keys from report code

As the array format is now documented, these unused variables are no longer needed for documentation purposes.

Co-authored-by: Klaus Purer <klaus.purer@protonmail.ch>

0 of 9 new or added lines in 4 files covered. (0.0%)

495 existing lines in 17 files now uncovered.

17949 of 24340 relevant lines covered (73.74%)

70.96 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\Common;
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
     */
UNCOV
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
                $suppressNewline = true;
×
47
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
×
UNCOV
48
                    $suppressNewline = false;
×
49
                }
50

UNCOV
51
                Common::forcePrintStatusMessage("=> Fixing file: $errors/$errors violations remaining", 1, $suppressNewline);
×
52
            }
53

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

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

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

69
        if (PHP_CODESNIFFER_VERBOSITY > 0) {
×
70
            if ($fixed === false) {
×
UNCOV
71
                Common::forcePrintStatusMessage('ERROR', 0, true);
×
72
            } else {
UNCOV
73
                Common::forcePrintStatusMessage('DONE', 0, true);
×
74
            }
75

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

UNCOV
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) {
×
UNCOV
92
                $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename;
×
93
            }
94

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

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

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

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

UNCOV
117
        return $fixed;
×
118

119
    }//end generateFileReport()
120

121

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

152
        if (empty($lines) === true) {
×
153
            echo PHP_EOL.'No fixable errors were found'.PHP_EOL;
×
UNCOV
154
            return;
×
155
        }
156

157
        $reportFiles = [];
×
158
        $maxLength   = 0;
×
159
        $totalFixed  = 0;
×
UNCOV
160
        $failures    = 0;
×
161

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

UNCOV
173
            $maxLength = max($maxLength, $fileLen);
×
174

UNCOV
175
            $totalFixed += $parts[4];
×
176

177
            if ($parts[3] > 0) {
×
UNCOV
178
                $failures++;
×
179
            }
180
        }
181

182
        $width = min($width, ($maxLength + 21));
×
UNCOV
183
        $width = max($width, 70);
×
184

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

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

UNCOV
197
            echo $file.str_repeat(' ', $padding).'  ';
×
198

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

UNCOV
204
            $remaining = ($data['errors'] + $data['warnings']);
×
205

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

213
            if ($remaining !== 0) {
×
UNCOV
214
                echo $remaining;
×
215
            } else {
UNCOV
216
                echo '0';
×
217
            }
218

UNCOV
219
            echo PHP_EOL;
×
220
        }//end foreach
221

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

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

UNCOV
234
        echo "\033[0m";
×
235

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

UNCOV
243
            echo "\033[0m";
×
244
        }
245

UNCOV
246
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
247

248
    }//end generate()
249

250

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