ViewableTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 155
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A setTemplateIdent() 0 5 1
A templateIdent() 0 4 1
A setView() 0 5 1
A view() 0 4 1
A render() 0 8 2
A renderTemplate() 0 4 1
A setViewController() 0 12 3
A viewController() 0 8 2
A setDynamicTemplate() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Charcoal\View;
6
7
use InvalidArgumentException;
8
9
// From 'charcoal-view'
10
use Charcoal\View\ViewInterface;
11
12
/**
13
 * Implementation, as trait, of the {@see \Charcoal\View\ViewableInterface}.
14
 */
15
trait ViewableTrait
16
{
17
    /**
18
     * The object's template identifier.
19
     *
20
     * @var string
21
     */
22
    private $templateIdent = '';
23
24
    /**
25
     * The context for the {@see self::$view} to render templates.
26
     *
27
     * @var ViewableInterface
28
     */
29
    private $viewController;
30
31
    /**
32
     * The renderable view.
33
     *
34
     * @var ViewInterface
35
     */
36
    private $view;
37
38
    /**
39
     * Render the viewable object.
40
     *
41
     * @return string
42
     */
43
    public function __toString()
44
    {
45
        return $this->render();
46
    }
47
48
    /**
49
     * Set the template identifier for this viewable object.
50
     *
51
     * Usually, a path to a file containing the template to be rendered at runtime.
52
     *
53
     * @param string $templateIdent The template ID.
54
     * @return self
55
     */
56
    public function setTemplateIdent(string $templateIdent)
57
    {
58
        $this->templateIdent = $templateIdent;
59
        return $this;
60
    }
61
62
    /**
63
     * Retrieve the template identifier for this viewable object.
64
     *
65
     * @return string
66
     */
67
    public function templateIdent(): string
68
    {
69
        return $this->templateIdent;
70
    }
71
72
    /**
73
     * Set the renderable view.
74
     *
75
     * @param ViewInterface|array $view The view instance to use to render.
76
     * @throws InvalidArgumentException If the view parameter is not an array or a View object.
77
     * @return self
78
     */
79
    public function setView(ViewInterface $view)
80
    {
81
        $this->view = $view;
82
        return $this;
83
    }
84
85
    /**
86
     * Retrieve the renderable view.
87
     *
88
     * @return ViewInterface The object's View instance.
89
     */
90
    public function view()
91
    {
92
        return $this->view;
93
    }
94
95
    /**
96
     * Render the template by the given identifier.
97
     *
98
     * Usually, a path to a file containing the template to be rendered at runtime.
99
     *
100
     * @param string $templateIdent The template to load, parse, and render.
101
     *     If NULL, will use the object's previously set template identifier.
102
     * @return string The rendered template.
103
     */
104
    public function render(?string $templateIdent = null): string
105
    {
106
        if ($templateIdent === null) {
107
            $templateIdent = $this->templateIdent();
108
        }
109
110
        return $this->view()->render($templateIdent, $this->viewController());
111
    }
112
113
    /**
114
     * Render the given template from string.
115
     *
116
     * @param string $templateString The template  to render from string.
117
     * @return string The rendered template.
118
     */
119
    public function renderTemplate(string $templateString): string
120
    {
121
        return $this->view()->renderTemplate($templateString, $this->viewController());
122
    }
123
124
    /**
125
     * Set a view controller for the template's context.
126
     *
127
     * @param ViewableInterface|object|array|null $controller A view controller to use when rendering.
128
     * @throws InvalidArgumentException If the controller is invalid.
129
     * @return self
130
     */
131
    public function setViewController($controller)
132
    {
133
        if (is_scalar($controller) || is_resource($controller)) {
134
            throw new InvalidArgumentException(
135
                'View controller must be an object, null or an array'
136
            );
137
        }
138
139
        $this->viewController = $controller;
0 ignored issues
show
Documentation Bug introduced by
It seems like $controller can also be of type array. However, the property $viewController is declared as type object<Charcoal\View\ViewableInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
140
141
        return $this;
142
    }
143
144
    /**
145
     * Retrieve a view controller for the template's context.
146
     *
147
     * If no controller has been defined, it will return itself.
148
     *
149
     * @return mixed
150
     */
151
    public function viewController()
152
    {
153
        if ($this->viewController === null) {
154
            return $this;
155
        }
156
157
        return $this->viewController;
158
    }
159
160
    /**
161
     * @param string      $varName       The name of the variable to set this template unto.
162
     * @param string|null $templateIdent The "dynamic template" to set. null to clear.
163
     * @return void
164
     */
165
    public function setDynamicTemplate(string $varName, ?string $templateIdent): void
166
    {
167
        $this->view()->setDynamicTemplate($varName, $templateIdent);
168
    }
169
}
170