Completed
Pull Request — master (#293)
by Дмитрий
03:16
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
            $this->passNode($stmt, $block);
35 4
        }
36 4
    }
37
38 4
    protected function passNode($stmt, Block $block)
39
    {
40 4
        switch (get_class($stmt)) {
41 4
            case \PhpParser\Node\Expr\Assign::class:
42
                $this->passAssign($stmt, $block);
43
                break;
44 4
            case \PhpParser\Node\Stmt\Return_::class:
45 3
                $this->passReturn($stmt, $block);
46 3
                break;
47 1
            case \PhpParser\Node\Stmt\For_::class:
48
                $block = $this->passFor($stmt, $block);
0 ignored issues
show
Unused Code introduced by
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
                break;
50 1
            case \PhpParser\Node\Stmt\If_::class:
51
                $block = $this->passIf($stmt, $block);
0 ignored issues
show
Unused Code introduced by
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
                break;
53 1
            case \PhpParser\Node\Stmt\While_::class:
54
                $block = $this->passWhile($stmt, $block);
0 ignored issues
show
Unused Code introduced by
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
                break;
56 1
            case \PhpParser\Node\Stmt\Do_::class:
57
                $block = $this->passDo($stmt, $block);
0 ignored issues
show
Unused Code introduced by
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
                break;
59 1
            case \PhpParser\Node\Stmt\Throw_::class:
60
                $this->passThrow($stmt, $block);
61
                break;
62 1
            case \PhpParser\Node\Expr\Exit_::class:
63
                $block->addChildren(new Node\ExitNode());
64
                break;
65 1
            case \PhpParser\Node\Stmt\Label::class:
66
                $block = $this->createNewBlockIfNeeded($block);
67
                $block->label = $stmt->name;
68
                break;
69 1
            case \PhpParser\Node\Stmt\Nop::class:
70
                // ignore commented code
71
                break;
72 1
            default:
73 1
                echo 'Unimplemented ' . get_class($stmt) . PHP_EOL;
74 1
                break;
75 4
        }
76 4
    }
77
78
    /**
79
     * If current block is not empty, lets create a new one
80
     *
81
     * @param Block $block
82
     * @return Block
83
     */
84
    protected function createNewBlockIfNeeded(Block $block)
85
    {
86
        if ($block->getChildrens()) {
87
            $block->setExit(
88
                $block = new Block($this->lastBlockId++)
89
            );
90
        }
91
92
        return $block;
93
    }
94
95
    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...
96
    {
97
        $trueBlock = new Block($this->lastBlockId++);
98
        $this->passNodes($if->stmts, $trueBlock);
99
100
        $jumpIf = new Node\JumpIfNode($trueBlock);
101
102
        $elseBlock = null;
103
104
        if ($if->else) {
105
            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...
106
                $elseBlock = new Block($this->lastBlockId++);
107
                $this->passNodes($if->else->stmts, $elseBlock);
108
109
                $jumpIf->setElse($elseBlock);
110
            }
111
        }
112
113
        $block->addChildren(
114
            $jumpIf
115
        );
116
117
        $exitBlock = new Block($this->lastBlockId++);
118
        $trueBlock->setExit($exitBlock);
119
120
        if ($elseBlock) {
121
            $elseBlock->setExit($exitBlock);
122
        }
123
124
        return $exitBlock;
125
    }
126
127
    protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block)
128
    {
129
        $this->passNodes($for->init, $block);
130
131
        $block->setExit(
132
            $loop = new Block($this->lastBlockId++)
133
        );
134
        $this->passNodes($for->stmts, $loop);
135
136
        $loop->setExit(
137
            $after = new Block($this->lastBlockId++)
138
        );
139
        return $after;
140
    }
141
142
    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...
143
    {
144
        $loop = new Block($this->lastBlockId++);
145
        $this->passNodes($do->stmts, $loop);
146
147
        $block->setExit($loop);
148
149
        $cond = new Block($this->lastBlockId++);
150
        $loop->setExit($cond);
151
152
        $jumpIf = new Node\JumpIfNode($loop);
153
        $cond->addChildren($jumpIf);
154
155
        $exitBlock = new Block($this->lastBlockId++);
156
        $jumpIf->setElse($exitBlock);
157
158
        return $exitBlock;
159
    }
160
161
    protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block)
162
    {
163
        $cond = new Block($this->lastBlockId++);
164
        $block->setExit(
165
            $cond
166
        );
167
168
        $loop = new Block($this->lastBlockId++);
169
170
        $jumpIf = new Node\JumpIfNode($loop);
171
        $cond->addChildren($jumpIf);
172
173
        $this->passNodes($while->stmts, $loop);
174
175
        $loop->addChildren(new Node\JumpNode($cond));
176
        //$loop->setExit($cond);
177
178
        $after = new Block($this->lastBlockId++);
179
        $jumpIf->setElse($after);
180
181
        return $after;
182
    }
183
184
    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...
185
    {
186
        $block->addChildren(new Node\ThrowNode());
187
    }
188
189
    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...
190
    {
191
        $block->addChildren(new Node\AssignNode());
192
    }
193
194 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...
195
    {
196 3
        $block->addChildren(new Node\ReturnNode());
197 3
    }
198
199
    /**
200
     * @return Block
201
     */
202
    public function getRoot()
203
    {
204
        return $this->root;
205
    }
206
}
207