Completed
Push — master ( 064b7a...1a9aa6 )
by Дмитрий
05:33 queued 02:36
created

SpaceShip::compile()   F

Complexity

Conditions 20
Paths 265

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 28
nc 265
nop 2
dl 0
loc 35
ccs 30
cts 30
cp 1
crap 20
rs 3.6338
c 1
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
 * @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 11
    protected function compile($expr, Context $context)
25
    {
26 11
        $left = $context->getExpressionCompiler()->compile($expr->left);
27 11
        $right = $context->getExpressionCompiler()->compile($expr->right);
28
29 11
        switch ($left->getType()) {
30 11
            case CompiledExpression::INTEGER:
31 11
            case CompiledExpression::DOUBLE:
32 11
            case CompiledExpression::BOOLEAN:
33 11
            case CompiledExpression::NUMBER:
34 11
            case CompiledExpression::NULL:
35 11
            case CompiledExpression::ARR:
36 11
            case CompiledExpression::OBJECT:
37 11
            case CompiledExpression::STRING:
38 10
                switch ($right->getType()) {
39 10
                    case CompiledExpression::INTEGER:
40 10
                    case CompiledExpression::DOUBLE:
41 10
                    case CompiledExpression::BOOLEAN:
42 10
                    case CompiledExpression::NUMBER:
43 10
                    case CompiledExpression::NULL:
44 10
                    case CompiledExpression::ARR:
45 10
                    case CompiledExpression::OBJECT:
46 10
                    case CompiledExpression::STRING:
47 9
                        if ($left->getValue() == $right->getValue()) {
48 2
                            return new CompiledExpression(CompiledExpression::INTEGER, 0);
49 7
                        } elseif ($left->getValue() < $right->getValue()) {
50 3
                            return new CompiledExpression(CompiledExpression::INTEGER, -1);
51 4
                        } elseif ($left->getValue() > $right->getValue()) {
52 4
                            return new CompiledExpression(CompiledExpression::INTEGER, 1);
53
                        }
54 1
                }
55 2
        }
56
57 2
        return new CompiledExpression();
58
    }
59
}
60