Completed
Push — master ( 8f0d0f...15736a )
by Дмитрий
06:53
created

ArrayOp::compile()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.0125

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 10
nop 2
dl 0
loc 33
ccs 19
cts 20
cp 0.95
crap 10.0125
rs 4.8196
c 0
b 0
f 0

How to fix   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
namespace PHPSA\Compiler\Expression;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\Expression;
8
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
9
10
class ArrayOp extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\Array_';
13
14
    /**
15
     * [] array()
16
     *
17
     * @param \PhpParser\Node\Expr\Array_ $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21 24
    protected function compile($expr, Context $context)
22
    {
23 24
        $compiler = $context->getExpressionCompiler();
24
25 24
        if ($expr->items === []) {
26 9
            return new CompiledExpression(CompiledExpression::ARR, []);
27
        }
28
29 17
        $resultArray = [];
30
31 17
        foreach ($expr->items as $item) {
32 17
            if ($item === null) {
33
                continue;
34
            }
35
36 17
            $compiledValueResult = $compiler->compile($item->value);
37 17
            if ($item->key) {
38 6
                $compiledKeyResult = $compiler->compile($item->key);
39 6
                switch ($compiledKeyResult->getType()) {
40 6
                    case CompiledExpression::INTEGER:
41 5
                    case CompiledExpression::DOUBLE:
42 5
                    case CompiledExpression::BOOLEAN:
43 5
                    case CompiledExpression::NULL:
44 5
                    case CompiledExpression::STRING:
45 6
                        $resultArray[$compiledKeyResult->getValue()] = $compiledValueResult->getValue();
46
                }
47
            } else {
48 17
                $resultArray[] = $compiledValueResult->getValue();
49
            }
50
        }
51
52 17
        return new CompiledExpression(CompiledExpression::ARR, $resultArray);
53
    }
54
}
55