Completed
Push — master ( 662a49...9d1eac )
by Дмитрий
9s
created

DebugText::printGraph()   C

Complexity

Conditions 13
Paths 5

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 27
nc 5
nop 1
dl 0
loc 46
ccs 0
cts 36
cp 0
crap 182
rs 5.1118
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $children 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...
27
            foreach ($children as $child) {
28
                $subBlocks = $child->getSubBlocks();
29
                if ($subBlocks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subBlocks of type PHPSA\ControlFlow\Block[] 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...
30
                    foreach ($subBlocks as $name => $block) {
31
                        if ($block) {
32
                            $this->blocks[$block->getId()] = $block;
33
34
                            $blockExit = $block->getExit();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $blockExit is correct as $block->getExit() (which targets PHPSA\ControlFlow\Block::getExit()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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)
0 ignored issues
show
Complexity introduced by
This operation has 821 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
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