• 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/Info.php
1
<?php
2
/**
3
 * Info 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 Info 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
        $metrics = $phpcsFile->getMetrics();
×
36
        foreach ($metrics as $metric => $data) {
×
37
            foreach ($data['values'] as $value => $count) {
×
38
                echo "$metric>>$value>>$count".PHP_EOL;
×
39
            }
40
        }
41

42
        return true;
×
43

44
    }//end generateFileReport()
45

46

47
    /**
48
     * Prints the recorded metrics.
49
     *
50
     * @param string $cachedData    Any partial report data that was returned from
51
     *                              generateFileReport during the run.
52
     * @param int    $totalFiles    Total number of files processed during the run.
53
     * @param int    $totalErrors   Total number of errors found during the run.
54
     * @param int    $totalWarnings Total number of warnings found during the run.
55
     * @param int    $totalFixable  Total number of problems that can be fixed.
56
     * @param bool   $showSources   Show sources?
57
     * @param int    $width         Maximum allowed line width.
58
     * @param bool   $interactive   Are we running in interactive mode?
59
     * @param bool   $toScreen      Is the report being printed to screen?
60
     *
61
     * @return void
62
     */
63
    public function generate(
×
64
        $cachedData,
65
        $totalFiles,
66
        $totalErrors,
67
        $totalWarnings,
68
        $totalFixable,
69
        $showSources=false,
70
        $width=80,
71
        $interactive=false,
72
        $toScreen=true
73
    ) {
74
        $lines = explode(PHP_EOL, $cachedData);
×
75
        array_pop($lines);
×
76

77
        if (empty($lines) === true) {
×
78
            return;
×
79
        }
80

81
        $metrics = [];
×
82
        foreach ($lines as $line) {
×
83
            $parts  = explode('>>', $line);
×
84
            $metric = $parts[0];
×
85
            $value  = $parts[1];
×
86
            $count  = $parts[2];
×
87
            if (isset($metrics[$metric]) === false) {
×
88
                $metrics[$metric] = [];
×
89
            }
90

91
            if (isset($metrics[$metric][$value]) === false) {
×
92
                $metrics[$metric][$value] = $count;
×
93
            } else {
94
                $metrics[$metric][$value] += $count;
×
95
            }
96
        }
97

98
        ksort($metrics);
×
99

100
        echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL;
×
101
        echo str_repeat('-', 70).PHP_EOL;
×
102

103
        foreach ($metrics as $metric => $values) {
×
104
            if (count($values) === 1) {
×
105
                $count = reset($values);
×
106
                $value = key($values);
×
107

108
                echo "$metric: \033[4m$value\033[0m [$count/$count, 100%]".PHP_EOL;
×
109
            } else {
110
                $totalCount = 0;
×
111
                $valueWidth = 0;
×
112
                foreach ($values as $value => $count) {
×
113
                    $totalCount += $count;
×
114
                    $valueWidth  = max($valueWidth, strlen($value));
×
115
                }
116

117
                // Length of the total string, plus however many
118
                // thousands separators there are.
119
                $countWidth = strlen($totalCount);
×
120
                $thousandSeparatorCount = floor($countWidth / 3);
×
121
                $countWidth            += $thousandSeparatorCount;
×
122

123
                // Account for 'total' line.
124
                $valueWidth = max(5, $valueWidth);
×
125

126
                echo "$metric:".PHP_EOL;
×
127

128
                ksort($values, SORT_NATURAL);
×
129
                arsort($values);
×
130

131
                $percentPrefixWidth = 0;
×
132
                $percentWidth       = 6;
×
133
                foreach ($values as $value => $count) {
×
134
                    $percent       = round(($count / $totalCount * 100), 2);
×
135
                    $percentPrefix = '';
×
136
                    if ($percent === 0.00) {
×
137
                        $percent            = 0.01;
×
138
                        $percentPrefix      = '<';
×
139
                        $percentPrefixWidth = 2;
×
140
                        $percentWidth       = 4;
×
141
                    }
142

143
                    printf(
×
144
                        "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL,
×
145
                        $value,
×
146
                        number_format($count),
×
147
                        $percentPrefix,
×
148
                        $percent
×
149
                    );
×
150
                }
151

152
                echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL;
×
153
                printf(
×
154
                    "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL,
×
155
                    'total',
×
156
                    number_format($totalCount)
×
157
                );
×
158
            }//end if
159

160
            echo PHP_EOL;
×
161
        }//end foreach
162

163
        echo str_repeat('-', 70).PHP_EOL;
×
164

165
    }//end generate()
166

167

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