Completed
Push — master ( f036f0...b950dc )
by Rafał
02:16
created

GimmeNode   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
B 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
21
{
22
    private static $count = 1;
23
24
    /**
25
     * GimmeNode constructor.
26
     *
27
     * @param \Twig_Node                 $annotation
28
     * @param \Twig_Node_Expression|null $parameters
29
     * @param \Twig_Node_Expression|null $ignoreContext
30
     * @param \Twig_NodeInterface        $body
31
     * @param $lineno
32
     * @param null $tag
33
     */
34
    public function __construct(
35
        \Twig_Node $annotation,
36
        \Twig_Node_Expression $parameters = null,
37
        \Twig_Node_Expression $ignoreContext = null,
38
        \Twig_NodeInterface $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_gimme')->getLoader();\n");
68 View Code Duplication
        if ($this->hasNode('ignoreContext')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $compiler->write('$swpContext'.$i."Gimme = \$this->env->getExtension('swp_gimme')->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