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

DebugText::visitBlock()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 5
nop 2
dl 0
loc 33
ccs 0
cts 26
cp 0
crap 90
rs 4.909
c 0
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
    /**
14
     * @var Block[]
15
     */
16
    protected $blocks = [];
17
18
    protected function visitBlock(Block $parent, $level = 0)
19
    {
20
        if ($level > 1) {
21
            return false;
22
        }
23
24
        $this->blocks[$parent->getId()] = $parent;
25
26
        $childrens = $parent->getChildrens();
27
        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...
28
            foreach ($childrens as $children) {
29
                if ($children instanceof JumpIf) {
0 ignored issues
show
Bug introduced by
The class PHPSA\ControlFlow\Node\JumpIf does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
30
                    $blocks = $children->getSubBlocks();
31
32
                    foreach ($blocks as $name => $block) {
33
                        if ($block) {
34
                            $this->blocks[$block->getId()] = $block;
35
36
                            $blockExit = $block->getExit();
37
                            if ($blockExit) {
38
                                $this->visitBlock($blockExit, $level + 1);
39
                            }
40
                        }
41
                    }
42
                }
43
            }
44
        }
45
46
        $exit = $parent->getExit();
47
        if ($exit) {
48
            $this->visitBlock($exit, 0);
49
        }
50
    }
51
52
    public function printGraph(Block $parent)
0 ignored issues
show
Complexity introduced by
This operation has 221 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...
53
    {
54
        $this->visitBlock($parent);
55
56
        ksort($this->blocks);
57
58
        foreach ($this->blocks as $id => $block) {
59
            echo 'Block #' . $id . ($block->label ? ' Label: ' . $block->label : '') . PHP_EOL;
60
61
            $childrens = $block->getChildrens();
62
            if ($childrens) {
63
                foreach ($childrens as $children) {
64
                    echo '  ' . get_class($children) . ($children->willExit() ? ' WILL EXIT!! ' : '') . PHP_EOL;
65
66
                    if ($children instanceof JumpIf) {
0 ignored issues
show
Bug introduced by
The class PHPSA\ControlFlow\Node\JumpIf does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
                        $blocks = $children->getSubBlocks();
68
69
                        foreach ($blocks as $name => $subBlock) {
70
                            if ($subBlock) {
71
                                echo "\t" . $name . ' -> ' . $subBlock->getId() . PHP_EOL;
72
                            }
73
                        }
74
                    }
75
                }
76
            }
77
78
            $exit = $block->getExit();
79
            if ($exit) {
80
                echo '  -> ' . $exit->getId() . PHP_EOL;
81
            }
82
83
            echo PHP_EOL . PHP_EOL;
84
        }
85
    }
86
}
87