Passed
Pull Request — master (#142)
by Sergei
02:15
created

PhpEvalStringRenderer::render()   A

Complexity

Conditions 5
Paths 14

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.9256

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 20
ccs 10
cts 15
cp 0.6667
rs 9.4888
cc 5
nc 14
nop 3
crap 5.9256
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\StringRenderer;
6
7
use Yiisoft\View\View;
8
9
final class PhpEvalStringRenderer implements StringRendererInterface
10
{
11 1
    public function render(View $view, string $template, array $parameters): string
12
    {
13 1
        $renderer = function () {
14 1
            extract(func_get_arg(1), EXTR_OVERWRITE);
15 1
            eval(func_get_arg(0));
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
16 1
        };
17
18 1
        $obInitialLevel = ob_get_level();
19 1
        ob_start();
20 1
        PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0);
21
        try {
22 1
            $renderer->bindTo($view)($template, $parameters);
23 1
            return ob_get_clean();
24
        } catch (\Throwable $e) {
25
            while (ob_get_level() > $obInitialLevel) {
26
                if (!@ob_end_clean()) {
27
                    ob_clean();
28
                }
29
            }
30
            throw $e;
31
        }
32
    }
33
}
34