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

ControlFlowGraph::passFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 2
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\TryCatch::class:
70
                $block = $this->passTryCatch($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...
71
                break;
72 1
            case \PhpParser\Node\Stmt\Nop::class:
73
                // ignore commented code
74
                break;
75 1
            default:
76 1
                echo 'Unimplemented ' . get_class($stmt) . PHP_EOL;
77 1
                break;
78 4
        }
79 4
    }
80
81
    /**
82
     * If current block is not empty, lets create a new one
83
     *
84
     * @param Block $block
85
     * @return Block
86
     */
87
    protected function createNewBlockIfNeeded(Block $block)
88
    {
89
        if ($block->getChildrens()) {
90
            $block->setExit(
91
                $block = new Block($this->lastBlockId++)
92
            );
93
        }
94
95
        return $block;
96
    }
97
98
    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...
99
    {
100
        $trueBlock = new Block($this->lastBlockId++);
101
        $this->passNodes($if->stmts, $trueBlock);
102
103
        $jumpIf = new Node\JumpIfNode($trueBlock);
104
105
        $elseBlock = null;
106
107
        if ($if->else) {
108
            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...
109
                $elseBlock = new Block($this->lastBlockId++);
110
                $this->passNodes($if->else->stmts, $elseBlock);
111
112
                $jumpIf->setElse($elseBlock);
113
            }
114
        }
115
116
        $block->addChildren(
117
            $jumpIf
118
        );
119
120
        $exitBlock = new Block($this->lastBlockId++);
121
        $trueBlock->setExit($exitBlock);
122
123
        if ($elseBlock) {
124
            $elseBlock->setExit($exitBlock);
125
        }
126
127
        return $exitBlock;
128
    }
129
130
    protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block)
131
    {
132
        $this->passNodes($for->init, $block);
133
134
        $block->setExit(
135
            $loop = new Block($this->lastBlockId++)
136
        );
137
        $this->passNodes($for->stmts, $loop);
138
139
        $loop->setExit(
140
            $after = new Block($this->lastBlockId++)
141
        );
142
        return $after;
143
    }
144
145
    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...
146
    {
147
        $loop = new Block($this->lastBlockId++);
148
        $this->passNodes($do->stmts, $loop);
149
150
        $block->setExit($loop);
151
152
        $cond = new Block($this->lastBlockId++);
153
        $loop->setExit($cond);
154
155
        $jumpIf = new Node\JumpIfNode($loop);
156
        $cond->addChildren($jumpIf);
157
158
        $exitBlock = new Block($this->lastBlockId++);
159
        $jumpIf->setElse($exitBlock);
160
161
        return $exitBlock;
162
    }
163
164
    protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block)
165
    {
166
        $cond = new Block($this->lastBlockId++);
167
        $block->setExit(
168
            $cond
169
        );
170
171
        $loop = new Block($this->lastBlockId++);
172
173
        $jumpIf = new Node\JumpIfNode($loop);
174
        $cond->addChildren($jumpIf);
175
176
        $this->passNodes($while->stmts, $loop);
177
178
        $loop->addChildren(new Node\JumpNode($cond));
179
        //$loop->setExit($cond);
180
181
        $after = new Block($this->lastBlockId++);
182
        $jumpIf->setElse($after);
183
184
        return $after;
185
    }
186
187
    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...
188
    {
189
        $block->addChildren(new Node\ThrowNode());
190
    }
191
192
    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...
193
    {
194
        $block->addChildren(new Node\AssignNode());
195
    }
196
197 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...
198
    {
199 3
        $block->addChildren(new Node\ReturnNode());
200 3
    }
201
202
    protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block)
203
    {
204
        $try = new Block($this->lastBlockId++);
205
        $this->passNodes($stmt->stmts, $try);
206
207
        $block->setExit($try);
208
209
        if ($stmt->finally) {
210
            $finally = new Block($this->lastBlockId++);
211
            $this->passNodes($stmt->finally->stmts, $finally);
212
213
            $try->setExit($finally);
214
            return $finally;
215
        }
216
217
        return $try;
218
    }
219
220
    /**
221
     * @return Block
222
     */
223
    public function getRoot()
224
    {
225
        return $this->root;
226
    }
227
}
228