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

PHPCSStandards / PHP_CodeSniffer / 9756688221

02 Jul 2024 07:22AM UTC coverage: 75.507%. Remained the same
9756688221

push

github

web-flow
Merge pull request #523 from PHPCSStandards/feature/docs-improve-reports-array-format-info

Documentation: improve information about Report data format + remove unused foreach keys

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

1 existing line in 1 file now uncovered.

23275 of 30825 relevant lines covered (75.51%)

61.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Reports/Source.php
1
<?php
2
/**
3
 * Source 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
use PHP_CodeSniffer\Util\Timing;
14

15
class Source implements Report
16
{
17

18

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

41
        $sources = [];
×
42

NEW
43
        foreach ($report['messages'] as $lineErrors) {
×
NEW
44
            foreach ($lineErrors as $colErrors) {
×
45
                foreach ($colErrors as $error) {
×
46
                    $src = $error['source'];
×
47
                    if (isset($sources[$src]) === false) {
×
48
                        $sources[$src] = [
×
49
                            'fixable' => (int) $error['fixable'],
×
50
                            'count'   => 1,
×
51
                        ];
×
52
                    } else {
53
                        $sources[$src]['count']++;
×
54
                    }
55
                }
56
            }
57
        }
58

59
        foreach ($sources as $source => $data) {
×
60
            echo $source.'>>'.$data['fixable'].'>>'.$data['count'].PHP_EOL;
×
61
        }
62

63
        return true;
×
64

65
    }//end generateFileReport()
66

67

68
    /**
69
     * Prints the source of all errors and warnings.
70
     *
71
     * @param string $cachedData    Any partial report data that was returned from
72
     *                              generateFileReport during the run.
73
     * @param int    $totalFiles    Total number of files processed during the run.
74
     * @param int    $totalErrors   Total number of errors found during the run.
75
     * @param int    $totalWarnings Total number of warnings found during the run.
76
     * @param int    $totalFixable  Total number of problems that can be fixed.
77
     * @param bool   $showSources   Show sources?
78
     * @param int    $width         Maximum allowed line width.
79
     * @param bool   $interactive   Are we running in interactive mode?
80
     * @param bool   $toScreen      Is the report being printed to screen?
81
     *
82
     * @return void
83
     */
84
    public function generate(
×
85
        $cachedData,
86
        $totalFiles,
87
        $totalErrors,
88
        $totalWarnings,
89
        $totalFixable,
90
        $showSources=false,
91
        $width=80,
92
        $interactive=false,
93
        $toScreen=true
94
    ) {
95
        $lines = explode(PHP_EOL, $cachedData);
×
96
        array_pop($lines);
×
97

98
        if (empty($lines) === true) {
×
99
            return;
×
100
        }
101

102
        $sources   = [];
×
103
        $maxLength = 0;
×
104

105
        foreach ($lines as $line) {
×
106
            $parts   = explode('>>', $line);
×
107
            $source  = $parts[0];
×
108
            $fixable = (bool) $parts[1];
×
109
            $count   = $parts[2];
×
110

111
            if (isset($sources[$source]) === false) {
×
112
                if ($showSources === true) {
×
113
                    $parts = null;
×
114
                    $sniff = $source;
×
115
                } else {
116
                    $parts = explode('.', $source);
×
117
                    if ($parts[0] === 'Internal') {
×
118
                        $parts[2] = $parts[1];
×
119
                        $parts[1] = '';
×
120
                    }
121

122
                    $parts[1] = $this->makeFriendlyName($parts[1]);
×
123

124
                    $sniff = $this->makeFriendlyName($parts[2]);
×
125
                    if (isset($parts[3]) === true) {
×
126
                        $name    = $this->makeFriendlyName($parts[3]);
×
127
                        $name[0] = strtolower($name[0]);
×
128
                        $sniff  .= ' '.$name;
×
129
                        unset($parts[3]);
×
130
                    }
131

132
                    $parts[2] = $sniff;
×
133
                }//end if
134

135
                $maxLength = max($maxLength, strlen($sniff));
×
136

137
                $sources[$source] = [
×
138
                    'count'   => $count,
×
139
                    'fixable' => $fixable,
×
140
                    'parts'   => $parts,
×
141
                ];
×
142
            } else {
143
                $sources[$source]['count'] += $count;
×
144
            }//end if
145
        }//end foreach
146

147
        if ($showSources === true) {
×
148
            $width = min($width, ($maxLength + 11));
×
149
        } else {
150
            $width = min($width, ($maxLength + 41));
×
151
        }
152

153
        $width = max($width, 70);
×
154

155
        // Sort the data based on counts and source code.
156
        $sourceCodes = array_keys($sources);
×
157
        $counts      = [];
×
158
        foreach ($sources as $source => $data) {
×
159
            $counts[$source] = $data['count'];
×
160
        }
161

162
        array_multisort($counts, SORT_DESC, $sourceCodes, SORT_ASC, SORT_NATURAL, $sources);
×
163

164
        echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL;
×
165
        echo str_repeat('-', $width).PHP_EOL."\033[1m";
×
166
        if ($showSources === true) {
×
167
            if ($totalFixable > 0) {
×
168
                echo '    SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL;
×
169
            } else {
170
                echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL;
×
171
            }
172
        } else {
173
            if ($totalFixable > 0) {
×
174
                echo '    STANDARD  CATEGORY            SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL;
×
175
            } else {
176
                echo 'STANDARD  CATEGORY            SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL;
×
177
            }
178
        }
179

180
        echo "\033[0m".str_repeat('-', $width).PHP_EOL;
×
181

182
        $fixableSources = 0;
×
183

184
        if ($showSources === true) {
×
185
            $maxSniffWidth = ($width - 7);
×
186
        } else {
187
            $maxSniffWidth = ($width - 37);
×
188
        }
189

190
        if ($totalFixable > 0) {
×
191
            $maxSniffWidth -= 4;
×
192
        }
193

194
        foreach ($sources as $source => $sourceData) {
×
195
            if ($totalFixable > 0) {
×
196
                echo '[';
×
197
                if ($sourceData['fixable'] === true) {
×
198
                    echo 'x';
×
199
                    $fixableSources++;
×
200
                } else {
201
                    echo ' ';
×
202
                }
203

204
                echo '] ';
×
205
            }
206

207
            if ($showSources === true) {
×
208
                if (strlen($source) > $maxSniffWidth) {
×
209
                    $source = substr($source, 0, $maxSniffWidth);
×
210
                }
211

212
                echo $source;
×
213
                if ($totalFixable > 0) {
×
214
                    echo str_repeat(' ', ($width - 9 - strlen($source)));
×
215
                } else {
216
                    echo str_repeat(' ', ($width - 5 - strlen($source)));
×
217
                }
218
            } else {
219
                $parts = $sourceData['parts'];
×
220

221
                if (strlen($parts[0]) > 8) {
×
222
                    $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1));
×
223
                }
224

225
                echo $parts[0].str_repeat(' ', (10 - strlen($parts[0])));
×
226

227
                $category = $parts[1];
×
228
                if (strlen($category) > 18) {
×
229
                    $category = substr($category, 0, ((strlen($category) - 18) * -1));
×
230
                }
231

232
                echo $category.str_repeat(' ', (20 - strlen($category)));
×
233

234
                $sniff = $parts[2];
×
235
                if (strlen($sniff) > $maxSniffWidth) {
×
236
                    $sniff = substr($sniff, 0, $maxSniffWidth);
×
237
                }
238

239
                if ($totalFixable > 0) {
×
240
                    echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff)));
×
241
                } else {
242
                    echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff)));
×
243
                }
244
            }//end if
245

246
            echo $sourceData['count'].PHP_EOL;
×
247
        }//end foreach
248

249
        echo str_repeat('-', $width).PHP_EOL;
×
250
        echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION';
×
251
        if (($totalErrors + $totalWarnings) > 1) {
×
252
            echo 'S';
×
253
        }
254

255
        echo ' WERE FOUND IN '.count($sources).' SOURCE';
×
256
        if (count($sources) !== 1) {
×
257
            echo 'S';
×
258
        }
259

260
        echo "\033[0m";
×
261

262
        if ($totalFixable > 0) {
×
263
            echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
×
264
            echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m";
×
265
        }
266

267
        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
×
268

269
        if ($toScreen === true && $interactive === false) {
×
270
            Timing::printRunTime();
×
271
        }
272

273
    }//end generate()
274

275

276
    /**
277
     * Converts a camel caps name into a readable string.
278
     *
279
     * @param string $name The camel caps name to convert.
280
     *
281
     * @return string
282
     */
283
    public function makeFriendlyName($name)
×
284
    {
285
        if (trim($name) === '') {
×
286
            return '';
×
287
        }
288

289
        $friendlyName = '';
×
290
        $length       = strlen($name);
×
291

292
        $lastWasUpper   = false;
×
293
        $lastWasNumeric = false;
×
294
        for ($i = 0; $i < $length; $i++) {
×
295
            if (is_numeric($name[$i]) === true) {
×
296
                if ($lastWasNumeric === false) {
×
297
                    $friendlyName .= ' ';
×
298
                }
299

300
                $lastWasUpper   = false;
×
301
                $lastWasNumeric = true;
×
302
            } else {
303
                $lastWasNumeric = false;
×
304

305
                $char = strtolower($name[$i]);
×
306
                if ($char === $name[$i]) {
×
307
                    // Lowercase.
308
                    $lastWasUpper = false;
×
309
                } else {
310
                    // Uppercase.
311
                    if ($lastWasUpper === false) {
×
312
                        $friendlyName .= ' ';
×
313
                        if ($i < ($length - 1)) {
×
314
                            $next = $name[($i + 1)];
×
315
                            if (strtolower($next) === $next) {
×
316
                                // Next char is lowercase so it is a word boundary.
317
                                $name[$i] = strtolower($name[$i]);
×
318
                            }
319
                        }
320
                    }
321

322
                    $lastWasUpper = true;
×
323
                }
324
            }//end if
325

326
            $friendlyName .= $name[$i];
×
327
        }//end for
328

329
        $friendlyName    = trim($friendlyName);
×
330
        $friendlyName[0] = strtoupper($friendlyName[0]);
×
331

332
        return $friendlyName;
×
333

334
    }//end makeFriendlyName()
335

336

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