Completed
Pull Request — master (#38)
by Pádraic
02:50
created

DefaultExtension::extend()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace PHPMND\Extension;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr\BinaryOp;
7
use PhpParser\Node\Expr\BinaryOp\Coalesce;
8
use PhpParser\Node\Expr\BinaryOp\Equal;
9
use PhpParser\Node\Expr\BinaryOp\Greater;
10
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
11
use PhpParser\Node\Expr\BinaryOp\Identical;
12
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
13
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
14
use PhpParser\Node\Expr\BinaryOp\LogicalXor;
15
use PhpParser\Node\Expr\BinaryOp\NotEqual;
16
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
17
use PhpParser\Node\Expr\BinaryOp\Smaller;
18
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
19
use PhpParser\Node\Expr\BinaryOp\Spaceship;
20
use PhpParser\Node\Expr\ConstFetch;
21
use PhpParser\Node\Stmt\Case_;
22
use PhpParser\Node\Stmt\Return_;
23
24
/**
25
 * Class DefaultExtension
26
 */
27
class DefaultExtension implements Extension
28
{
29
    /**
30
     * @inheritdoc
31
     */
32
    public function extend(Node $node)
33
    {
34
        return
35
            ($this->isCondition($node->getAttribute('parent')) && false === $this->comparesToConst($node->getAttribute('parent')))
36
            ||
37
            $this->isReturn($node->getAttribute('parent'))
38
            ||
39
            $this->isSwitchCase($node->getAttribute('parent'));
40
    }
41
42
    /**
43
     * @param Node $node
44
     *
45
     * @return bool
46
     */
47
    private function isCondition($node)
48
    {
49
        return
50
            $node instanceof BinaryOp
51
            &&
52
            (
53
                $node instanceof Equal
54
                ||
55
                $node instanceof NotEqual
56
                ||
57
                $node instanceof Greater
58
                ||
59
                $node instanceof GreaterOrEqual
60
                ||
61
                $node instanceof Smaller
62
                ||
63
                $node instanceof SmallerOrEqual
64
                ||
65
                $node instanceof Identical
66
                ||
67
                $node instanceof NotIdentical
68
                ||
69
                $node instanceof LogicalAnd
70
                ||
71
                $node instanceof LogicalOr
72
                ||
73
                $node instanceof LogicalXor
74
                ||
75
                $node instanceof Coalesce
76
                ||
77
                $node instanceof Spaceship
78
            );
79
    }
80
81
    /**
82
     * @param BinaryOp $node
83
     *
84
     * @return bool
85
     */
86
    private function comparesToConst($node)
87
    {
88
        return
89
            $node instanceof BinaryOp
90
            &&
91
            (
92
                $node->left instanceof ConstFetch
93
                ||
94
                $node->right instanceof ConstFetch
95
            );
96
    }
97
98
    /**
99
     * @param Node $node
100
     *
101
     * @return bool
102
     */
103
    private function isReturn($node)
104
    {
105
        return $node instanceof Return_;
106
    }
107
108
    /**
109
     * @param Node $node
110
     *
111
     * @return bool
112
     */
113
    private function isSwitchCase($node)
114
    {
115
        return $node instanceof Case_;
116
    }
117
}
118