HtmlOutput::renderView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace Finder\Logic\Output;
3
4
use League\Plates\Engine;
5
use Finder\Logic\Analyser;
6
use Finder\Logic\AnalysisResult;
7
use Finder\Logic\Output\Html\FileHighlighter;
8
use Finder\Logic\Output\Html\History;
9
use DateTime;
10
use SplFileObject;
11
12
class HtmlOutput extends AbstractOutput implements TriggerableInterface
13
{
14
    use TextTriggerTrait;
15
16
    /**
17
     * Plates engine used to output HTML.
18
     *
19
     * @var Engine Plates engine instance.
20
     */
21
    protected $platesEngine;
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function result(AnalysisResult $result)
27
    {
28
        $this->cli->br();
29
        $this->cli->inline('Writing HTML report in "./Finder\Logic/"... ');
30
        $history = new History($this->getOutputDirectory());
31
        $history->append($result);
32
        foreach ($result->toArray() as $filePath => $lines) {
33
            $this->writeFileHtml($filePath, $lines);
34
        }
35
        $this->writeIndexHtml($result, $history);
36
        $history->save();
37
        $this->cli->out('Done!');
38
    }
39
40
    /**
41
     * Create HTML report index page.
42
     *
43
     * @param  AnalysisResult $result result data object.
44
     * @return void
45
     */
46
    protected function writeIndexHtml(AnalysisResult $result, History $history)
47
    {
48
        $files = [];
49
50
        foreach ($result->toArray() as $fileName => $lines) {
51
            if (!isset($files[$fileName])) {
52
                $files[$fileName] = 0;
53
            }
54
55
            foreach ($lines as $issues) {
56
                $files[$fileName] += count($issues);
57
            }
58
        }
59
60
        $chartData = $history->getData();
61
        $indexHtml = $this->renderView(
62
            'index',
63
            [
64
                'files' => $files,
65
                'executions' => $chartData['executions'],
66
                'historyData' => array_values($chartData['historyData']),
67
            ]
68
        );
69
        $fileName = $this->getOutputDirectory() . '/index.html';
70
        $file = new SplFileObject($fileName, 'w');
71
        $file->fwrite($indexHtml);
72
    }
73
74
    /**
75
     * Create HTML report for one file.
76
     *
77
     * @param  string $phpFilePath analyzed PHP file path.
78
     * @param  array  $lines       lines with their issues.
79
     * @return void
80
     */
81
    protected function writeFileHtml($phpFilePath, $lines)
82
    {
83
        $highlighter = new FileHighlighter($phpFilePath, $lines);
84
        $fileHtml = $this->renderView(
85
            'file',
86
            [
87
                'fileName' => $phpFilePath,
88
                'lines' => $lines,
89
                'fileContent' => $highlighter->getHtml(),
90
                'backButton' => true,
91
            ]
92
        );
93
        $htmlFileName = $this->getOutputDirectory() . '/'
94
            . str_replace(DIRECTORY_SEPARATOR, '_', $phpFilePath) . '.html';
95
96
        $file = new SplFileObject($htmlFileName, 'w');
97
        $file->fwrite($fileHtml);
98
    }
99
100
    /**
101
     * Render a HTML view within the layout.
102
     *
103
     * @param  string $view view file name.
104
     * @param  array  $data variables required by view file.
105
     * @return string output HTML.
106
     */
107
    protected function renderView($view, $data)
108
    {
109
        $date = new DateTime;
110
        $content = $this->getPlatesEngine()->render($view, $data);
111
112
        return $this->getPlatesEngine()->render(
113
            'layout',
114
            [
115
                'content' => $content,
116
                'phpHoundVersion' => 'PHP Hound ' . Analyser::VERSION,
117
                'phpVersion' => 'PHP ' . phpversion(),
118
                'generationTime' => $date->format('r'),
119
                'backButton' => !empty($data['backButton']),
120
            ]
121
        );
122
    }
123
124
    /**
125
     * Configure and return Plates engine.
126
     *
127
     * @return Engine Plates engine instance.
128
     */
129
    protected function getPlatesEngine()
130
    {
131
        if (null === $this->platesEngine) {
132
            $this->platesEngine = new Engine(__DIR__ . '/../templates');
133
        }
134
135
        return $this->platesEngine;
136
    }
137
138
    /**
139
     * Get output directory.
140
     *
141
     * @return string output directory path.
142
     */
143
    protected function getOutputDirectory()
144
    {
145
        $directory = $this->outputDirectory . '/Finder\Logic';
146
147
        if (!file_exists($directory)) {
148
            mkdir($directory);
149
        }
150
151
        return $directory;
152
    }
153
}
154