Completed
Push — master ( be4467...8bad52 )
by Дмитрий
02:43
created

ArrayOp::compile()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10.0658

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 10
nop 2
dl 0
loc 33
ccs 21
cts 23
cp 0.913
crap 10.0658
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 2
    protected function compile($expr, Context $context)
22
    {
23 2
        $compiler = $context->getExpressionCompiler();
24
25 2
        if ($expr->items === []) {
26 1
            return new CompiledExpression(CompiledExpression::ARR, []);
27
        }
28
29 2
        $resultArray = [];
30
31 2
        foreach ($expr->items as $item) {
32 2
            if ($item === null) {
33
                continue;
34
            }
35
36 2
            $compiledValueResult = $compiler->compile($item->value);
37 2
            if ($item->key) {
38 2
                $compiledKeyResult = $compiler->compile($item->key);
39 2
                switch ($compiledKeyResult->getType()) {
40 2
                    case CompiledExpression::INTEGER:
41 2
                    case CompiledExpression::DOUBLE:
42 2
                    case CompiledExpression::BOOLEAN:
43 2
                    case CompiledExpression::NULL:
44 2
                    case CompiledExpression::STRING:
45 2
                        $resultArray[$compiledKeyResult->getValue()] = $compiledValueResult->getValue();
46 2
                }
47 2
            } else {
48
                $resultArray[] = $compiledValueResult->getValue();
49
            }
50 2
        }
51
52 2
        return new CompiledExpression(CompiledExpression::ARR, $resultArray);
53
    }
54
}
55