Completed
Pull Request — master (#116)
by Enrico
03:35
created

AbstractOperator::compare()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Compiler\Expression\Operators\Comparison;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\Expression;
8
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
9
10
abstract class AbstractOperator extends AbstractExpressionCompiler
11
{
12
    /**
13
     * Do compare
14
     *
15
     * @param $left
16
     * @param $right
17
     * @return boolean
18
     */
19
    abstract public function compare($left, $right);
20
21
    /**
22
     * {expr} $operator {expr}
23
     *
24
     * @param \PhpParser\Node\Expr\BinaryOp $expr
25
     * @param Context $context
26
     * @return CompiledExpression
27
     */
28
    protected function compile($expr, Context $context)
29
    {
30
        $left = $context->getExpressionCompiler()->compile($expr->left);
31
        $right = $context->getExpressionCompiler()->compile($expr->right);
32
33
        switch ($left->getType()) {
34
            case CompiledExpression::INTEGER:
35
            case CompiledExpression::DOUBLE:
36
                switch ($right->getType()) {
37
                    case CompiledExpression::INTEGER:
38
                    case CompiledExpression::DOUBLE:
39
                        return new CompiledExpression(
40
                            CompiledExpression::BOOLEAN,
41
                            $this->compare($left->getValue(), $right->getValue())
42
                        );
43
                }
44
                break;
45
        }
46
47
        return new CompiledExpression(CompiledExpression::BOOLEAN);
48
    }
49
}
50