Passed
Push — v5 ( d8021b...6f63c4 )
by Andrew
19:19 queued 14:18
created

SandboxView::renderString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 2
b 0
f 1
nc 2
nop 4
dl 0
loc 10
rs 10
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 Twig\Extension\SandboxExtension;
11
use Twig\Sandbox\SecurityPolicyInterface;
12
13
class SandboxView extends View
14
{
15
    // Public Properties
16
    // =========================================================================
17
18
    /**
19
     * @var SecurityPolicyInterface|null The security policy to use for the SandboxView
20
     */
21
    public ?SecurityPolicyInterface $securityPolicy = null;
22
23
    /**
24
     * @var WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null The error handler to use for the SandboxView
25
     */
26
    public WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null $sandboxErrorHandler = null;
27
28
    // Public Methods
29
    // =========================================================================
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function init(): void
35
    {
36
        parent::init();
37
        $this->sandboxErrorHandler = Craft::$app->getRequest()->getIsConsoleRequest() ? new ConsoleSandboxErrorHandler() : new WebSandboxErrorHandler();
38
        // Use the passed in SecurityPolicy, or create a default security policy
39
        $this->securityPolicy = $this->securityPolicy ?? new BlacklistSecurityPolicy();
40
        // Add the SandboxExtension with our SecurityPolicy lazily via ::registerTwigExtension()
41
        $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

41
        $this->registerTwigExtension(new SandboxExtension(/** @scrutinizer ignore-type */ $this->securityPolicy, true));
Loading history...
42
    }
43
}
44