Completed
Pull Request — master (#293)
by Дмитрий
03:30
created

ControlFlowGraph::createNewBlockIfNeeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
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
    /**
14
     * @var int
15
     */
16
    protected $lastBlockId = 1;
17
18
    /**
19
     * @var Block
20
     */
21
    protected $root;
22
23
    /**
24
     * @param $statement
25
     */
26 5
    public function __construct($statement)
27
    {
28 5
        $this->root = new Block($this->lastBlockId++);
29
30 5
        if ($statement instanceof Function_) {
31 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...
32 4
                $this->passNodes($statement->stmts, $this->root);
33 4
            }
34 5
        }
35 5
    }
36
37
    /**
38
     * @param array $nodes
39
     * @param Block $block
40
     */
41 4
    protected function passNodes(array $nodes, Block $block)
42
    {
43 4
        foreach ($nodes as $stmt) {
44 4
            switch (get_class($stmt)) {
45 4
                case \PhpParser\Node\Expr\Assign::class:
46
                    $this->passAssign($stmt, $block);
47
                    break;
48 4
                case \PhpParser\Node\Stmt\Return_::class:
49 3
                    $this->passReturn($stmt, $block);
50 3
                    break;
51 1
                case \PhpParser\Node\Stmt\For_::class:
52
                    $block = $this->passFor($stmt, $block);
53
                    break;
54 1
                case \PhpParser\Node\Stmt\If_::class:
55
                    $block = $this->passIf($stmt, $block);
56
                    break;
57 1
                case \PhpParser\Node\Stmt\While_::class:
58
                    $block = $this->passWhile($stmt, $block);
59
                    break;
60 1
                case \PhpParser\Node\Stmt\Do_::class:
61
                    $block = $this->passDo($stmt, $block);
62
                    break;
63 1
                case \PhpParser\Node\Stmt\Throw_::class:
64
                    $this->passThrow($stmt, $block);
65
                    break;
66 1
                case \PhpParser\Node\Expr\Exit_::class:
67
                    $block->addChildren(new Node\ExitNode());
68
                    break;
69 1
                case \PhpParser\Node\Stmt\Label::class:
70
                    $block = $this->createNewBlockIfNeeded($block);
71
                    $block->label = $stmt->name;
72
                    break;
73 1
                case \PhpParser\Node\Stmt\TryCatch::class:
74
                    $block = $this->passTryCatch($stmt, $block);
75
                    break;
76 1
                case \PhpParser\Node\Stmt\Nop::class:
77
                    // ignore commented code
78
                    break;
79 1
                default:
80 1
                    echo 'Unimplemented ' . get_class($stmt) . PHP_EOL;
81 1
                    break;
82 4
            }
83 4
        }
84 4
    }
85
86
    /**
87
     * If current block is not empty, lets create a new one
88
     *
89
     * @param Block $block
90
     * @return Block
91
     */
92
    protected function createNewBlockIfNeeded(Block $block)
93
    {
94
        if ($block->getChildrens()) {
95
            $block->setExit(
96
                $block = new Block($this->lastBlockId++)
97
            );
98
        }
99
100
        return $block;
101
    }
102
103
    /**
104
     * @param \PhpParser\Node\Expr $expr
105
     * @return Node\AbstractNode
106
     */
107
    protected function passExpr(\PhpParser\Node\Expr $expr)
108
    {
109
        switch (get_class($expr)) {
110
            case \PhpParser\Node\Expr\BinaryOp\Greater::class:
111
                return new Node\Expr\Greater();
112
            case \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual::class:
113
                return new Node\Expr\GreaterOrEqual();
114
            case \PhpParser\Node\Expr\Instanceof_::class:
115
                return new Node\Expr\InstanceOfExpr();
116
            default:
117
                echo 'Unimplemented ' . get_class($expr) . PHP_EOL;
118
        }
119
120
        return new Node\UnknownNode();
121
    }
122
123
    /**
124
     * @param \PhpParser\Node\Stmt\If_ $if
125
     * @param Block $block
126
     * @return Block
127
     */
128
    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...
129
    {
130
        $trueBlock = new Block($this->lastBlockId++);
131
        $this->passNodes($if->stmts, $trueBlock);
132
133
        $jumpIf = new Node\JumpIfNode($this->passExpr($if->cond), $trueBlock);
134
135
        $elseBlock = null;
136
137
        if ($if->else) {
138
            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...
139
                $elseBlock = new Block($this->lastBlockId++);
140
                $this->passNodes($if->else->stmts, $elseBlock);
141
142
                $jumpIf->setElse($elseBlock);
143
            }
144
        }
145
146
        $block->addChildren(
147
            $jumpIf
148
        );
149
150
        $exitBlock = new Block($this->lastBlockId++);
151
        $trueBlock->setExit($exitBlock);
152
153
        if ($elseBlock) {
154
            $elseBlock->setExit($exitBlock);
155
        }
156
157
        return $exitBlock;
158
    }
159
160
    /**
161
     * @param \PhpParser\Node\Stmt\For_ $for
162
     * @param Block $block
163
     * @return Block
164
     */
165
    protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block)
166
    {
167
        $this->passNodes($for->init, $block);
168
169
        $block->setExit(
170
            $loop = new Block($this->lastBlockId++)
171
        );
172
        $this->passNodes($for->stmts, $loop);
173
174
        $loop->setExit(
175
            $after = new Block($this->lastBlockId++)
176
        );
177
        return $after;
178
    }
179
180
    /**
181
     * @param \PhpParser\Node\Stmt\Do_ $do
182
     * @param Block $block
183
     * @return Block
184
     */
185
    protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $do. 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...
186
    {
187
        $loop = new Block($this->lastBlockId++);
188
        $this->passNodes($do->stmts, $loop);
189
190
        $block->setExit($loop);
191
192
        $cond = new Block($this->lastBlockId++);
193
        $loop->setExit($cond);
194
195
        $jumpIf = new Node\JumpIfNode($this->passExpr($do->cond), $loop);
196
        $cond->addChildren($jumpIf);
197
198
        $exitBlock = new Block($this->lastBlockId++);
199
        $jumpIf->setElse($exitBlock);
200
201
        return $exitBlock;
202
    }
203
204
    /**
205
     * @param \PhpParser\Node\Stmt\While_ $while
206
     * @param Block $block
207
     * @return Block
208
     */
209
    protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block)
210
    {
211
        $cond = new Block($this->lastBlockId++);
212
        $block->setExit(
213
            $cond
214
        );
215
216
        $loop = new Block($this->lastBlockId++);
217
218
        $jumpIf = new Node\JumpIfNode($this->passExpr($while->cond), $loop);
219
        $cond->addChildren($jumpIf);
220
221
        $this->passNodes($while->stmts, $loop);
222
223
        $loop->addChildren(new Node\JumpNode($cond));
224
        //$loop->setExit($cond);
225
226
        $after = new Block($this->lastBlockId++);
227
        $jumpIf->setElse($after);
228
229
        return $after;
230
    }
231
232
    /**
233
     * @param \PhpParser\Node\Stmt\Throw_ $throw_
234
     * @param Block $block
235
     */
236
    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...
237
    {
238
        $block->addChildren(new Node\ThrowNode());
239
    }
240
241
    /**
242
     * @param \PhpParser\Node\Expr\Assign $assign
243
     * @param Block $block
244
     */
245
    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...
246
    {
247
        $block->addChildren(new Node\AssignNode());
248
    }
249
250
    /**
251
     * @param \PhpParser\Node\Stmt\Return_ $return_
252
     * @param Block $block
253
     */
254 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...
255
    {
256 3
        $block->addChildren(new Node\ReturnNode());
257 3
    }
258
259
    /**
260
     * @param \PhpParser\Node\Stmt\TryCatch $stmt
261
     * @param Block $block
262
     * @return Block
263
     */
264
    protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block)
265
    {
266
        $try = new Block($this->lastBlockId++);
267
        $this->passNodes($stmt->stmts, $try);
268
269
        $block->setExit($try);
270
271
        if ($stmt->finally) {
272
            $finally = new Block($this->lastBlockId++);
273
            $this->passNodes($stmt->finally->stmts, $finally);
274
275
            $try->setExit($finally);
276
            return $finally;
277
        }
278
279
        return $try;
280
    }
281
282
    /**
283
     * @return Block
284
     */
285
    public function getRoot()
286
    {
287
        return $this->root;
288
    }
289
}
290