PhpTemplateRenderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 13
c 0
b 0
f 0
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View;
6
7
use Throwable;
8
9
use function extract;
10
use function func_get_arg;
11
use function ob_end_clean;
12
use function ob_get_clean;
13
use function ob_get_level;
14
use function ob_implicit_flush;
15
use function ob_start;
16
17
/**
18
 * `PhpTemplateRenderer` renders the PHP views.
19
 */
20
final class PhpTemplateRenderer implements TemplateRendererInterface
21
{
22 64
    public function render(ViewInterface $view, string $template, array $parameters): string
23
    {
24 64
        $renderer = function (): void {
25
            /** @psalm-suppress MixedArgument */
26 64
            extract(func_get_arg(1), EXTR_OVERWRITE);
27
            /** @psalm-suppress UnresolvableInclude */
28 64
            require func_get_arg(0);
29 64
        };
30
31 64
        $obInitialLevel = ob_get_level();
32 64
        ob_start();
33 64
        ob_implicit_flush(false);
34
        try {
35
            /** @psalm-suppress PossiblyInvalidFunctionCall,PossiblyNullFunctionCall */
36 64
            $renderer->bindTo($view)($template, $parameters);
37 62
            return ob_get_clean();
38 4
        } catch (Throwable $e) {
39 4
            while (ob_get_level() > $obInitialLevel) {
40 4
                ob_end_clean();
41
            }
42 4
            throw $e;
43
        }
44
    }
45
}
46