Passed
Pull Request — master (#18)
by Andru
03:44
created

HTMLRenderer::glomProcessingErrors()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4.0023

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 27
ccs 18
cts 19
cp 0.9474
crap 4.0023
rs 9.488
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Renderer;
19
20
use PHPMD\AbstractRenderer;
21
use PHPMD\Report;
22
23
/**
24
 * This renderer output a simple html file with all found violations and suspect
25
 * software artifacts.
26
 */
27
class HTMLRenderer extends AbstractRenderer
28
{
29
    /**
30
     * This method will be called on all renderers before the engine starts the
31
     * real report processing.
32
     *
33
     * @return void
34
     */
35 2
    public function start()
36
    {
37 2
        $writer = $this->getWriter();
38
39 2
        $writer->write('<html><head><title>PHPMD</title></head><body>');
40 2
        $writer->write(PHP_EOL);
41 2
        $writer->write('<center><h1>PHPMD report</h1></center>');
42 2
        $writer->write('<center><h2>Problems found</h2></center>');
43 2
        $writer->write(PHP_EOL);
44 2
        $writer->write('<table align="center" cellspacing="0" cellpadding="3">');
45 2
        $writer->write('<tr>');
46 2
        $writer->write('<th>#</th><th>File</th><th>Line</th><th>Problem</th>');
47 2
        $writer->write('</tr>');
48 2
        $writer->write(PHP_EOL);
49 2
    }
50
51
    /**
52
     * This method will be called when the engine has finished the source analysis
53
     * phase.
54
     *
55
     * @param \PHPMD\Report $report
56
     * @return void
57
     */
58 2
    public function renderReport(Report $report)
59
    {
60 2
        $index = 0;
61
62 2
        $writer = $this->getWriter();
63 2
        foreach ($report->getRuleViolations() as $violation) {
64 1
            $writer->write('<tr');
65 1
            if (++$index % 2 === 1) {
66 1
                $writer->write(' bgcolor="lightgrey"');
67
            }
68 1
            $writer->write('>');
69 1
            $writer->write(PHP_EOL);
70
71 1
            $writer->write('<td align="center">');
72 1
            $writer->write($index);
73 1
            $writer->write('</td>');
74 1
            $writer->write(PHP_EOL);
75
76 1
            $writer->write('<td>');
77 1
            $writer->write(htmlentities($violation->getFileName()));
78 1
            $writer->write('</td>');
79 1
            $writer->write(PHP_EOL);
80
81 1
            $writer->write('<td align="center" width="5%">');
82 1
            $writer->write($violation->getBeginLine());
83 1
            $writer->write('</td>');
84 1
            $writer->write(PHP_EOL);
85
86 1
            $writer->write('<td>');
87 1
            if ($violation->getRule()->getExternalInfoUrl()) {
88 1
                $writer->write('<a href="');
89 1
                $writer->write($violation->getRule()->getExternalInfoUrl());
90 1
                $writer->write('">');
91
            }
92
93 1
            $writer->write(htmlentities($violation->getDescription()));
94 1
            if ($violation->getRule()->getExternalInfoUrl()) {
95 1
                $writer->write('</a>');
96
            }
97
98 1
            $writer->write('</td>');
99 1
            $writer->write(PHP_EOL);
100
101 1
            $writer->write('</tr>');
102 1
            $writer->write(PHP_EOL);
103
        }
104
105 2
        $writer->write('</table>');
106
107 2
        $this->glomProcessingErrors($report);
108 2
    }
109
110
    /**
111
     * This method will be called the engine has finished the report processing
112
     * for all registered renderers.
113
     *
114
     * @return void
115
     */
116 2
    public function end()
117
    {
118 2
        $writer = $this->getWriter();
119 2
        $writer->write('</body></html>');
120 2
    }
121
122
    /**
123
     * This method will render a html table with occurred processing errors.
124
     *
125
     * @param \PHPMD\Report $report
126
     * @return void
127
     * @since 1.2.1
128
     */
129 2
    private function glomProcessingErrors(Report $report)
130
    {
131 2
        if (false === $report->hasErrors()) {
132
            return;
133
        }
134
135 2
        $writer = $this->getWriter();
136
137 2
        $writer->write('<hr />');
138 2
        $writer->write('<center><h3>Processing errors</h3></center>');
139 2
        $writer->write('<table align="center" cellspacing="0" cellpadding="3">');
140 2
        $writer->write('<tr><th>File</th><th>Problem</th></tr>');
141
142 2
        $index = 0;
143 2
        foreach ($report->getErrors() as $error) {
144 1
            $writer->write('<tr');
145 1
            if (++$index % 2 === 1) {
146 1
                $writer->write(' bgcolor="lightgrey"');
147
            }
148 1
            $writer->write('>');
149 1
            $writer->write('<td>' . $error->getFile() . '</td>');
150 1
            $writer->write('<td>' . htmlentities($error->getMessage()) . '</td>');
151 1
            $writer->write('</tr>' . PHP_EOL);
152
        }
153
154 2
        $writer->write('</table>');
155 2
    }
156
}
157