Completed
Push — master ( e5807f...3d4dba )
by Дмитрий
03:14 queued 19s
created

SpaceShip::compile()   C

Complexity

Conditions 11
Paths 31

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 11
eloc 19
nc 31
nop 2
dl 0
loc 26
rs 5.2653

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
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler\Expression\BinaryOp;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
10
use PHPSA\Context;
11
12
class SpaceShip extends AbstractExpressionCompiler
13
{
14
    protected $name = 'PhpParser\Node\Expr\BinaryOp\Spaceship';
15
16
    /**
17
     * It's used in conditions
18
     * {left-expr} <=> {right-expr}
19
     *
20
     * @param \PhpParser\Node\Expr\BinaryOp\Spaceship $expr
21
     * @param Context $context
22
     * @return CompiledExpression
23
     */
24
    protected function compile($expr, Context $context)
25
    {
26
        $left = $context->getExpressionCompiler()->compile($expr->left);
27
        $right = $context->getExpressionCompiler()->compile($expr->right);
28
29
        switch ($left->getType()) {
30
            case CompiledExpression::INTEGER:
31
            case CompiledExpression::DOUBLE:
32
            case CompiledExpression::BOOLEAN:
33
            case CompiledExpression::NUMBER:
34
            case CompiledExpression::NULL:
35
                switch ($right->getType()) {
36
                    case CompiledExpression::INTEGER:
37
                    case CompiledExpression::DOUBLE:
38
                    case CompiledExpression::BOOLEAN:
39
                    case CompiledExpression::NUMBER:
40
                    case CompiledExpression::NULL:
41
                        return new CompiledExpression(
42
                            CompiledExpression::INTEGER,
43
                            $left->getValue() <=> $right->getValue()
44
                        );
45
                }
46
        }
47
48
        return new CompiledExpression(CompiledExpression::BOOLEAN);
49
    }
50
}
51