Completed
Push — master ( cd7e71...ba3348 )
by Midori
39:44 queued 24:43
created

TemplateRenderer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A render() 0 17 2
1
<?php
2
3
declare(strict_types=1);
4
5
// phpcs:disable Generic.PHP.ForbiddenFunctions
6
7
namespace midorikocak\view;
8
9
use function array_map;
10
use function array_unique;
11
use function count;
12
use function file_get_contents;
13
use function preg_match_all;
14
use function preg_replace;
15
use function str_replace;
16
use function trim;
17
18
class TemplateRenderer implements RendererInterface
19
{
20
    public function render(string $filename, array $data): string
21
    {
22
        $templateContents = file_get_contents($filename);
23
        preg_match_all('/{{ *([a-zA-Z_0-9-]*)? *}}/', $templateContents, $templateVars);
24
25
        $templateVarNames = array_unique(array_map(function ($item) {
26
            return trim($item);
27
        }, $templateVars[1]));
28
29
        $tokenized = preg_replace('/{{ *([a-zA-Z_0-9-]*)? *}}/', '{{$1}}', $templateContents);
30
31
        $varLength = count($templateVars);
0 ignored issues
show
Unused Code introduced by
The assignment to $varLength is dead and can be removed.
Loading history...
32
33
        foreach ($templateVarNames as $toReplace) {
34
            $tokenized = str_replace('{{' . $toReplace . '}}', $data[$toReplace] ?? '', $tokenized);
35
        }
36
        return $tokenized;
37
    }
38
}
39