InstanceOfOp   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 27 7
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