• 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/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

14
class Source 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
        $sources = [];
×
41

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

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

62
        return true;
×
63

64
    }//end generateFileReport()
65

66

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

181
        $fixableSources = 0;
×
182

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

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

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

203
                echo '] ';
×
204
            }
205

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

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

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

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

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

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

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

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

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

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

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

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

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

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

268
    }//end generate()
269

270

271
    /**
272
     * Converts a camel caps name into a readable string.
273
     *
274
     * @param string $name The camel caps name to convert.
275
     *
276
     * @return string
277
     */
278
    public function makeFriendlyName($name)
×
279
    {
280
        if (trim($name) === '') {
×
281
            return '';
×
282
        }
283

284
        $friendlyName = '';
×
285
        $length       = strlen($name);
×
286

287
        $lastWasUpper   = false;
×
288
        $lastWasNumeric = false;
×
289
        for ($i = 0; $i < $length; $i++) {
×
290
            if (is_numeric($name[$i]) === true) {
×
291
                if ($lastWasNumeric === false) {
×
292
                    $friendlyName .= ' ';
×
293
                }
294

295
                $lastWasUpper   = false;
×
296
                $lastWasNumeric = true;
×
297
            } else {
298
                $lastWasNumeric = false;
×
299

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

317
                    $lastWasUpper = true;
×
318
                }
319
            }//end if
320

321
            $friendlyName .= $name[$i];
×
322
        }//end for
323

324
        $friendlyName    = trim($friendlyName);
×
325
        $friendlyName[0] = strtoupper($friendlyName[0]);
×
326

327
        return $friendlyName;
×
328

329
    }//end makeFriendlyName()
330

331

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