Test Failed
Pull Request — master (#61)
by Rustam
03:01 queued 34s
created

TemplateRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Twig;
6
7
use Throwable;
8
use Twig\Environment;
9
use Yiisoft\View\TemplateRendererInterface;
10
use Yiisoft\View\ViewInterface;
11
12
use function array_merge;
13
use function ob_end_clean;
14
use function ob_get_clean;
15
use function ob_get_level;
16
use function ob_implicit_flush;
17
use function ob_start;
18
use function str_replace;
19
20
/**
21
 * TemplateRenderer allows using Twig with a View service.
22
 */
23
final class TemplateRenderer implements TemplateRendererInterface
24
{
25
    public function __construct(private Environment $environment)
26
    {
27
    }
28
29
    public function render(ViewInterface $view, string $template, array $parameters): string
30
    {
31
        $environment = $this->environment;
32
        $renderer = function () use ($view, $template, $parameters, $environment): void {
33
            $template = str_replace([$view->getBasePath(), $view->getContext()?->getViewPath() ?? ''], '', $template);
0 ignored issues
show
Bug introduced by
The method getContext() does not exist on Yiisoft\View\ViewInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
            $template = str_replace([$view->getBasePath(), $view->/** @scrutinizer ignore-call */ getContext()?->getViewPath() ?? ''], '', $template);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
            $file = str_replace('\\', '/', $template);
35
36
            echo $environment->render($file, array_merge($parameters, ['this' => $view]));
37
        };
38
39
        $obInitialLevel = ob_get_level();
40
        ob_start();
41
        ob_implicit_flush(false);
42
43
        try {
44
            /** @psalm-suppress PossiblyInvalidFunctionCall,PossiblyNullFunctionCall */
45
            $renderer->bindTo($view)();
46
            return ob_get_clean();
47
        } catch (Throwable $e) {
48
            while (ob_get_level() > $obInitialLevel) {
49
                ob_end_clean();
50
            }
51
            throw $e;
52
        }
53
    }
54
}
55