GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ForCompiler::compile()   F
last analyzed

Complexity

Conditions 12
Paths 449

Size

Total Lines 120
Code Lines 81

Duplication

Lines 9
Ratio 7.5 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 120
rs 3.5232
cc 12
eloc 81
nc 449
nop 2

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
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace TwigJs\Compiler;
20
21
use TwigJs\JsCompiler;
22
use TwigJs\TypeCompilerInterface;
23
24
class ForCompiler implements TypeCompilerInterface
25
{
26
    private $count = 0;
27
28
    public function getType()
29
    {
30
        return 'Twig_Node_For';
31
    }
32
33
    public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
34
    {
35
        if (!$node instanceof \Twig_Node_For) {
36
            throw new \RuntimeException(
37
                sprintf(
38
                    '$node must be an instanceof of \For, but got "%s".',
39
                    get_class($node)
40
                )
41
            );
42
        }
43
44
        $count = $this->count++;
45
        $suffix = $count > 0 ? $count : '';
46
        $seqName = 'seq'.$suffix;
47
        $keyName = 'k'.$suffix;
48
        $valueName = 'v'.$suffix;
49
        $iteratedName = 'iterated'.$suffix;
50
        $loopName = 'loop'.$suffix;
51
52
        if ($count > 0) {
53
            $compiler->enterScope();
54
        }
55
56
        $compiler
57
            ->setVar('_seq', $seqName)
58
            ->setVar('_iterated', $iteratedName)
59
            ->setVar('loop', $loopName)
60
            ->addDebugInfo($node)
61
        ;
62
63
        // keep parent reference as some might rely on this
64
        if (0 === $count) {
65
            $compiler->write("context['_parent'] = context;\n");
66
        }
67
68
        $compiler
69
            ->write("var $seqName = ")
70
//             ->write("\$context['_seq'] = twig_ensure_traversable(")
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
            ->subcompile($node->getNode('seq'))
72
//             ->raw(");\n")
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
            ->raw(";\n")
74
        ;
75
76
        if ($this->hasElseNode($node)) {
77
            $compiler->write("var $iteratedName = false;\n");
78
        }
79
80
        if ($node->getAttribute('with_loop')) {
81
            $compiler
82
                ->write("var $loopName = {\n")
83
                ->indent()
84
            ;
85
86
            if ($count > 0) {
87
                $parentSuffix = ($count-1 > 0) ? $count - 1 : '';
88
89
                $compiler
90
                    ->write("'parent': loop$parentSuffix,\n")
91
                ;
92
            }
93
94
            $compiler
95
                ->write("'index0': 0,\n")
96
                ->write("'index': 1,\n")
97
                ->write("'first': true\n")
98
                ->outdent()
99
                ->write("};\n")
100
            ;
101
102
            if (false === $node->getAttribute('ifexpr')) {
103
                $compiler
104
                    ->write("if (twig.countable($seqName)) {\n")
105
                    ->indent()
106
                    ->write("var length = twig.count($seqName);\n")
107
                    ->write("{$loopName}['revindex0'] = length - 1;\n")
108
                    ->write("{$loopName}['revindex'] = length;\n")
109
                    ->write("{$loopName}['length'] = length;\n")
110
                    ->write("{$loopName}['last'] = 1 === length;\n")
111
                    ->outdent()
112
                    ->write("}\n")
113
                ;
114
            }
115
        }
116
117
        $ref = new \ReflectionProperty($node, 'loop');
118
        $ref->setAccessible(true);
119
        $loop = $ref->getValue($node);
120
        $loop->setAttribute('else', $this->hasElseNode($node));
121
        $loop->setAttribute('with_loop', $node->getAttribute('with_loop'));
122
        $loop->setAttribute('ifexpr', $node->getAttribute('ifexpr'));
123
124
        $compiler
125
            ->write("twig.forEach($seqName, function($valueName, $keyName) {\n")
126
            ->indent()
127
            ->write("")
128
            ->subcompile($node->getNode('key_target'))
129
            ->raw(" = $keyName;\n")
130
            ->write("")
131
            ->subcompile($node->getNode('value_target'))
132
            ->raw(" = $valueName;\n")
133
            ->subcompile($node->getNode('body'))
134
            ->outdent()
135
            ->write("}, this);\n")
136
        ;
137
138 View Code Duplication
        if ($this->hasElseNode($node)) {
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...
139
            $compiler
140
                ->write("if (!$iteratedName) {\n")
141
                ->indent()
142
                ->subcompile($node->getNode('else'))
143
                ->outdent()
144
                ->write("}\n")
145
            ;
146
        }
147
148
        if ($count > 0) {
149
            $compiler->leaveScope();
150
        }
151
        $this->count = $count;
152
    }
153
154 View Code Duplication
    private function hasElseNode(\Twig_NodeInterface $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
155
    {
156
        if (!$node->hasNode('else')) {
157
            return false;
158
        }
159
160
        if (null === $node->getNode('else')) {
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
161
            return false;
162
        }
163
164
        return true;
165
    }
166
}
167