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

DebugText::visitBlock()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
nc 4
nop 1
dl 0
loc 29
rs 5.3846
c 1
b 0
f 0
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
use PHPSA\ControlFlow\Node\JumpIf;
10
11
class DebugText
12
{
13
    protected $blocks = [];
14
15
    protected function visitBlock(Block $parent)
16
    {
17
        $this->blocks[$parent->getId()] = $parent;
18
19
        $childrens = $parent->getChildrens();
20
        if ($childrens) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $childrens of type PHPSA\ControlFlow\Node\AbstractNode[] 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...
21
            foreach ($childrens as $children) {
22
                if ($children instanceof JumpIf) {
23
                    $blocks = $children->getSubBlocks();
24
25
                    foreach ($blocks as $name => $block) {
26
                        if ($block) {
27
                            $this->blocks[$block->getId()] = $block;
28
29
                            $blockExit = $block->getExit();
30
                            if ($blockExit) {
31
                                $this->visitBlock($blockExit);
32
                            }
33
                        }
34
                    }
35
                }
36
            }
37
        }
38
39
        $exit = $parent->getExit();
40
        if ($exit) {
41
            $this->visitBlock($exit);
42
        }
43
    }
44
45
    public function printGraph(Block $parent)
46
    {
47
        $this->visitBlock($parent);
48
49
        ksort($this->blocks);
50
51
        foreach ($this->blocks as $id => $block) {
52
            echo 'Block #' . $id . PHP_EOL;
53
54
            $childrens = $block->getChildrens();
55
            if ($childrens) {
56
                foreach ($childrens as $children) {
57
                    echo '  ' . get_class($children) . ($children->willExit() ? ' WILL EXIT!! ' : '') . PHP_EOL;
58
59
                    if ($children instanceof JumpIf) {
60
                        $blocks = $children->getSubBlocks();
61
62
                        foreach ($blocks as $name => $subBlock) {
63
                            if ($subBlock) {
64
                                echo "\t" . $name . ' -> ' . $subBlock->getId() . PHP_EOL;
65
                            }
66
                        }
67
                    }
68
                }
69
            }
70
71
            $exit = $block->getExit();
72
            if ($exit) {
73
                echo '  -> ' . $exit->getId() . PHP_EOL;
74
            }
75
76
            echo PHP_EOL . PHP_EOL;
77
        }
78
    }
79
}
80