InstanceOfOp::compile()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 2
dl 0
loc 27
ccs 0
cts 10
cp 0
crap 56
rs 8.5546
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\Compiler\Expression\AbstractExpressionCompiler;
12
use PHPSA\Context;
13
use PHPSA\Compiler\Expression;
14
15
class InstanceOfOp extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\Instanceof_';
18
19
    /**
20
     * $a instanceof Node
21
     * $expr->expr instance of $expr->class
22
     *
23
     * @param \PhpParser\Node\Expr\Instanceof_ $expr
24
     * @param Context $context
25
     * @return CompiledExpression
26
     */
27
    protected function compile($expr, Context $context)
28
    {
29
        $leftCompiledExpression = $context->getExpressionCompiler()->compile($expr->expr);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $leftCompiledExpression exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
30
        $rightCompiledExpression = $context->getExpressionCompiler()->compile($expr->class);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $rightCompiledExpression exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
31
32
        if ($leftCompiledExpression->isObject() && $rightCompiledExpression->isObject()) {
33
            $leftVariable = $leftCompiledExpression->getVariable();
34
            $rightVariable = $rightCompiledExpression->getVariable();
35
36
            /**
37
             * $a = new A();
38
             * $b = $a;
39
             *
40
             * $a instanceof $b
41
             */
42
            if ($leftVariable && $rightVariable) {
43
                if ($leftVariable->isReferenced() && $leftVariable->getReferencedTo() instanceof $rightVariable) {
44
                    return new CompiledExpression(CompiledExpression::BOOLEAN, true);
45
                }
46
            }
47
        }
48
49
        /**
50
         * Anyway this operator will return BOOLEAN
51
         */
52
        return new CompiledExpression(CompiledExpression::BOOLEAN);
53
    }
54
}
55