Completed
Pull Request — master (#149)
by Enrico
03:38
created

EmptyOp::compile()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
nc 7
nop 2
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 6
rs 8.6737
c 1
b 0
f 0
1
<?php
2
3
namespace PHPSA\Compiler\Expression;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\Expression;
8
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
9
use PhpParser\Node\Expr\Variable;
10
use PhpParser\Node\Name;
11
12
class EmptyOp extends AbstractExpressionCompiler
13
{
14
    protected $name = 'PhpParser\Node\Expr\Empty_';
15
16
    /**
17
     * empty({expr]})
18
     *
19
     * @param \PhpParser\Node\Expr\Empty_ $expr
20
     * @param Context $context
21
     * @return CompiledExpression
22
     */
23 5
    protected function compile($expr, Context $context)
24
    {
25 5
        if ($expr->expr instanceof Variable) {
26 5
            $varName = $expr->expr->name;
27
28 5
            if ($varName instanceof Name) {
29 5
                $varName = $varName->parts[0];
30 5
            }
31
32 5
            $variable = $context->getSymbol($varName);
33
34 5
            if ($variable) {
35 4
                $variable->incUse();
36
37 4
                if ($variable->getValue() !== null && $variable->getValue() != false) {
38 1
                    return CompiledExpression::fromZvalValue(true);
39
                }
40 3
            }
41 4
        }
42
43 4
        return CompiledExpression::fromZvalValue(false);
44
    }
45
}
46