Completed
Push — master ( 8398cb...f02e4f )
by
unknown
05:33
created

HtmlReporter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 2
A processResults() 0 11 2
A finish() 0 19 3
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use whm\Smoke\Config\Configuration;
7
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
8
use whm\Smoke\Rules\CheckResult;
9
10
11
class HtmlReporter implements Reporter
12
{
13
    private $results = [];
14
    /**
15
     * @var Configuration
16
     */
17
    private $templateDir;
18
    private $templateFile;
19
    private $resultFile;
20
21
    /**
22
     * @var OutputInterface
23
     */
24
    private $output;
25
26
    public function init(OutputInterface $_output, $resultFile, $templateDir = null, $templateFile = 'index.html.twig')
27
    {
28
        $this->resultFile = $resultFile;
29
30
        if ($templateDir === null) {
31
            $this->templateDir = __DIR__ . '/templates';
0 ignored issues
show
Documentation Bug introduced by
It seems like __DIR__ . '/templates' of type string is incompatible with the declared type object<whm\Smoke\Config\Configuration> of property $templateDir.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        } else {
33
            $this->templateDir = $templateDir;
34
        }
35
36
        $this->templateFile = $templateFile;
37
        $this->output = $_output;
38
    }
39
40
    /**
41
     * @param CheckResult[] $results
42
     */
43
    public function processResults($results)
44
    {
45
        foreach ($results as $result) {
46
            $this->results[] = [
47
                'url' => $result->getResponse()->getUri(),
48
                'rule' => $result->getRuleName(),
49
                'message' => $result->getMessage(),
50
                'status' => $result->getStatus(),
51
            ];
52
        }
53
    }
54
55
    public function finish()
56
    {
57
        $loader = new \Twig_Loader_Filesystem($this->templateDir);
0 ignored issues
show
Documentation introduced by
$this->templateDir is of type object<whm\Smoke\Config\Configuration>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
        $twig = new \Twig_Environment($loader);
59
60
        $html = $twig->render($this->templateFile, [
61
            'results' => $this->results,
62
        ]);
63
64
        if (!file_exists(dirname($this->resultFile))) {
65
            mkdir(dirname($this->resultFile));
66
        }
67
68
        if (!file_put_contents($this->resultFile, $html)) {
69
            $this->output->writeln("<error>HTML Reporter extension: Could not write result file to " . $this->resultFile ."</error>");
70
        } else {
71
            $this->output->writeln("<info>HTML Reporter extension:</info> Result file written to " . $this->resultFile);
72
        }
73
    }
74
75
}
76