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.

ArrayCompiler::compile()   C
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 6
cc 10
eloc 34
nc 64
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\Expression;
20
21
use TwigJs\JsCompiler;
22
use TwigJs\TypeCompilerInterface;
23
24
class ArrayCompiler implements TypeCompilerInterface
25
{
26
    public function getType()
27
    {
28
        return 'Twig_Node_Expression_Array';
29
    }
30
31
    public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
32
    {
33
        if (!$node instanceof \Twig_Node_Expression_Array) {
34
            throw new \RuntimeException(
35
                sprintf('$node must be an instanceof of \Expression_Array, but got "%s".', get_class($node))
36
            );
37
        }
38
39
40
        $pairs = $this->getKeyValuePairs($node);
41
42
        if ($isList = $this->isList($pairs)) {
43
            $compiler->raw('[');
44
        } elseif ($hasDynamicKeys = $this->hasDynamicKeys($pairs)) {
45
            $compiler->raw('twig.createObj(');
46
        } else {
47
            $compiler->raw('{');
48
        }
49
50
        $first = true;
51
        foreach ($pairs as $pair) {
52
            if (!$first) {
53
                $compiler->raw(', ');
54
            }
55
            $first = false;
56
57
            if ($isList) {
58
                $compiler->subcompile($pair['value']);
59
            } elseif ($hasDynamicKeys) {
0 ignored issues
show
Bug introduced by
The variable $hasDynamicKeys does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
60
                $compiler
61
                    ->subcompile($pair['key'])
62
                    ->raw(', ')
63
                    ->subcompile($pair['value'])
64
                ;
65
            } else {
66
                $compiler
67
                    ->subcompile($pair['key'])
68
                    ->raw(': ')
69
                    ->subcompile($pair['value'])
70
                ;
71
            }
72
        }
73
74
        if ($isList) {
75
            $compiler->raw(']');
76
        } elseif ($hasDynamicKeys) {
77
            $compiler->raw(')');
78
        } else {
79
            $compiler->raw('}');
80
        }
81
    }
82
83
    private function hasDynamicKeys(array $pairs)
84
    {
85
        foreach ($pairs as $pair) {
86
            if (!$pair['key'] instanceof \Twig_Node_Expression_Constant) {
87
                return true;
88
            }
89
        }
90
91
        return false;
92
    }
93
94
    private function isList(array $pairs)
95
    {
96
        for ($i=0,$c=count($pairs); $i<$c; $i++) {
97
            if (!$pairs[$i]['key'] instanceof \Twig_Node_Expression_Constant) {
98
                return false;
99
            }
100
101
            if ($pairs[$i]['key']->getAttribute('value') !== $i) {
102
                return false;
103
            }
104
        }
105
106
        return true;
107
    }
108
109
    private function getKeyValuePairs(\Twig_Node $node)
110
    {
111
        $pairs = array();
112
113
        foreach (array_chunk($node->getIterator()->getArrayCopy(), 2) as $pair) {
114
            $pairs[] = array(
115
                'key'   => $pair[0],
116
                'value' => $pair[1],
117
            );
118
        }
119
120
        return $pairs;
121
    }
122
}
123