HtmlReporter::processResults()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 1
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
    private $successes = [];
15
    private $failures = [];
16
    private $unknowns = [];
17
18
19
    /**
20
     * @var Configuration
21
     */
22
    private $templateDir;
23
    private $templateFile;
24
    private $resultFile;
25
26
    /**
27
     * @var OutputInterface
28
     */
29
    private $output;
30
31
    public function init(OutputInterface $_output, $resultFile, $templateDir = null, $templateFile = 'index.html.twig')
32
    {
33
        $this->resultFile = $resultFile;
34
35
        if ($templateDir === null) {
36
            $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...
37
        } else {
38
            $this->templateDir = $templateDir;
39
        }
40
41
        $this->templateFile = $templateFile;
42
        $this->output = $_output;
43
    }
44
45
    /**
46
     * @param CheckResult[] $results
47
     */
48
    public function processResults($results)
49
    {
50
        foreach ($results as $result) {
51
            $temp = [
52
                'url' => $result->getResponse()->getUri(),
53
                'rule' => $result->getRuleName(),
54
                'message' => $result->getMessage(),
55
                'status' => $result->getStatus(),
56
            ];
57
58
            switch ($result->getStatus()) {
59
                case CheckResult::STATUS_SUCCESS:
60
                    $this->successes[] = $temp;
61
                    break;
62
63
                case CheckResult::STATUS_FAILURE:
64
                    $this->failures[] = $temp;
65
                break;
66
                default:
67
                    $this->unknowns[] = $temp;
68
            }
69
70
            $this->results[] = $temp;
71
        }
72
    }
73
74
    public function finish()
75
    {
76
        $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...
Deprecated Code introduced by
The class Twig_Loader_Filesystem has been deprecated with message: since Twig 2.7, use "Twig\Loader\FilesystemLoader" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
77
        $twig = new \Twig_Environment($loader);
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated with message: since Twig 2.7, use "Twig\Environment" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
78
79
        $html = $twig->render($this->templateFile, [
80
            'results' => $this->results,
81
            'successes' => $this->successes,
82
            'failures' => $this->failures,
83
            'unknowns' => $this->unknowns,
84
            'success' => count($this->successes),
85
            'failure' => count($this->failures),
86
            'unknown' => count($this->unknowns),
87
            'total' => count($this->results),
88
        ]);
89
90
        if (!file_exists(dirname($this->resultFile))) {
91
            mkdir(dirname($this->resultFile));
92
        }
93
94
        if (!file_put_contents($this->resultFile, $html)) {
95
            $this->output->writeln("<error>HTML Reporter extension: Could not write result file to " . $this->resultFile ."</error>");
96
        } else {
97
            $this->output->writeln("<info>HTML Reporter extension:</info> Result file written to " . $this->resultFile);
98
        }
99
    }
100
101
}
102