Passed
Push — master ( 808cac...64bfdf )
by Evgeniy
02:17
created

PhpTemplateRenderer::render()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 9.8333
cc 4
nc 10
nop 3
crap 4
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 52
    public function render(BaseView $view, string $template, array $params): string
23
    {
24 52
        $renderer = function (): void {
25
            /** @psalm-suppress MixedArgument */
26 52
            extract(func_get_arg(1), EXTR_OVERWRITE);
27
            /** @psalm-suppress UnresolvableInclude */
28 52
            require func_get_arg(0);
29 52
        };
30
31 52
        $obInitialLevel = ob_get_level();
32 52
        ob_start();
33
        /** @psalm-suppress PossiblyFalseArgument */
34 52
        PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0);
35
        try {
36
            /** @psalm-suppress PossiblyInvalidFunctionCall */
37 52
            $renderer->bindTo($view)($template, $params);
38 51
            return ob_get_clean();
39 2
        } catch (Throwable $e) {
40 2
            while (ob_get_level() > $obInitialLevel) {
41 2
                ob_end_clean();
42
            }
43 2
            throw $e;
44
        }
45
    }
46
}
47