SandboxView   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 2
Metric Value
wmc 2
eloc 7
c 10
b 0
f 2
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 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 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