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\Block; |
9
|
|
|
|
10
|
|
|
class DebugText |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Block[] |
14
|
|
|
*/ |
15
|
|
|
protected $blocks = []; |
16
|
|
|
|
17
|
|
|
protected function visitBlock(Block $parent, $level = 0) |
18
|
|
|
{ |
19
|
|
|
if ($level > 1) { |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->blocks[$parent->getId()] = $parent; |
24
|
|
|
|
25
|
|
|
$children = $parent->getChildren(); |
26
|
|
|
if ($children) { |
|
|
|
|
27
|
|
|
foreach ($children as $child) { |
28
|
|
|
$subBlocks = $child->getSubBlocks(); |
29
|
|
|
if ($subBlocks) { |
|
|
|
|
30
|
|
|
foreach ($subBlocks as $name => $block) { |
31
|
|
|
if ($block) { |
32
|
|
|
$this->blocks[$block->getId()] = $block; |
33
|
|
|
|
34
|
|
|
$blockExit = $block->getExit(); |
|
|
|
|
35
|
|
|
if ($blockExit) { |
36
|
|
|
$this->visitBlock($blockExit, $level + 1); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$exit = $parent->getExit(); |
45
|
|
|
if ($exit) { |
46
|
|
|
$this->visitBlock($exit, 0); |
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 . ($block->label ? ' Label: ' . $block->label : '') . PHP_EOL; |
58
|
|
|
|
59
|
|
|
$children = $block->getChildren(); |
60
|
|
|
if ($children) { |
61
|
|
|
foreach ($children as $child) { |
62
|
|
|
echo ' ' . get_class($child) . ($child->willExit() ? ' WILL EXIT!! ' : '') . PHP_EOL; |
63
|
|
|
|
64
|
|
|
$subVariables = $child->getSubVariables(); |
65
|
|
|
if ($subVariables) { |
66
|
|
|
foreach ($subVariables as $name => $subVariable) { |
67
|
|
|
if ($subVariable) { |
68
|
|
|
echo "\t" . $name . ' -> ' . get_class($subVariable) . PHP_EOL; |
69
|
|
|
} else { |
70
|
|
|
echo "\t" . $name . ' -> NOTHING'; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$subBlocks = $child->getSubBlocks(); |
76
|
|
|
if ($subBlocks) { |
77
|
|
|
foreach ($subBlocks as $name => $subBlock) { |
78
|
|
|
if ($subBlock) { |
79
|
|
|
echo "\t" . $name . ' -> Block#' . $subBlock->getId() . PHP_EOL; |
80
|
|
|
} else { |
81
|
|
|
echo "\t" . $name . ' -> NOTHING'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$exit = $block->getExit(); |
89
|
|
|
if ($exit) { |
90
|
|
|
echo ' -> Block#' . $exit->getId() . PHP_EOL; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
echo PHP_EOL . PHP_EOL; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.