• 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/Full.php
1
<?php
2
/**
3
 * Full report for PHP_CodeSniffer.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Reports;
11

12
use PHP_CodeSniffer\Files\File;
13

14
class Full implements Report
15
{
16

17

18
    /**
19
     * Generate a partial report for a single processed file.
20
     *
21
     * Function should return TRUE if it printed or stored data about the file
22
     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
23
     * its data should be counted in the grand totals.
24
     *
25
     * @param array<string, string|int|array> $report      Prepared report data.
26
     *                                                     See the {@see Report} interface for a detailed specification.
27
     * @param \PHP_CodeSniffer\Files\File     $phpcsFile   The file being reported on.
28
     * @param bool                            $showSources Show sources?
29
     * @param int                             $width       Maximum allowed line width.
30
     *
31
     * @return bool
32
     */
33
    public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
×
34
    {
35
        if ($report['errors'] === 0 && $report['warnings'] === 0) {
×
36
            // Nothing to print.
37
            return false;
×
38
        }
39

40
        // The length of the word ERROR or WARNING; used for padding.
41
        if ($report['warnings'] > 0) {
×
42
            $typeLength = 7;
×
43
        } else {
44
            $typeLength = 5;
×
45
        }
46

47
        // Work out the max line number length for formatting.
48
        $maxLineNumLength = max(array_map('strlen', array_keys($report['messages'])));
×
49

50
        // The padding that all lines will require that are
51
        // printing an error message overflow.
52
        $paddingLine2  = str_repeat(' ', ($maxLineNumLength + 1));
×
53
        $paddingLine2 .= ' | ';
×
54
        $paddingLine2 .= str_repeat(' ', $typeLength);
×
55
        $paddingLine2 .= ' | ';
×
56
        if ($report['fixable'] > 0) {
×
57
            $paddingLine2 .= '    ';
×
58
        }
59

60
        $paddingLength = strlen($paddingLine2);
×
61

62
        // Make sure the report width isn't too big.
63
        $maxErrorLength = 0;
×
64
        foreach ($report['messages'] as $lineErrors) {
×
65
            foreach ($lineErrors as $colErrors) {
×
66
                foreach ($colErrors as $error) {
×
67
                    // Start with the presumption of a single line error message.
68
                    $length    = strlen($error['message']);
×
69
                    $srcLength = (strlen($error['source']) + 3);
×
70
                    if ($showSources === true) {
×
71
                        $length += $srcLength;
×
72
                    }
73

74
                    // ... but also handle multi-line messages correctly.
75
                    if (strpos($error['message'], "\n") !== false) {
×
76
                        $errorLines = explode("\n", $error['message']);
×
77
                        $length     = max(array_map('strlen', $errorLines));
×
78

79
                        if ($showSources === true) {
×
80
                            $lastLine = array_pop($errorLines);
×
81
                            $length   = max($length, (strlen($lastLine) + $srcLength));
×
82
                        }
83
                    }
84

85
                    $maxErrorLength = max($maxErrorLength, ($length + 1));
×
86
                }//end foreach
87
            }//end foreach
88
        }//end foreach
89

90
        $file       = $report['filename'];
×
91
        $fileLength = strlen($file);
×
92
        $maxWidth   = max(($fileLength + 6), ($maxErrorLength + $paddingLength));
×
93
        $width      = min($width, $maxWidth);
×
94
        if ($width < 70) {
×
95
            $width = 70;
×
96
        }
97

98
        echo PHP_EOL."\033[1mFILE: ";
×
99
        if ($fileLength <= ($width - 6)) {
×
100
            echo $file;
×
101
        } else {
102
            echo '...'.substr($file, ($fileLength - ($width - 6)));
×
103
        }
104

105
        echo "\033[0m".PHP_EOL;
×
106
        echo str_repeat('-', $width).PHP_EOL;
×
107

108
        echo "\033[1m".'FOUND '.$report['errors'].' ERROR';
×
109
        if ($report['errors'] !== 1) {
×
110
            echo 'S';
×
111
        }
112

113
        if ($report['warnings'] > 0) {
×
114
            echo ' AND '.$report['warnings'].' WARNING';
×
115
            if ($report['warnings'] !== 1) {
×
116
                echo 'S';
×
117
            }
118
        }
119

120
        echo ' AFFECTING '.count($report['messages']).' LINE';
×
121
        if (count($report['messages']) !== 1) {
×
122
            echo 'S';
×
123
        }
124

125
        echo "\033[0m".PHP_EOL;
×
126
        echo str_repeat('-', $width).PHP_EOL;
×
127

128
        // The maximum amount of space an error message can use.
129
        $maxErrorSpace = ($width - $paddingLength - 1);
×
130

131
        $beforeMsg = '';
×
132
        $afterMsg  = '';
×
133
        if ($showSources === true) {
×
134
            $beforeMsg = "\033[1m";
×
135
            $afterMsg  = "\033[0m";
×
136
        }
137

138
        $beforeAfterLength = strlen($beforeMsg.$afterMsg);
×
139

140
        foreach ($report['messages'] as $line => $lineErrors) {
×
141
            foreach ($lineErrors as $colErrors) {
×
142
                foreach ($colErrors as $error) {
×
143
                    $errorMsg = wordwrap(
×
144
                        $error['message'],
×
145
                        $maxErrorSpace
×
146
                    );
147

148
                    // Add the padding _after_ the wordwrap as the message itself may contain line breaks
149
                    // and those lines will also need to receive padding.
150
                    $errorMsg = str_replace("\n", $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg, $errorMsg);
×
151
                    $errorMsg = $beforeMsg.$errorMsg.$afterMsg;
×
152

153
                    if ($showSources === true) {
×
154
                        $lastMsg          = $errorMsg;
×
155
                        $startPosLastLine = strrpos($errorMsg, PHP_EOL.$paddingLine2.$beforeMsg);
×
156
                        if ($startPosLastLine !== false) {
×
157
                            // Message is multiline. Grab the text of last line of the message, including the color codes.
158
                            $lastMsg = substr($errorMsg, ($startPosLastLine + strlen(PHP_EOL.$paddingLine2)));
×
159
                        }
160

161
                        // When show sources is used, the message itself will be bolded, so we need to correct the length.
162
                        $sourceSuffix = '('.$error['source'].')';
×
163

164
                        $lastMsgPlusSourceLength = strlen($lastMsg);
×
165
                        // Add space + source suffix length.
166
                        $lastMsgPlusSourceLength += (1 + strlen($sourceSuffix));
×
167
                        // Correct for the color codes.
168
                        $lastMsgPlusSourceLength -= $beforeAfterLength;
×
169

170
                        if ($lastMsgPlusSourceLength > $maxErrorSpace) {
×
171
                            $errorMsg .= PHP_EOL.$paddingLine2.$sourceSuffix;
×
172
                        } else {
173
                            $errorMsg .= ' '.$sourceSuffix;
×
174
                        }
175
                    }//end if
176

177
                    // The padding that goes on the front of the line.
178
                    $padding = ($maxLineNumLength - strlen($line));
×
179

180
                    echo ' '.str_repeat(' ', $padding).$line.' | ';
×
181
                    if ($error['type'] === 'ERROR') {
×
182
                        echo "\033[31mERROR\033[0m";
×
183
                        if ($report['warnings'] > 0) {
×
184
                            echo '  ';
×
185
                        }
186
                    } else {
187
                        echo "\033[33mWARNING\033[0m";
×
188
                    }
189

190
                    echo ' | ';
×
191
                    if ($report['fixable'] > 0) {
×
192
                        echo '[';
×
193
                        if ($error['fixable'] === true) {
×
194
                            echo 'x';
×
195
                        } else {
196
                            echo ' ';
×
197
                        }
198

199
                        echo '] ';
×
200
                    }
201

202
                    echo $errorMsg.PHP_EOL;
×
203
                }//end foreach
204
            }//end foreach
205
        }//end foreach
206

207
        echo str_repeat('-', $width).PHP_EOL;
×
208
        if ($report['fixable'] > 0) {
×
209
            echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL;
×
210
            echo str_repeat('-', $width).PHP_EOL;
×
211
        }
212

213
        echo PHP_EOL;
×
214
        return true;
×
215

216
    }//end generateFileReport()
217

218

219
    /**
220
     * Prints all errors and warnings for each file processed.
221
     *
222
     * @param string $cachedData    Any partial report data that was returned from
223
     *                              generateFileReport during the run.
224
     * @param int    $totalFiles    Total number of files processed during the run.
225
     * @param int    $totalErrors   Total number of errors found during the run.
226
     * @param int    $totalWarnings Total number of warnings found during the run.
227
     * @param int    $totalFixable  Total number of problems that can be fixed.
228
     * @param bool   $showSources   Show sources?
229
     * @param int    $width         Maximum allowed line width.
230
     * @param bool   $interactive   Are we running in interactive mode?
231
     * @param bool   $toScreen      Is the report being printed to screen?
232
     *
233
     * @return void
234
     */
235
    public function generate(
×
236
        $cachedData,
237
        $totalFiles,
238
        $totalErrors,
239
        $totalWarnings,
240
        $totalFixable,
241
        $showSources=false,
242
        $width=80,
243
        $interactive=false,
244
        $toScreen=true
245
    ) {
246
        if ($cachedData === '') {
×
247
            return;
×
248
        }
249

250
        echo $cachedData;
×
251

252
    }//end generate()
253

254

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