Completed
Push — master ( 3902a1...10f82c )
by Rick
04:55
created

Detector::bidirectionalCheck()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 5
nop 3
crap 4
1
<?php
2
3
namespace PhpAssumptions;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr;
7
use PhpParser\Node\Stmt;
8
9
class Detector
10
{
11
    /**
12
     * @param Node $node
13
     * @return bool
14
     */
15 12
    public function scan(Node $node)
16
    {
17 12
        if (($node instanceof Stmt\Expression)) {
18 10
            $node = $node->expr;
19
        }
20
21 12
        if (($node instanceof Expr\BinaryOp\BooleanOr || $node instanceof Expr\BinaryOp\BooleanAnd)
22 12
            && $this->bidirectionalCheck($node, Expr\Variable::class, Expr\BinaryOp::class)
23
        ) {
24 3
            return true;
25
        }
26
27 12
        if ($node instanceof Expr\BinaryOp\Equal || $node instanceof Expr\BinaryOp\NotEqual
28 12
            || $node instanceof Expr\BinaryOp\NotIdentical
29
        ) {
30 8
            return true;
31
        }
32
33 10
        if ($this->isVariableExpression($node)) {
34 7
            return true;
35
        }
36
37 6
        return false;
38
    }
39
40
    /**
41
     * @param Node $node
42
     * @return bool
43
     */
44 6
    public function isBoolExpression(Node $node)
45
    {
46 6
        if ($node instanceof Expr\Ternary || $node instanceof Stmt\If_
47 6
            || $node instanceof Stmt\ElseIf_ || $node instanceof Stmt\While_
48 6
            || $node instanceof Expr\BinaryOp\BooleanAnd || $node instanceof Expr\BinaryOp\BooleanOr
49 6
            || $node instanceof Stmt\For_
50
        ) {
51 6
            return true;
52
        }
53
54 6
        return false;
55
    }
56
57
    /**
58
     * @param Node $node
59
     * @return bool
60
     */
61 10
    private function isVariableExpression(Node $node)
62
    {
63 10
        if ($node instanceof Expr\BooleanNot && $node->expr instanceof Expr\Variable) {
64 4
            return true;
65
        }
66
67 9
        if ($node instanceof Expr\Ternary || $node instanceof Stmt\If_
68 9
            || $node instanceof Stmt\ElseIf_ || $node instanceof Stmt\While_
69 9
            || $node instanceof Stmt\For_
70
        ) {
71 9
            if ($node->cond instanceof Expr\Variable) {
72 5
                return true;
73
            }
74
75 7
            if (is_array($node->cond)) {
76 4
                foreach ($node->cond as $condition) {
77 4
                    if ($condition instanceof Expr\Variable) {
78 4
                        return true;
79
                    }
80
                }
81
            }
82
        }
83
84 6
        return false;
85
    }
86
87
    /**
88
     * @param Expr   $condition
89
     * @param string $left
90
     * @param string $right
91
     * @return bool
92
     */
93 3
    private function bidirectionalCheck(Expr $condition, $left, $right)
94
    {
95 3
        return ($this->isInstanceOf($condition->left, $left) && $this->isInstanceOf($condition->right, $right))
0 ignored issues
show
Bug introduced by
The property left does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property right does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
96 3
            || ($this->isInstanceOf($condition->right, $left) && $this->isInstanceOf($condition->left, $right));
97
    }
98
99
    /**
100
     * @param object $object
101
     * @param string $class
102
     * @return bool
103
     */
104 3
    private function isInstanceOf($object, $class)
105
    {
106 3
        return get_class($object) === $class || is_subclass_of($object, $class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
107
    }
108
}
109