File: Reporting.php

package info (click to toggle)
php-codesniffer 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,092 kB
  • sloc: php: 30,445; xml: 3,768; makefile: 15; pascal: 8; sh: 6
file content (231 lines) | stat: -rw-r--r-- 7,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
 * A class to manage reporting.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Gabriele Santini <gsantini@sqli.com>
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @copyright 2009 SQLI <www.sqli.com>
 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */

if (is_file(dirname(__FILE__).'/../CodeSniffer.php') === true) {
    include_once dirname(__FILE__).'/../CodeSniffer.php';
} else {
    include_once 'PHP/CodeSniffer.php';
}

/**
 * A class to manage reporting.
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Gabriele Santini <gsantini@sqli.com>
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @copyright 2009 SQLI <www.sqli.com>
 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @version   Release: 1.3.4
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */
class PHP_CodeSniffer_Reporting
{


    /**
     * Produce the appropriate report object based on $type parameter.
     *
     * @param string $type Demanded report type.
     *
     * @return PHP_CodeSniffer_Report
     * @throws PHP_CodeSniffer_Exception If report is not available.
     */
    public function factory($type)
    {
        $type            = ucfirst($type);
        $filename        = $type.'.php';
        $reportClassName = 'PHP_CodeSniffer_Reports_'.$type;
        if (class_exists($reportClassName, true) === false) {
            throw new PHP_CodeSniffer_Exception('Report type "'.$type.'" not found.');
        }

        $reportClass = new $reportClassName();
        if (false === ($reportClass instanceof PHP_CodeSniffer_Report)) {
            throw new PHP_CodeSniffer_Exception('Class "'.$reportClassName.'" must implement the "PHP_CodeSniffer_Report" interface.');
        }

        return $reportClass;

    }//end factory()


    /**
     * Actually generates the report.
     * 
     * @param string  $report          Report type.
     * @param array   $filesViolations Collected violations.
     * @param boolean $showSources     Show sources?
     * @param string  $reportFile      Report file to generate.
     * @param integer $reportWidth     Report max width.
     * 
     * @return integer
     */
    public function printReport(
        $report,
        $filesViolations,
        $showSources,
        $reportFile='',
        $reportWidth=80
    ) {
        if ($reportFile !== null) {
            $reportDir = dirname($reportFile);
            if ($reportDir === '.') {
                // Passed report file is a filename in the current directory.
                $reportFile = PHPCS_CWD.'/'.basename($reportFile);
            } else {
                $reportDir = realpath(PHPCS_CWD.'/'.$reportDir);
                if ($reportDir !== false) {
                    // Report file path is relative.
                    $reportFile = $reportDir.'/'.basename($reportFile);
                }
            }
        }

        $reportClass = self::factory($report);
        $reportData  = $this->prepare($filesViolations);

        $toScreen = true;
        if ($reportFile !== null) {
            $toScreen = false;
            ob_start();
        }

        $numErrors = $reportClass->generate($reportData, $showSources, $reportWidth, $toScreen);

        if ($reportFile !== null) {
            $generatedReport = ob_get_contents();
            if (PHP_CODESNIFFER_VERBOSITY > 0) {
                ob_end_flush();
            } else {
                ob_end_clean();
            }

            $generatedReport = trim($generatedReport);
            file_put_contents($reportFile, $generatedReport.PHP_EOL);
        }

        return $numErrors;

    }//end printReport()


    /**
     * Pre-process and package violations for all files.
     *
     * Used by error reports to get a packaged list of all errors in each file.
     *
     * @param array $filesViolations List of found violations.
     *
     * @return array
     */
    public function prepare(array $filesViolations)
    {
        $report = array(
                   'totals' => array(
                                'warnings' => 0,
                                'errors'   => 0,
                               ),
                   'files'  => array(),
                  );

        foreach ($filesViolations as $filename => $fileViolations) {
            $warnings    = $fileViolations['warnings'];
            $errors      = $fileViolations['errors'];
            $numWarnings = $fileViolations['numWarnings'];
            $numErrors   = $fileViolations['numErrors'];

            $report['files'][$filename] = array(
                                           'errors'   => 0,
                                           'warnings' => 0,
                                           'messages' => array(),
                                          );

            if ($numErrors === 0 && $numWarnings === 0) {
                // Prefect score!
                continue;
            }

            $report['files'][$filename]['errors']   = $numErrors;
            $report['files'][$filename]['warnings'] = $numWarnings;

            $report['totals']['errors']   += $numErrors;
            $report['totals']['warnings'] += $numWarnings;

            // Merge errors and warnings.
            foreach ($errors as $line => $lineErrors) {
                foreach ($lineErrors as $column => $colErrors) {
                    $newErrors = array();
                    foreach ($colErrors as $data) {
                        $newErrors[] = array(
                                        'message'  => $data['message'],
                                        'source'   => $data['source'],
                                        'severity' => $data['severity'],
                                        'type'     => 'ERROR',
                                       );
                    }//end foreach

                    $errors[$line][$column] = $newErrors;
                }//end foreach

                ksort($errors[$line]);
            }//end foreach

            foreach ($warnings as $line => $lineWarnings) {
                foreach ($lineWarnings as $column => $colWarnings) {
                    $newWarnings = array();
                    foreach ($colWarnings as $data) {
                        $newWarnings[] = array(
                                          'message'  => $data['message'],
                                          'source'   => $data['source'],
                                          'severity' => $data['severity'],
                                          'type'     => 'WARNING',
                                         );
                    }//end foreach

                    if (isset($errors[$line]) === false) {
                        $errors[$line] = array();
                    }

                    if (isset($errors[$line][$column]) === true) {
                        $errors[$line][$column] = array_merge(
                            $newWarnings,
                            $errors[$line][$column]
                        );
                    } else {
                        $errors[$line][$column] = $newWarnings;
                    }
                }//end foreach

                ksort($errors[$line]);
            }//end foreach

            ksort($errors);

            $report['files'][$filename]['messages'] = $errors;
        }//end foreach

        ksort($report['files']);

        return $report;

    }//end prepare()


}//end class

?>