Passed
Push — v4 ( 3051ad...6c9e4e )
by Andrew
20:01 queued 04:42
created

SandboxView   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 2
Metric Value
eloc 25
c 9
b 0
f 2
dl 0
loc 77
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderString() 0 10 2
A init() 0 8 2
A renderObjectTemplate() 0 10 2
A renderTemplate() 0 10 2
1
<?php
2
3
namespace nystudio107\crafttwigsandbox\web;
4
5
use Craft;
6
use craft\web\View;
7
use nystudio107\crafttwigsandbox\console\SandboxErrorHandler as ConsoleSandboxErrorHandler;
8
use nystudio107\crafttwigsandbox\twig\BlacklistSecurityPolicy;
9
use nystudio107\crafttwigsandbox\web\SandboxErrorHandler as WebSandboxErrorHandler;
10
use Throwable;
11
use Twig\Extension\SandboxExtension;
12
use Twig\Sandbox\SecurityPolicyInterface;
13
14
class SandboxView extends View
15
{
16
    // Public Properties
17
    // =========================================================================
18
19
    /**
20
     * @var SecurityPolicyInterface|null The security policy to use for the SandboxView
21
     */
22
    public ?SecurityPolicyInterface $securityPolicy = null;
23
24
    // Protected Properties
25
    // =========================================================================
26
27
    /**
28
     * @var WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null The error handler to use for the SandboxView
29
     */
30
    protected WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null $sandboxErrorHandler = null;
31
32
    // Public Methods
33
    // =========================================================================
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function init(): void
39
    {
40
        parent::init();
41
        $this->sandboxErrorHandler = Craft::$app->getRequest()->getIsConsoleRequest() ? new ConsoleSandboxErrorHandler() : new WebSandboxErrorHandler();
42
        // Use the passed in SecurityPolicy, or create a default security policy
43
        $this->securityPolicy = $this->securityPolicy ?? new BlacklistSecurityPolicy();
44
        // Add the SandboxExtension with our SecurityPolicy lazily via ::registerTwigExtension()
45
        $this->registerTwigExtension(new SandboxExtension($this->securityPolicy, true));
0 ignored issues
show
Bug introduced by
It seems like $this->securityPolicy can also be of type null; however, parameter $policy of Twig\Extension\SandboxExtension::__construct() does only seem to accept Twig\Sandbox\SecurityPolicyInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        $this->registerTwigExtension(new SandboxExtension(/** @scrutinizer ignore-type */ $this->securityPolicy, true));
Loading history...
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function renderObjectTemplate(string $template, mixed $object, array $variables = [], string $templateMode = self::TEMPLATE_MODE_SITE): string
52
    {
53
        $result = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
54
        try {
55
            $result = parent::renderObjectTemplate($template, $object, $variables, $templateMode);
56
        } catch (Throwable $e) {
57
            $this->sandboxErrorHandler->handleException($e);
0 ignored issues
show
Bug introduced by
The method handleException() does not exist on null. ( Ignorable by Annotation )

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

57
            $this->sandboxErrorHandler->/** @scrutinizer ignore-call */ 
58
                                        handleException($e);

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...
58
        }
59
60
        return $result;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function renderString(string $template, array $variables = [], string $templateMode = self::TEMPLATE_MODE_SITE, bool $escapeHtml = false): string
67
    {
68
        $result = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
69
        try {
70
            $result = parent::renderString($template, $variables, $templateMode, $escapeHtml);
71
        } catch (Throwable $e) {
72
            $this->sandboxErrorHandler->handleException($e);
73
        }
74
75
        return $result;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function renderTemplate(string $template, array $variables = [], ?string $templateMode = null): string
82
    {
83
        $result = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
84
        try {
85
            $result = parent::renderTemplate($template, $variables, $templateMode);
86
        } catch (Throwable $e) {
87
            $this->sandboxErrorHandler->handleException($e);
88
        }
89
90
        return $result;
91
    }
92
}
93