Completed
Push — master ( a124c8...dbcf9b )
by Дмитрий
03:11
created

InstanceOfOp   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 0
cbo 5
dl 0
loc 43
ccs 0
cts 14
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C compile() 0 30 7
1
<?php
2
/**
3
 * PHP Static Analysis project 2015
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression\Operators;
9
10
use PhpParser\Node;
11
use PHPSA\CompiledExpression;
12
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
13
use PHPSA\Context;
14
use PHPSA\Compiler\Expression;
15
16
class InstanceOfOp extends AbstractExpressionCompiler
17
{
18
    protected $name = 'PhpParser\Node\Expr\Instanceof_';
19
20
    /**
21
     * $a instanceof Node
22
     * $expr->expr instance of $expr->class
23
     *
24
     * @param \PhpParser\Node\Expr\Instanceof_ $expr
25
     * @param Context $context
26
     * @return CompiledExpression
27
     */
28
    protected function compile($expr, Context $context)
29
    {
30
        $expression = new Expression($context);
31
        $leftCompiledExpression = $expression->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...
32
33
        $expression = new Expression($context);
34
        $rightCompiledExpression = $expression->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...
35
36
        if ($leftCompiledExpression->isObject() && $rightCompiledExpression->isObject()) {
37
            $leftVariable = $leftCompiledExpression->getVariable();
38
            $rightVariable = $rightCompiledExpression->getVariable();
39
40
            /**
41
             * $a = new A();
42
             * $b = $a;
43
             *
44
             * $a instanceof $b
45
             */
46
            if ($leftVariable && $rightVariable) {
47
                if ($leftVariable->isReferenced() && $leftVariable->getReferencedTo() instanceof $rightVariable) {
48
                    return new CompiledExpression(CompiledExpression::BOOLEAN, true);
49
                }
50
            }
51
         }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 9
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 9
Loading history...
52
53
        /**
54
         * Anyway this operator will return BOOLEAN
55
         */
56
        return new CompiledExpression(CompiledExpression::BOOLEAN);
57
    }
58
}
59