Completed
Push — v4.0-dev ( b01e70 )
by
unknown
01:53
created

PlatesRenderTemplate::renderTemplate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\RenderTemplate;
4
5
use League\Plates;
6
7
final class PlatesRenderTemplate implements Plates\RenderTemplate
8
{
9
    private $resolve_name;
10
    private $resolve_data;
11
    private $include;
12
    private $create_render_context;
13
    private $render_context_var_name;
14
15
    public function __construct(
16
        $resolve_name = null,
17
        $resolve_data = null,
18
        $include = null,
19
        $create_render_context = null,
20
        $render_context_var_name = 'v'
21
    ) {
22
        $this->resolve_name = $resolve_name ?: Plates\Util\id(true);
23
        $this->resolve_data = $resolve_data ?: Plates\Util\id(true);
24
        $this->include = $include ?: Plates\Template\phpInclude();
25
        $this->create_render_context = $create_render_context ?: function($render, $template) {
26
            return (object) [
27
                'render' => $render,
28
                'template' => $template
29
            ];
30
        };
31
        $this->render_context_var_name = $render_context_var_name;
32
    }
33
34
    public function renderTemplate(Plates\Template $template) {
35
        $resolve_name = $this->resolve_name;
0 ignored issues
show
Unused Code introduced by
$resolve_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
37
        list($path) = $template->resolveName($this->resolve_name);
38
        list($data) = $template->resolveData($this->resolve_data);
39
40
        $template->addContext([
41
            'path' => $path,
42
            'current_directory' => basename($path)
43
        ]);
44
        $ctx = call_user_func($this->create_render_context, $this, $template);
45
46
        if (array_key_exists($this->render_context_var_name, $data)) {
47
            throw new Plates\Exception\PlatesException('Cannot set render context because a variable already exists as ' . $this->render_context_var_name);
48
        }
49
        $data[$this->render_context_var_name] = $ctx;
50
51
        // this is done for BC reasons}
52
        $include = $this->include->bindTo($ctx);
53
54
        $contents = $include($path, $data);
55
56
        return $contents;
57
    }
58
}
59