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

SpaceShip   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 17 6
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 12
    protected function compile($expr, Context $context)
25
    {
26 12
        $left = $context->getExpressionCompiler()->compile($expr->left);
27 12
        $right = $context->getExpressionCompiler()->compile($expr->right);
28
29 12
        if ($left->isTypeKnown() && $right->isTypeKnown()) {
30 10
            if ($left->getValue() == $right->getValue()) {
31 3
                return new CompiledExpression(CompiledExpression::INTEGER, 0);
32 7
            } elseif ($left->getValue() < $right->getValue()) {
33 3
                return new CompiledExpression(CompiledExpression::INTEGER, -1);
34 4
            } elseif ($left->getValue() > $right->getValue()) {
35 4
                return new CompiledExpression(CompiledExpression::INTEGER, 1);
36
            }
37
        }
38
39 2
        return new CompiledExpression();
40
    }
41
}
42