Completed
Pull Request — master (#293)
by Дмитрий
03:54 queued 01:06
created

ControlFlowGraph::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\ControlFlow;
7
8
use PhpParser\Node\Stmt\Function_;
9
use PHPSA\ControlFlow\Node;
10
11
class ControlFlowGraph
12
{
13
    protected $lastBlockId = 1;
14
15
    /**
16
     * @var Block
17
     */
18
    protected $root;
19
20 5
    public function __construct($statement)
21
    {
22 5
        $this->root = new Block($this->lastBlockId++);
23
24 5
        if ($statement instanceof Function_) {
25 5
            if ($statement->stmts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $statement->stmts of type PhpParser\Node[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26 4
                $this->passNodes($statement->stmts, $this->root);
27 4
            }
28 5
        }
29 5
    }
30
31 4
    protected function passNodes(array $nodes, Block $block)
32
    {
33 4
        foreach ($nodes as $stmt) {
34 4
            switch (get_class($stmt)) {
35 4
                case \PhpParser\Node\Expr\Assign::class:
36
                    $this->passAssign($stmt, $block);
37
                    break;
38 4
                case \PhpParser\Node\Stmt\Return_::class:
39 3
                    $this->passReturn($stmt, $block);
40 3
                    break;
41 1
                case \PhpParser\Node\Stmt\For_::class:
42
                    $block = $this->passFor($stmt, $block);
43
                    break;
44 1
                case \PhpParser\Node\Stmt\If_::class:
45
                    $block = $this->passIf($stmt, $block);
46
                    break;
47 1
                case \PhpParser\Node\Stmt\While_::class:
48
                    $block = $this->passWhile($stmt, $block);
49
                    break;
50 1
                case \PhpParser\Node\Stmt\Throw_::class:
51
                    $this->passThrow($stmt, $block);
52
                    break;
53 1
                case \PhpParser\Node\Expr\Exit_::class:
54
                    $block->addChildren(new Node\ExitNode());
55
                    break;
56 1
                case \PhpParser\Node\Stmt\Label::class:
57
                    $block->setExit(
58
                        $block = new Block($this->lastBlockId++)
59
                    );
60
                    $block->label = $stmt->name;
61
                    break;
62 1
                case \PhpParser\Node\Stmt\Nop::class:
63
                    // ignore commented code
64
                    break;
65 1
                default:
66 1
                    echo 'Unimplemented ' . get_class($stmt) . PHP_EOL;
67 1
                    break;
68 4
            }
69 4
        }
70 4
    }
71
72
    protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $if. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
73
    {
74
        $trueBlock = new Block($this->lastBlockId++);
75
        $this->passNodes($if->stmts, $trueBlock);
76
77
        $jumpIf = new Node\JumpIfNode($trueBlock);
78
79
        $elseBlock = null;
80
81
        if ($if->else) {
82
            if ($if->else->stmts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $if->else->stmts of type PhpParser\Node[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83
                $elseBlock = new Block($this->lastBlockId++);
84
                $this->passNodes($if->else->stmts, $elseBlock);
85
86
                $jumpIf->setElse($elseBlock);
87
            }
88
        }
89
90
        $block->addChildren(
91
            $jumpIf
92
        );
93
94
        $exitBlock = new Block($this->lastBlockId++);
95
        $trueBlock->setExit($exitBlock);
96
97
        if ($elseBlock) {
98
            $elseBlock->setExit($exitBlock);
99
        }
100
101
        return $exitBlock;
102
    }
103
104
    protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block)
105
    {
106
        $this->passNodes($for->init, $block);
107
108
        $block->setExit(
109
            $loop = new Block($this->lastBlockId++)
110
        );
111
        $this->passNodes($for->stmts, $loop);
112
113
        $loop->setExit(
114
            $after = new Block($this->lastBlockId++)
115
        );
116
        return $after;
117
    }
118
119
    protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block)
120
    {
121
        $cond = new Block($this->lastBlockId++);
122
        $block->setExit(
123
            $cond
124
        );
125
126
        $loop = new Block($this->lastBlockId++);
127
128
        $jumpIf = new Node\JumpIfNode($loop);
129
        $cond->addChildren($jumpIf);
130
131
        $this->passNodes($while->stmts, $loop);
132
133
        $loop->addChildren(new Node\JumpNode($cond));
134
        //$loop->setExit($cond);
135
136
        $after = new Block($this->lastBlockId++);
137
        $jumpIf->setElse($after);
138
139
        return $after;
140
    }
141
142
    protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block)
0 ignored issues
show
Unused Code introduced by
The parameter $throw_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144
        $block->addChildren(new Node\ThrowNode());
145
    }
146
147
    protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block)
0 ignored issues
show
Unused Code introduced by
The parameter $assign is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        $block->addChildren(new Node\AssignNode());
150
    }
151
152 3
    protected function passReturn(\PhpParser\Node\Stmt\Return_ $return_, Block $block)
0 ignored issues
show
Unused Code introduced by
The parameter $return_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
    {
154 3
        $block->addChildren(new Node\ReturnNode());
155 3
    }
156
157
    /**
158
     * @return Block
159
     */
160
    public function getRoot()
161
    {
162
        return $this->root;
163
    }
164
}
165