Completed
Pull Request — master (#95)
by
unknown
02:41
created

layoutLine::getWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Gui\Layouts;
4
5
use FML\Controls\Control;
6
use FML\Controls\Frame;
7
use FML\Script\Features\ScriptFeature;
8
use FML\Types\Container;
9
use FML\Types\Renderable;
10
use FML\Types\ScriptFeatureable;
11
12 View Code Duplication
class layoutLine implements Renderable, ScriptFeatureable
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
13
{
14
    private $width = 0;
15
    private $height = 0;
16
    /** @var Control[] */
17
    private $elements = [];
18
19
    private $margin = 2;
20
    /**
21
     * @var
22
     */
23
    private $startX = 0;
24
    /**
25
     * @var
26
     */
27
    private $startY = 0;
28
29
    /**
30
     * layoutLine constructor.
31
     * @param $startX
32
     * @param $startY
33
     * @param Control[] $elements
34
     * @param $margin
35
     * @throws \Exception
36
     */
37
    public function __construct($startX, $startY, $elements = [], $margin = 0)
38
    {
39
        if (!is_array($elements)) {
40
            throw new \Exception('not an array');
41
        }
42
        $this->margin = $margin;
43
        $this->elements = $elements;
44
        $this->startX = $startX;
45
        $this->startY = $startY;
46
        foreach ($this->elements as $idx => $element) {
47
            $this->width += $element->getWidth() + $this->margin;
48
            $this->height += $element->getHeight();
49
        }
50
51
    }
52
53
    /**
54
     * Render the XML element
55
     *
56
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
57
     * @return \DOMElement
58
     */
59
    public function render(\DOMDocument $domDocument)
60
    {
61
        $frame = new Frame();
62
        $frame->setPosition($this->startX, $this->startY);
63
64
        $startX = 0;
65
        foreach ($this->elements as $idx => $element) {
66
            $element->setX($startX);
67
            $startX += $element->getWidth() + $this->margin;
68
            $frame->addChild($element);
69
        }
70
71
        return $frame->render($domDocument);
72
    }
73
74
    /**
75
     * Get the Script Features
76
     *
77
     * @return ScriptFeature[]
78
     */
79
    public function getScriptFeatures()
80
    {
81
        $features = [];
82
        foreach ($this->elements as $element) {
83
            if ($element instanceof ScriptFeatureable) {
84
                $features[] = $element->getScriptFeatures();
85
            }
86
        }
87
88
        return ScriptFeature::collect($features);
89
    }
90
91
    /**
92
     * @param mixed $startX
93
     * @return layoutLine
94
     */
95
    public function setX($startX)
96
    {
97
        $this->startX = $startX;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param mixed $startY
104
     * @return layoutLine
105
     */
106
    public function setY($startY)
107
    {
108
        $this->startY = $startY;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function getX()
117
    {
118
        return $this->startX;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function getY()
125
    {
126
        return $this->startY;
127
    }
128
129
    /**
130
     * @return float
131
     */
132
    public function getWidth()
133
    {
134
        return $this->width;
135
    }
136
137
    /**
138
     * @param float $width
139
     */
140
    public function setWidth($width)
141
    {
142
        $this->width = $width;
0 ignored issues
show
Documentation Bug introduced by
The property $width was declared of type integer, but $width is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
143
    }
144
145
    /**
146
     * @return float
147
     */
148
    public function getHeight()
149
    {
150
        return $this->height;
151
    }
152
153
    /**
154
     * @param float $height
155
     */
156
    public function setHeight($height)
157
    {
158
        $this->height = $height;
0 ignored issues
show
Documentation Bug introduced by
The property $height was declared of type integer, but $height is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
159
    }
160
161
    public function addElement($element)
162
    {
163
        $this->elements[] = $element;
164
        $this->width += $element->getWidth() + $this->margin;
165
        $this->height += $element->getHeight();
166
    }
167
}
168