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

GimmeListNode::compile()   F

Complexity

Conditions 13
Paths 768

Size

Total Lines 116
Code Lines 83

Duplication

Lines 16
Ratio 13.79 %

Importance

Changes 0
Metric Value
dl 16
loc 116
rs 2.4575
c 0
b 0
f 0
cc 13
eloc 83
nc 768
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 GimmeListNode extends \Twig_Node
21
{
22
    protected static $count = 1;
23
24
    protected $loop;
25
26
    /**
27
     * GimmeListNode constructor.
28
     *
29
     * @param \Twig_Node                        $variable
30
     * @param \Twig_Node                        $collectionType
31
     * @param \Twig_Node_Expression_Filter|null $collectionFilters
32
     * @param \Twig_Node_Expression|null        $parameters
33
     * @param \Twig_Node_Expression|null        $ignoreContext
34
     * @param \Twig_Node_Expression|null        $ifExpression
35
     * @param \Twig_NodeInterface|null          $else
36
     * @param \Twig_NodeInterface               $body
37
     * @param $lineno
38
     * @param null $tag
39
     */
40
    public function __construct(
41
        \Twig_Node $variable,
42
        \Twig_Node $collectionType,
43
        \Twig_Node_Expression_Filter $collectionFilters = null,
44
        \Twig_Node_Expression $parameters = null,
45
        \Twig_Node_Expression $ignoreContext = null,
46
        \Twig_Node_Expression $ifExpression = null,
47
        \Twig_NodeInterface $else = null,
48
        \Twig_NodeInterface $body,
49
        $lineno,
50
        $tag = null
51
    ) {
52
        $body = new \Twig_Node([$body, $this->loop = new \Twig_Node_ForLoop($lineno, $tag)]);
53
54
        if (null !== $ifExpression) {
55
            $body = new \Twig_Node_If(new \Twig_Node([$ifExpression, $body]), null, $lineno, $tag);
56
        }
57
58
        $nodes = [
59
            'variable' => $variable,
60
            'collectionType' => $collectionType,
61
            'body' => $body,
62
        ];
63
64
        if (!is_null($parameters)) {
65
            $nodes['parameters'] = $parameters;
66
        }
67
68
        if (!is_null($ignoreContext)) {
69
            $nodes['ignoreContext'] = $ignoreContext;
70
        }
71
72
        if (!is_null($collectionFilters)) {
73
            $nodes['collectionFilters'] = $collectionFilters;
74
        }
75
76
        if (!is_null($ifExpression)) {
77
            $nodes['ifExpression'] = $ifExpression;
78
        }
79
80
        if (!is_null($else)) {
81
            $nodes['else'] = $else;
82
        }
83
84
        parent::__construct($nodes, ['with_loop' => true, 'ifexpr' => null !== $ifExpression], $lineno, $tag);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function compile(\Twig_Compiler $compiler)
91
    {
92
        $i = self::$count++;
93
94
        $collectionTypeName = $this->getNode('collectionType')->getNode(0)->getAttribute('name');
95
96
        $compiler
97
            ->addDebugInfo($this);
98
99
        if ($this->hasNode('collectionFilters')) {
100
            $compiler->write("\$context['_collection_type_filters'] = [];\n");
101
            $compiler->write("\$context['".$collectionTypeName."'] = null;\n");
102
            $compiler->write("\$context['_collection_type_filters'] = ")->subcompile($this->getNode('collectionFilters'))->raw("['_collection_type_filters']; unset(\$context['".$collectionTypeName."']['_collection_type_filters']);\n");
103
104 View Code Duplication
            if ($this->hasNode('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...
105
                $compiler->write('$parameters = array_merge(')->subcompile($this->getNode('parameters'))->raw(", \$context['_collection_type_filters']);\n");
106
            } else {
107
                $compiler->write("\$parameters = \$context['_collection_type_filters'];\n");
108
            }
109 View Code Duplication
        } else {
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...
110
            if ($this->hasNode('parameters')) {
111
                $compiler->raw('$parameters = ')->subcompile($this->getNode('parameters'))->raw(";\n");
112
            } else {
113
                $compiler->raw("\$parameters = [];\n");
114
            }
115
        }
116
117
        $compiler->write('$swpCollectionMetaLoader'.$i." = \$this->env->getExtension('swp_gimme')->getLoader();\n");
118 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...
119
            $compiler->write('$swpContext'.$i."GimmeList = \$this->env->getExtension('swp_gimme')->getContext();\n");
120
            $compiler->write('$swpIgnoreContext'.$i.'GimmeList = $swpContext'.$i.'GimmeList->temporaryUnset(')->subcompile($this->getNode('ignoreContext'))->raw(");\n");
121
        }
122
        $compiler->write('')->subcompile($this->getNode('collectionType'))->raw(' = twig_ensure_traversable($swpCollectionMetaLoader'.$i.'->load("')->raw($collectionTypeName)->raw('", ');
123
        $compiler->raw('$parameters');
124
        $compiler->raw(", \SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface::COLLECTION));\n");
125
126
        // the (array) cast bypasses a PHP 5.2.6 bug
127
        $compiler->write("\$context['_parent'] = (array) \$context;\n");
128
129
        if ($this->hasNode('else')) {
130
            $compiler->write("\$context['_iterated'] = false;\n");
131
        }
132
133
        if ($this->getAttribute('with_loop')) {
134
            $compiler
135
                ->write("\$context['loop'] = array(\n")
136
                ->write("  'parent' => \$context['_parent'],\n")
137
                ->write("  'index0' => 0,\n")
138
                ->write("  'index'  => 1,\n")
139
                ->write("  'first'  => true,\n")
140
                ->write(");\n");
141
142
            if (!$this->getAttribute('ifexpr') && $this->getNode('collectionType')) {
143
                $compiler
144
                    ->write('if (is_array(')->subcompile($this->getNode('collectionType'))->raw(') || (is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof Countable)) {\n")
145
                    ->indent()
146
                    ->write('$length = count(')->subcompile($this->getNode('collectionType'))->raw(");\n")
147
                    ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
148
                    ->write("\$context['loop']['revindex'] = \$length;\n")
149
                    ->write("\$context['loop']['length'] = \$length;\n")
150
                    ->write("\$context['loop']['totalLength'] = \$length;\n")
151
                    ->write("\$context['loop']['last'] = 1 === \$length;\n")
152
                    ->outdent()
153
                    ->write("}\n");
154
155
                $compiler
156
                    ->write('if(is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof \SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection) {\n")
157
                    ->indent()
158
                    ->write('$context[\'loop\'][\'totalLength\'] = ')->subcompile($this->getNode('collectionType'))->raw("->getTotalItemsCount();\n")
159
                    ->outdent()
160
                    ->write("}\n");
161
            }
162
        }
163
164
        $this->loop->setAttribute('else', $this->hasNode('else'));
165
        $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
166
        $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
167
168
        if (null !== $this->getNode('collectionType')) {
169
            $compiler
170
                ->write('foreach (')
171
                ->subcompile($this->getNode('collectionType'))
172
                ->raw(' as $_key')
173
                ->raw(' => ')
174
                ->subcompile($this->getNode('variable'))
175
                ->raw(") {\n")
176
                ->indent()
177
                ->subcompile($this->getNode('body'))
178
                ->outdent()
179
                ->write("}\n");
180
        }
181
182
        if ($this->hasNode('else')) {
183
            $compiler
184
                ->write("if (!\$context['_iterated']) {\n")
185
                ->indent()
186
                ->subcompile($this->getNode('else'))
187
                ->outdent()
188
                ->write("}\n");
189
        }
190
191
        if ($this->hasNode('ignoreContext')) {
192
            $compiler->write('$swpContext'.$i.'GimmeList->restoreTemporaryUnset($swpIgnoreContext'.$i."GimmeList);\n");
193
        }
194
        $compiler->write("\$_parent = \$context['_parent'];\n");
195
196
        // remove some "private" loop variables (needed for nested loops)
197
        $compiler->write('unset($context[\''.$this->getNode('variable')->getNode(0)->getAttribute('name').'\'], $context[\'_iterated\'], $context[\''.$collectionTypeName.'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
198
199
        if ($this->hasNode('collectionFilters')) {
200
            $compiler->write("unset(\$context['_collection_type_filters']);\n");
201
        }
202
203
        // keep the values set in the inner context for variables defined in the outer context
204
        $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
205
    }
206
}
207