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

DebugText   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C visitBlock() 0 29 8
C printGraph() 0 73 9
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $childrens of type array 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
            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