|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA\ControlFlow\Printer; |
|
7
|
|
|
|
|
8
|
|
|
use PHPSA\ControlFlow\Graph\Block; |
|
9
|
|
|
use PHPSA\ControlFlow\Node\JumpIf; |
|
10
|
|
|
|
|
11
|
|
|
class DebugText |
|
12
|
|
|
{ |
|
13
|
|
|
protected $blocks = []; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function visitBlock(Block $parent) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->blocks[$parent->getId()] = $parent; |
|
23
|
|
|
|
|
24
|
|
|
$childrens = $parent->getChildrens(); |
|
25
|
|
|
if ($childrens) { |
|
|
|
|
|
|
26
|
|
|
foreach ($childrens as $children) { |
|
27
|
|
|
if ($children instanceof JumpIf) { |
|
28
|
|
|
$blocks = $children->getSubBlocks(); |
|
29
|
|
|
|
|
30
|
|
|
foreach ($blocks as $name => $block) { |
|
31
|
|
|
if ($block) { |
|
32
|
|
|
$this->blocks[$block->getId()] = $block; |
|
33
|
|
|
|
|
34
|
|
|
$blockExit = $block->getExit(); |
|
35
|
|
|
if ($blockExit) { |
|
36
|
|
|
$this->visitBlock($blockExit); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$exit = $parent->getExit(); |
|
45
|
|
|
if ($exit) { |
|
46
|
|
|
$this->visitBlock($exit); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function printGraph(Block $parent) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->visitBlock($parent); |
|
53
|
|
|
|
|
54
|
|
|
ksort($this->blocks); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($this->blocks as $id => $block) { |
|
57
|
|
|
echo 'Block #' . $id . PHP_EOL; |
|
58
|
|
|
|
|
59
|
|
|
$childrens = $block->getChildrens(); |
|
60
|
|
|
if ($childrens) { |
|
61
|
|
|
foreach ($childrens as $children) { |
|
62
|
|
|
echo ' ' . get_class($children) . ($children->willExit() ? ' WILL EXIT!! ' : '') . PHP_EOL; |
|
63
|
|
|
|
|
64
|
|
|
if ($children instanceof JumpIf) { |
|
65
|
|
|
$blocks = $children->getSubBlocks(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($blocks as $name => $subBlock) { |
|
68
|
|
|
if ($subBlock) { |
|
69
|
|
|
echo "\t" . $name . ' -> ' . $subBlock->getId() . PHP_EOL; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$exit = $block->getExit(); |
|
77
|
|
|
if ($exit) { |
|
78
|
|
|
echo ' -> ' . $exit->getId() . PHP_EOL; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
echo PHP_EOL . PHP_EOL; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// $childrenSubBlocks = new \SplQueue(); |
|
85
|
|
|
// $childrenSubBlocks->setIteratorMode(\SplQueue::IT_MODE_DELETE); |
|
86
|
|
|
// |
|
87
|
|
|
// |
|
88
|
|
|
// echo 'Block #' . $parent->getId() . PHP_EOL; |
|
89
|
|
|
// |
|
90
|
|
|
// $childrens = $parent->getChildrens(); |
|
91
|
|
|
// if ($childrens) { |
|
92
|
|
|
// foreach ($childrens as $children) { |
|
93
|
|
|
// echo ' ' . get_class($children) . PHP_EOL; |
|
94
|
|
|
// |
|
95
|
|
|
// if ($children instanceof JumpIf) { |
|
96
|
|
|
// $blocks = $children->getSubBlocks(); |
|
97
|
|
|
// |
|
98
|
|
|
// foreach ($blocks as $name => $block) { |
|
99
|
|
|
// if ($block) { |
|
100
|
|
|
// echo "\t" . $name . ' -> ' . $block->getId() . PHP_EOL; |
|
101
|
|
|
// $childrenSubBlocks->push($block); |
|
102
|
|
|
// } |
|
103
|
|
|
// } |
|
104
|
|
|
// } |
|
105
|
|
|
// } |
|
106
|
|
|
// } |
|
107
|
|
|
// |
|
108
|
|
|
// $exit = $parent->getExit(); |
|
109
|
|
|
// if ($exit) { |
|
110
|
|
|
// echo '-> ' . $exit->getId() . PHP_EOL; |
|
111
|
|
|
// } |
|
112
|
|
|
|
|
113
|
|
|
// echo PHP_EOL . PHP_EOL; |
|
114
|
|
|
// |
|
115
|
|
|
// if ($exit) { |
|
116
|
|
|
// $this->printBlock($exit); |
|
117
|
|
|
// } |
|
118
|
|
|
// |
|
119
|
|
|
// foreach ($childrenSubBlocks as $block) { |
|
120
|
|
|
// $this->printBlock($block); |
|
121
|
|
|
// } |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
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.