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

RenderContext::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates;
4
5
use BadMethodCallException;
6
7
final class RenderContext
8
{
9
    public $render;
10
    public $template;
11
12
    private $func_stack;
13
14
    public function __construct(
15
        RenderTemplate $render,
16
        Template $template,
17
        $func_stack = null
18
    ) {
19
        $this->render = $render;
20
        $this->template = $template;
21
        $this->func_stack = $func_stack ?: Util\stack([RenderContext\platesFunc()]);
22
    }
23
24
    public function __get($name) {
25
        if (!$this->func_stack) {
26
            throw new BadMethodCallException('Cannot access ' . $name . ' because no func stack has been setup.');
27
        }
28
29
        $func_stack = $this->func_stack;
0 ignored issues
show
Unused Code introduced by
$func_stack 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...
30
        return $prop_stack(new RenderContext\FuncArgs(
0 ignored issues
show
Bug introduced by
The variable $prop_stack does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
31
            $this->render,
32
            $this->template,
33
            $name
34
        ));
35
    }
36
37
    public function __set($name, $value) {
38
        throw new BadMethodCallException('Cannot set ' . $name . ' on this render context.');
39
    }
40
41
    public function __call($name, array $args) {
42
        if (!$this->func_stack) {
43
            throw new BadMethodCallException('Cannot call ' . $name . ' because no func stack has been setup.');
44
        }
45
46
        $func_stack = $this->func_stack;
47
        return $func_stack(new RenderContext\FuncArgs(
48
            $this->render,
49
            $this->template,
50
            $name,
51
            $args
52
        ));
53
    }
54
55
    public function __invoke(...$args) {
56
        if (!$this->func_stack) {
57
            throw new BadMethodCallException('Cannot invoke the render context because no func stack has been setup.');
58
        }
59
60
        $func_stack = $this->func_stack;
61
        return $func_stack(new RenderContext\FuncArgs(
62
            $this->render,
63
            $this->template,
64
            '__invoke',
65
            $args
66
        ));
67
    }
68
69
    public static function factory($func_stack = null) {
70
        return function(RenderTemplate $render, Template $template) use (
71
            $func_stack
72
        ) {
73
            return new self(
74
                $render,
75
                $template,
76
                $func_stack
77
            );
78
        };
79
    }
80
}
81