HtmlReportHelper::generateHtmlFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of project-quality-inspector.
4
 *
5
 * (c) Alexandre GESLIN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ProjectQualityInspector\Application\Output;
12
13
use ProjectQualityInspector\Exception\RuleViolationException;
14
use ProjectQualityInspector\Iterator\RuleFilterIterator;
15
16
class HtmlReportHelper
17
{
18
    /**
19
     * @var string
20
     */
21
    const PAGE_HEADER = <<<EOT
22
<!doctype html>
23
<html lang="en">
24
    <head>
25
        <meta charset="utf-8"/>
26
        <title>Test Documentation</title>
27
        <style>
28
            body {
29
                text-rendering: optimizeLegibility;
30
                font-variant-ligatures: common-ligatures;
31
                font-kerning: normal;
32
                margin-left: 2em;
33
                font-family: Verdana, Arial, sans-serif;
34
            }
35
            h2 {
36
                font-family: Tahoma, Helvetica, Arial;
37
                font-size: 1em;
38
            }
39
            ul {
40
                list-style: none;
41
                margin-bottom: 1em;
42
            }
43
            table {
44
                border-collapse: collapse;
45
                width: 100%;
46
            }
47
            table, th, td {
48
               border: 1px solid black;
49
               font-size: 0.95em;
50
            }
51
            th {
52
                height: 2em;
53
                text-align: center;
54
                background-color:#4CAF50;
55
                color:white;
56
            }
57
            td {
58
                padding: 0.4em;
59
            }
60
            tr:nth-child(even) {
61
                background-color: #f2f2f2
62
            }
63
        </style>
64
    </head>
65
    <body>
66
EOT;
67
68
    /**
69
     * @var string
70
     */
71
    const PAGE_FOOTER = <<<EOT
72
    </body>
73
</html>
74
EOT;
75
76
    /**
77
     * @param  RuleInterface[]  $rules
78
     * @param  string $htmlFile
79
     */
80
    public static function generateHtmlFile(RuleFilterIterator $rules, $htmlFile)
81
    {
82
        $html = self::PAGE_HEADER . '<div>
83
        <h1>PQI</h1>';
84
85
        foreach ($rules as $rule) {
86
            $html .= static::createTestSuite($rule::getRuleName(), $rule->getAssertions());
0 ignored issues
show
Bug introduced by
Since createTestSuite() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTestSuite() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
87
        }
88
89
        $html .= '</div>' . self::PAGE_FOOTER;
90
91
        file_put_contents($htmlFile, $html);
92
    }
93
94
    /**
95
     * @param  string $name
96
     * @param  array $tests
97
     * @return string
98
     */
99
    private static function createTestSuite($name, array $tests)
100
    {
101
        $testSuite = '<h2> '.strtoupper($name).', tests: '.count($tests).' failures: '.static::sumArraysKey('failures', $tests, 'sum').', errors: '.static::sumArraysKey('errors', $tests, 'sum').', time: '.static::sumArraysKey('time', $tests).'</h2>';
0 ignored issues
show
Bug introduced by
Since sumArraysKey() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of sumArraysKey() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
102
103
        $testSuite .= '<table class="testsuite">
104
        <tr>
105
            <th>Type</th>
106
            <th>message</th>
107
        </tr>';
108
109
        foreach ($tests as $test) {
110
            $testSuite .= static::createTestCase($test);
0 ignored issues
show
Bug introduced by
Since createTestCase() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createTestCase() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
111
        }
112
113
        $testSuite .= '</table>';
114
115
        return $testSuite;
116
    }
117
118
    /**
119
     * @param  array $test
120
     * @return string
121
     */
122
    private static function createTestCase(array $test)
123
    {
124
        $testCase = '';
125
126 View Code Duplication
        if ($test['failures']['sum'] > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
            foreach ($test['failures']['list']  as $failure) {
128
                $testCase .= '<tr class="failure">
129
                    <td>'.$failure['type'].'</td>
130
                    <td>'.self::sanitizeTags($failure['message']).'</td>
131
                </tr>';
132
            }
133
        }
134
135 View Code Duplication
        if ($test['errors']['sum'] > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
            foreach ($test['errors']['list'] as $error) {
137
                $testCase .= '<tr class="error">
138
                    <td>'.$error['type'].'</td>
139
                    <td>'.self::sanitizeTags($error['message']).'</td>
140
                </tr>';
141
            }
142
        }
143
144
        return $testCase;
145
    }
146
147
    /**
148
     * @param  string $key
149
     * @param  array  $arrays
150
     * @param  string  $subKey
151
     * @return integer
152
     */
153 View Code Duplication
    private static function sumArraysKey($key, array $arrays, $subKey = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $sum = 0;
156
157
        foreach ($arrays as $array) {
158
            $sum += ($subKey) ? $array[$key][$subKey] : $array[$key];
159
        }
160
161
        return $sum;
162
    }
163
164
    private static function sanitizeTags($string)
165
    {
166
        $string = str_replace('<fg=green>', '<span style="color: green;">', $string);
167
        $string = str_replace('</>', '</span>', $string);
168
        $string = preg_replace('/\t+/', '<br />', $string);
169
170
        return $string;
171
    }
172
}