ShopUiCompatibilityDefineTwigNode   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 17
c 1
b 0
f 0
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compile() 0 17 1
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\ShopUiCompatibility\Twig\Node;
9
10
use Twig_Compiler;
0 ignored issues
show
Bug introduced by
The type Twig_Compiler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Twig_Node;
0 ignored issues
show
Bug introduced by
The type Twig_Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Twig_Node_Expression;
0 ignored issues
show
Bug introduced by
The type Twig_Node_Expression was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class ShopUiCompatibilityDefineTwigNode extends Twig_Node
15
{
16
    public const REQUIRED_VALUE = '___REQUIRED___';
17
18
    /**
19
     * @param string $name
20
     * @param \Twig_Node_Expression $value
21
     * @param int $line
22
     * @param string|null $tag
23
     */
24
    public function __construct(string $name, Twig_Node_Expression $value, int $line, ?string $tag = null)
25
    {
26
        parent::__construct(['value' => $value], ['name' => $name], $line, $tag);
27
    }
28
29
    /**
30
     * @param \Twig_Compiler $compiler
31
     *
32
     * @return void
33
     */
34
    public function compile(Twig_Compiler $compiler): void
35
    {
36
        $key = "'" . $this->getAttribute('name') . "'";
37
        $requiredValue = "'" . self::REQUIRED_VALUE . "'";
38
39
        $compiler
40
            ->addDebugInfo($this)
41
            ->raw('if (!array_key_exists(' . $key . ', $context)) {')
42
            ->raw('$context[' . $key . '] = [];')
43
            ->raw('}')
44
            ->raw('$context[' . $key . '] = array_replace_recursive(')
45
            ->subcompile($this->getNode('value'))
46
            ->raw(', $context[' . $key . ']);')
47
            ->raw('array_walk_recursive($context[' . $key . '], function($value, $key) {')
48
            ->raw('if ($value === ' . $requiredValue . ') {')
49
            ->raw('throw new Twig_Error_Runtime(\'required <em>' . $this->getAttribute('name') . '</em> property "\'.$key.\'" is not defined for "' . $this->getTemplateName() . ':' . $this->getTemplateLine() . '"\'); }')
50
            ->raw('});');
51
    }
52
}
53