Completed
Push — master ( 5f2691...53c2bd )
by Enrico
03:49
created

UnaryMinus::compile()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 7
nop 2
dl 0
loc 18
ccs 14
cts 14
cp 1
crap 7
rs 8.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression\Operators;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PHPSA\Compiler\Expression;
13
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
14
15
class UnaryMinus extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\UnaryMinus';
18
19
    /**
20
     * -{expr}
21
     *
22
     * @param \PhpParser\Node\Expr\UnaryMinus $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26 9
    protected function compile($expr, Context $context)
27
    {
28 9
        $left = $context->getExpressionCompiler()->compile($expr->expr);
29
30 9
        switch ($left->getType()) {
31 9
            case CompiledExpression::INTEGER:
32 9
            case CompiledExpression::DOUBLE:
33 9
            case CompiledExpression::NUMBER:
34 9
            case CompiledExpression::BOOLEAN:
35 9
            case CompiledExpression::STRING:
36 9
            case CompiledExpression::NULL:
37 8
                return CompiledExpression::fromZvalValue(
38 8
                    - (int) $left->getValue()
39 8
                );
40 1
        }
41
42 1
        return new CompiledExpression();
43
    }
44
}
45