Completed
Pull Request — master (#304)
by
unknown
06:29 queued 02:43
created

uiBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 1
eloc 9
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: php_r
5
 * Date: 16.2.2018
6
 * Time: 21.08
7
 */
8
9
namespace eXpansion\Framework\Gui\Builders;
10
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
13
use eXpansion\Framework\Gui\Components\Button;
14
use eXpansion\Framework\Gui\Components\Label as uiLAbel;
15
use eXpansion\Framework\Gui\Ui\Factory;
16
use FML\Controls\Frame;
17
use FML\Controls\Label;
18
use FML\Controls\Quad;
19
20
class uiBuilder
21
{
22
    /**
23
     * @var Factory
24
     */
25
    private $uiFactory;
26
    private $pluginClass;
27
    /**
28
     * @var ActionFactory
29
     */
30
    private $actionFactory;
31
    /**
32
     * @var ManialinkInterface
33
     */
34
    private $manialink;
35
36
    public function __construct(
37
        Factory $uiFactory,
38
        ActionFactory $actionFactory,
39
        ManialinkInterface $manialink,
40
        $pluginClass
41
    ) {
42
        $this->uiFactory = $uiFactory;
43
        $this->pluginClass = $pluginClass;
44
        $this->actionFactory = $actionFactory;
45
        $this->manialink = $manialink;
46
    }
47
48
    /**
49
     * @param string $xmlString
50
     * @return Frame
51
     */
52
    public function build($xmlString)
53
    {
54
        $xml = new \DOMDocument(1, "utf-8");
55
        $xml->loadXML($xmlString);
56
57
        return $this->parse($xml->documentElement);
58
    }
59
60
    /**
61
     * @param \DOMNode $node
62
     * @param null     $result
63
     * @return Frame
64
     */
65
    private function parse($node, $result = null)
66
    {
67
        if ($result === null) {
68
            $result = new Frame();
69
        }
70
71
        if ($node->nodeType == XML_TEXT_NODE) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
72
            /** @var DOMText $node */
73
            // $result->setText($node->nodeValue);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
        } else {
75
76
            if ($node->hasChildNodes()) {
77
                $children = $node->childNodes;
78
                for ($i = 0; $i < $children->length; $i++) {
79
                    $child = $children->item($i);
80
                    if ($child->nodeName != '#text') {
81
                        $aux = $this->castTagToMethod($child->nodeName, $child);
82
                        $result->addChild($this->parse($child, $aux));
0 ignored issues
show
Bug introduced by
It seems like $result is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
83
                    }
84
                }
85
86
87
            }
88
            if ($result instanceof Label) {
89
                $result->setText($node->nodeValue);
90
            }
91
            if ($result instanceof uiLabel) {
92
                $result->setText($node->nodeValue);
93
            }
94
            if ($result instanceof Button) {
95
                $result->setText($node->nodeValue);
96
            }
97
98
            if ($node->hasAttributes()) {
99
                $attributes = $node->attributes;
100
                if (!is_null($attributes)) {
101
                    foreach ($attributes as $index => $attr) {
102
                        //    $result->setAttr($attr->name, $attr->value);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
                        $this->parseAttributes($result, $attr, $node);
104
                    }
105
                }
106
            }
107
108
            return $result;
109
        }
110
    }
111
112
    private function castTagToMethod($tag, \DOMNode $node)
113
    {
114
        if (substr($tag, 0, 2) == "ui") {
115
            return $this->uiFactory->{"create".ucfirst(substr($tag, 2))}();
116
        } else {
117
            switch ($tag) {
118
                case "label":
119
                    return Label::create();
120
                case "quad":
121
                    return Quad::create();
122
                case "frame":
123
                    return Frame::create();
124
                case "include":
125
                    return $this->build($node->nodeValue);
126
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
127
                default:
128
                    return Frame::create();
129
            }
130
        }
131
    }
132
133
134
    private function parseAttributes(&$result, $attr, \DOMNode $node)
135
    {
136
        switch ($attr->name) {
137 View Code Duplication
            case "size":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
138
                list($x, $y) = explode(" ", $attr->value);
139
                $result->setSize($x, $y);
140
141
                return;
142 View Code Duplication
            case "pos":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
143
                list($x, $y) = explode(" ", $attr->value);
144
                $result->setPosition($x, $y);
145
146
                return;
147
            case "actionCallback":
148
                $param = null;
149
                foreach ($node->attributes as $index => $attr2) {
150
                    if ($attr2->name == 'actionParam') {
151
                        $param = ["id" => $attr2->value];
152
                        break;
153
                    }
154
                }
155
                $action = $this->actionFactory->createManialinkAction($this->manialink,
156
                    [$this->pluginClass, $attr->value], $param, false);
0 ignored issues
show
Bug introduced by
It seems like $param defined by null on line 148 can also be of type null; however, eXpansion\Framework\Core...createManialinkAction() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
157
158
                $result->setAction($action);
159
160
                return;
161
            default:
162
                if ($attr->name == "actionParam") {
163
                    return;
164
                }
165
                $method = "set".ucfirst($attr->name);
166
                $result->{$method}($attr->value);
167
168
                return;
169
        }
170
    }
171
172
}