GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

View   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
lcom 3
cbo 0
dl 0
loc 218
ccs 64
cts 64
cp 1
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setOptions() 0 10 3
A __set() 0 4 1
A __get() 0 8 2
A __isset() 0 4 1
A setLayout() 0 4 1
A render() 0 11 3
A renderScript() 0 14 2
A layoutContent() 0 4 1
A escape() 0 4 1
A getEscaped() 0 8 2
A addHelperClass() 0 4 1
A getHelper() 0 15 3
A __call() 0 9 2
A setRequest() 0 4 1
A getRequest() 0 4 1
A setLayoutContent() 0 4 1
1
<?php declare(strict_types=1);
2
/**
3
 * Starlit App.
4
 *
5
 * @copyright Copyright (c) 2016 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\App;
10
11
use Starlit\App\ViewHelper\Capturer;
12
use Starlit\App\ViewHelper\InlineScriptCapturer;
13
use Starlit\App\ViewHelper\MustacheTmplCapturer;
14
use Starlit\App\ViewHelper\Pagination;
15
use Starlit\App\ViewHelper\Url;
16
use Symfony\Component\HttpFoundation\Request;
17
use Starlit\App\ViewHelper\AbstractViewHelper;
18
19
class View implements ViewInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $scriptRootPath = 'app/views';
25
26
    /**
27
     * @var string
28
     */
29
    protected $fileExtension = 'html.php';
30
31
    /**
32
     * Default helpers.
33
     *
34
     * @var array
35
     */
36
    protected $helperClasses = [
37
        'capturer'             => Capturer::class,
38
        'mustacheTmplCapturer' => MustacheTmplCapturer::class,
39
        'pagination'           => Pagination::class,
40
        'url'                  => Url::class,
41
        'inlineScriptCapturer' => InlineScriptCapturer::class,
42
    ];
43
44
    /**
45
     * @var array
46
     */
47
    protected $helpers = [];
48
49
    /**
50
     * @var array
51
     */
52
    protected $variables = [];
53
54
    /**
55
     * @var string|null
56
     */
57
    protected $layoutScript;
58
59
    /**
60
     * @var string|null
61
     */
62
    protected $layoutContent;
63
64
    /**
65
     * @var Request
66
     */
67
    private $request;
68
69 15
    public function __construct(array $options = [])
70
    {
71 15
        $this->setOptions($options);
72 15
    }
73
74 15
    public function setOptions(array $options): void
75
    {
76 15
        if (isset($options['scriptRootPath'])) {
77 12
            $this->scriptRootPath = $options['scriptRootPath'];
78
        }
79
80 15
        if (isset($options['fileExtension'])) {
81 1
            $this->fileExtension = $options['fileExtension'];
82
        }
83 15
    }
84
85
    /**
86
     * Magic method to set view variables.
87
     *
88
     * @param   string $name
89
     * @param   mixed  $value
90
     */
91 4
    public function __set(string $name, $value): void
92
    {
93 4
        $this->variables[$name] = $value;
94 4
    }
95
96
    /**
97
     * Magic method to get view variables.
98
     *
99
     * @param string $name
100
     * @return mixed
101
     */
102 2
    public function __get(string $name)
103
    {
104 2
        if (!isset($this->variables[$name])) {
105 1
            return null;
106
        }
107
108 2
        return $this->variables[$name];
109
    }
110
111
    /**
112
     * Magic method to test if view variable is set.
113
     */
114 1
    public function __isset(string $name): bool
115
    {
116 1
        return isset($this->variables[$name]);
117
    }
118
119 1
    public function setLayout(string $script): void
120
    {
121 1
        $this->layoutScript = $script;
122 1
    }
123
124
    /**
125
     * Render output (returns) of the specified view script (with layout if that exists).
126
     *
127
     * @param string $script Relative script path (without file extension!). Eg. "some-script" or "admin/some-script".
128
     * @param bool   $renderLayout
129
     * @return string
130
     */
131 2
    public function render(string $script, bool $renderLayout = false): string
132
    {
133
        // Check If script should be rendered with a layout
134 2
        if ($renderLayout && !empty($this->layoutScript)) {
135 1
            $this->layoutContent = $this->renderScript($script);
136
137 1
            return $this->renderScript($this->layoutScript);
138
        } else {
139 1
            return $this->renderScript($script);
140
        }
141
    }
142
143
    /**
144
     * Internal method that does the actual script rendering.
145
     */
146 3
    protected function renderScript(string $script): string
147
    {
148 3
        $fullScriptPath = $this->scriptRootPath . '/' . $script . '.' . $this->fileExtension;
149
        // We don't unit test invalid script because it slows down the test process an entire second or more
150 3
        if (!file_exists($fullScriptPath)) {
151 1
            throw new \RuntimeException("Couldn't find view script \"{$fullScriptPath}\"");
152
        }
153
154 2
        ob_start();
155
156 2
        include $fullScriptPath;
157
158 2
        return ob_get_clean();
159
    }
160
161 2
    public function layoutContent(): string
162
    {
163 2
        return $this->layoutContent;
164
    }
165
166
    /**
167
     * Escape a string for output in view script.
168
     */
169 2
    public function escape(?string $string, int $flags = \ENT_QUOTES): string
170
    {
171 2
        return htmlspecialchars((string) $string, $flags, BaseApp::CHARSET);
172
    }
173
174
    /**
175
     * Returns view variable escaped for view script output.
176
     */
177 1
    public function getEscaped(string $name): string
178
    {
179 1
        if (empty($this->variables[$name])) {
180 1
            return '';
181
        }
182
183 1
        return $this->escape((string) $this->variables[$name]);
184
    }
185
186 2
    public function addHelperClass(string $helperName, string $className): void
187
    {
188 2
        $this->helperClasses[$helperName] = $className;
189 2
    }
190
191 3
    public function getHelper(string $helperName): AbstractViewHelper
192
    {
193
        // If helper has not already been instantiated
194 3
        if (!isset($this->helpers[$helperName])) {
195
            // Check that helper is defined
196 3
            if (!isset($this->helperClasses[$helperName])) {
197 1
                throw new \InvalidArgumentException("No helper named \"{$helperName}\"");
198
            }
199
200 2
            $this->helpers[$helperName] = new $this->helperClasses[$helperName]();
201 2
            $this->helpers[$helperName]->setView($this);
202
        }
203
204 2
        return $this->helpers[$helperName];
205
    }
206
207
    /**
208
     * Magic method to call view helpers.
209
     * If the helper has not defined __invoke(), the helper object will be returned.
210
     * Otherwise, the result of the __invoke() is returned.
211
     */
212 2
    public function __call(string $name, array $arguments = [])
213
    {
214 2
        $helper = $this->getHelper($name);
215 2
        if (is_callable($helper)) {
216 1
            return \call_user_func_array($helper, $arguments);
217
        }
218
219 1
        return $helper;
220
    }
221
222 1
    public function setRequest(Request $request): void
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
223
    {
224 1
        $this->request = $request;
225 1
    }
226
227 1
    public function getRequest(): Request
228
    {
229 1
        return $this->request;
230
    }
231
232 1
    public function setLayoutContent(string $value): void
233
    {
234 1
        $this->layoutContent = $value;
235 1
    }
236
}
237