GimmeNode   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 4
loc 72
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
A compile() 4 30 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Twig\Node;
16
17
/**
18
 * Gimme twig node.
19
 */
20
class GimmeNode extends \Twig\Node\Node
21
{
22
    private static $count = 1;
23
24
    /**
25
     * GimmeNode constructor.
26
     *
27
     * @param \Twig\Node\Node      $annotation
28
     * @param \Twig\Node\Node|null $parameters
29
     * @param \Twig\Node\Node|null $ignoreContext
30
     * @param \Twig\Node\Node      $body
31
     * @param int                  $lineno
32
     * @param null                 $tag
33
     */
34
    public function __construct(
35
        \Twig\Node\Node $annotation,
36
        \Twig\Node\Expression\AbstractExpression $parameters = null,
37
        \Twig\Node\Expression\AbstractExpression $ignoreContext = null,
38
        \Twig\Node\Node $body,
39
        $lineno,
40
        $tag = null
41
    ) {
42
        $nodes = [
43
            'body' => $body,
44
            'annotation' => $annotation,
45
        ];
46
47
        if (!is_null($parameters)) {
48
            $nodes['parameters'] = $parameters;
49
        }
50
51
        if (!is_null($ignoreContext)) {
52
            $nodes['ignoreContext'] = $ignoreContext;
53
        }
54
55
        parent::__construct($nodes, [], $lineno, $tag);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function compile(\Twig\Compiler $compiler)
62
    {
63
        $i = self::$count++;
64
65
        $compiler
66
            ->addDebugInfo($this)
67
            ->write('$swpMetaLoader'.$i." = \$this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getLoader();\n");
68 View Code Duplication
        if ($this->hasNode('ignoreContext')) {
69
            $compiler->write('$swpContext'.$i."Gimme = \$this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getContext();\n");
70
            $compiler->write('$swpIgnoreContext'.$i.'Gimme = $swpContext'.$i.'Gimme->temporaryUnset(')->subcompile($this->getNode('ignoreContext'))->raw(");\n");
71
        }
72
        $compiler
73
            ->write('')->subcompile($this->getNode('annotation'))->raw(' = $swpMetaLoader'.$i.'->load("')->raw($this->getNode('annotation')->getNode(0)->getAttribute('name'))->raw('", ');
74
        if ($this->hasNode('parameters')) {
75
            $compiler->subcompile($this->getNode('parameters'));
76
        } else {
77
            $compiler->raw('null');
78
        }
79
        $compiler->raw(");\n")
80
            ->write('if (')->subcompile($this->getNode('annotation'))->raw(" !== false) {\n")
81
            ->indent()
82
                ->subcompile($this->getNode('body'))
83
            ->outdent()
84
            ->write("}\n");
85
        if ($this->hasNode('ignoreContext')) {
86
            $compiler->write('$swpContext'.$i.'Gimme->restoreTemporaryUnset($swpIgnoreContext'.$i."Gimme);\n");
87
        }
88
        $compiler
89
            ->write('unset(')->subcompile($this->getNode('annotation'))->raw(');');
90
    }
91
}
92