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

ControlFlowGraph::passNodes()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 54
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 52.2305

Importance

Changes 0
Metric Value
cc 15
eloc 47
nc 15
nop 2
dl 0
loc 54
ccs 23
cts 51
cp 0.451
crap 52.2305
rs 6.7097
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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