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

DebugText   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
D visitBlock() 0 33 9
D printGraph() 0 34 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\Block;
9
use PHPSA\ControlFlow\Node\JumpIf;
10
11
class DebugText
12
{
13
    protected $blocks = [];
14
15
    protected function visitBlock(Block $parent, $level = 0)
16
    {
17
        if ($level > 1) {
18
            return false;
19
        }
20
21
        $this->blocks[$parent->getId()] = $parent;
22
23
        $childrens = $parent->getChildrens();
24
        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...
25
            foreach ($childrens as $children) {
26
                if ($children instanceof JumpIf) {
27
                    $blocks = $children->getSubBlocks();
28
29
                    foreach ($blocks as $name => $block) {
30
                        if ($block) {
31
                            $this->blocks[$block->getId()] = $block;
32
33
                            $blockExit = $block->getExit();
34
                            if ($blockExit) {
35
                                $this->visitBlock($blockExit, $level + 1);
36
                            }
37
                        }
38
                    }
39
                }
40
            }
41
        }
42
43
        $exit = $parent->getExit();
44
        if ($exit) {
45
            $this->visitBlock($exit, 0);
46
        }
47
    }
48
49
    public function printGraph(Block $parent)
50
    {
51
        $this->visitBlock($parent);
52
53
        ksort($this->blocks);
54
55
        foreach ($this->blocks as $id => $block) {
56
            echo 'Block #' . $id . PHP_EOL;
57
58
            $childrens = $block->getChildrens();
59
            if ($childrens) {
60
                foreach ($childrens as $children) {
61
                    echo '  ' . get_class($children) . ($children->willExit() ? ' WILL EXIT!! ' : '') . PHP_EOL;
62
63
                    if ($children instanceof JumpIf) {
64
                        $blocks = $children->getSubBlocks();
65
66
                        foreach ($blocks as $name => $subBlock) {
67
                            if ($subBlock) {
68
                                echo "\t" . $name . ' -> ' . $subBlock->getId() . PHP_EOL;
69
                            }
70
                        }
71
                    }
72
                }
73
            }
74
75
            $exit = $block->getExit();
76
            if ($exit) {
77
                echo '  -> ' . $exit->getId() . PHP_EOL;
78
            }
79
80
            echo PHP_EOL . PHP_EOL;
81
        }
82
    }
83
}
84