Completed
Push — master ( bf375b...cc5661 )
by Rafał
07:07
created

ContainerNode   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 10.64 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 5
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B compile() 5 29 2

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
 * Container twig node.
19
 */
20
class ContainerNode extends \Twig_Node
21
{
22
    /**
23
     * @param \Twig_Node_Expression $name
24
     * @param \Twig_Node_Expression $parameters
25
     * @param \Twig_NodeInterface   $body
26
     * @param int                   $lineno
27
     * @param string                $tag
28
     */
29
    public function __construct(\Twig_Node $name, \Twig_Node_Expression $parameters = null, \Twig_NodeInterface $body, $lineno, $tag = null)
30
    {
31
        parent::__construct(['name' => $name, 'parameters' => $parameters, 'body' => $body], [], $lineno, $tag);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function compile(\Twig_Compiler $compiler)
38
    {
39
        $compiler
40
            ->addDebugInfo($this)
41
            ->write("\$containerService = \$this->env->getExtension('swp_container')->getContainerService();\n")
42
            ->write('$container = $containerService->getContainer(')->subcompile($this->getNode('name'))->raw(', ');
43 View Code Duplication
        if (!is_null($this->getNode('parameters'))) {
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...
44
            $compiler->subcompile($this->getNode('parameters'));
45
        } else {
46
            $compiler->raw('array()');
47
        }
48
        $compiler->raw(");\n")
49
            ->write("if (\$container->isVisible()) {\n")
50
            ->indent()
51
                ->write("echo \$container->renderOpenTag();\n")
52
                ->write("if (\$container->hasWidgets()) {\n")
53
                ->indent()
54
                    ->write("echo \$container->renderWidgets();\n")
55
                ->outdent()
56
                ->write("} else {\n")
57
                ->indent()
58
                    ->subcompile($this->getNode('body'))
59
                ->outdent()
60
                ->write("}\n")
61
                ->write("echo \$container->renderCloseTag();\n")
62
            ->outdent()
63
            ->write("}\n")
64
            ->write("unset(\$container);unset(\$containerService);\n");
65
    }
66
}
67