Completed
Pull Request — master (#270)
by
unknown
03:25
created

LayoutRow   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 340
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 3
dl 17
loc 340
rs 9.3999
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A updateSize() 7 13 3
A setPosition() 0 7 1
A setX() 0 6 1
A setY() 0 6 1
A render() 0 16 2
A getScriptFeatures() 10 11 3
A getX() 0 4 1
A getY() 0 4 1
A getHeight() 0 4 1
A setHeight() 0 4 1
A setWidth() 0 6 1
A getWidth() 0 4 1
A addChild() 0 5 1
A getChildren() 0 4 1
A addClass() 0 6 1
A add() 0 4 1
A addChildren() 0 6 2
A removeAllChildren() 0 8 1
A removeChildren() 0 4 1
A getFormat() 0 4 1
A setFormat() 0 4 1
A setSize() 0 8 1
A setAlign() 0 7 1
A getHorizontalAlign() 0 4 1
A getVerticalAlign() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace eXpansion\Framework\Gui\Layouts;
4
5
use FML\Controls\Control;
6
use FML\Controls\Frame;
7
use FML\Elements\Format;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Types\Container;
10
use FML\Types\Renderable;
11
use FML\Types\ScriptFeatureable;
12
13
class LayoutRow implements Renderable, ScriptFeatureable, Container
14
{
15
16
    protected $frameClasses = [];
17
    /**
18
     * @var float|int
19
     */
20
    protected $height = 0;
21
    /**
22
     * @var float|int
23
     */
24
    protected $width = 0;
25
26
    /** @var Control[] */
27
    protected $elements = [];
28
29
    /**
30
     * @var float|int
31
     */
32
    protected $margin = 1;
33
34
    /**
35
     * @var float|int
36
     */
37
    protected $startX;
38
39
    /**
40
     * @var float|int
41
     */
42
    protected $startY;
43
44
    protected $hAlign = "left";
45
    protected $vAlign = "top";
46
47
48
    /**
49
     * layoutLine constructor.
50
     * @param float    $startX
51
     * @param float    $startY
52
     * @param object[] $elements
53
     * @param int      $margin
54
     * @throws \Exception
55
     */
56
    public function __construct($startX, $startY, $elements = [], $margin = 0)
57
    {
58
        if (!is_array($elements)) {
59
            throw new \Exception('not an array');
60
        }
61
62
        $this->margin = $margin;
63
        $this->elements = $elements;
64
        $this->setPosition($startX, $startY);
65
        $this->updateSize();
66
    }
67
68
    protected function updateSize()
69
    {
70
        $sizeX = 0;
71
        $sizeY = 0;
72 View Code Duplication
        foreach ($this->elements as $idx => $element) {
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...
73
            $sizeY += $element->getY() + $element->getHeight() + $this->margin;
74
75
            if (abs($element->getX()) + $element->getWidth() > $sizeX) {
76
                $sizeX = abs($element->getX()) + $element->getWidth();
77
            }
78
        }
79
        $this->setSize($sizeX, $sizeY);
80
    }
81
82
    /**
83
     * @param double $x
84
     * @param double $y
85
     * @return LayoutRow
86
     */
87
    public function setPosition($x, $y)
88
    {
89
        $this->startX = $x;
90
        $this->startY = $y;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param mixed $startX
97
     * @return LayoutRow
98
     */
99
    public function setX($startX)
100
    {
101
        $this->startX = $startX;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param mixed $startY
108
     * @return LayoutRow
109
     */
110
    public function setY($startY)
111
    {
112
        $this->startY = $startY;
113
114
        return $this;
115
    }
116
117
118
    /**
119
     * Render the XML element
120
     *
121
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
122
     * @return \DOMElement
123
     */
124
    public function render(\DOMDocument $domDocument)
125
    {
126
        $frame = new Frame();
127
        $frame->setAlign($this->hAlign, $this->vAlign);
128
        $frame->setPosition($this->startX, $this->startY);
129
        $frame->addClasses($this->frameClasses);
130
131
        $startY = 0;
132
        foreach ($this->elements as $idx => $element) {
133
            $element->setY($startY);
134
            $startY -= $element->getHeight() - $this->margin;
135
            $frame->addChild($element);
136
        }
137
138
        return $frame->render($domDocument);
139
    }
140
141
    /**
142
     * Get the Script Features
143
     *
144
     * @return ScriptFeature[]
145
     */
146 View Code Duplication
    public function getScriptFeatures()
0 ignored issues
show
Duplication introduced by
This method 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...
147
    {
148
        $features = [];
149
        foreach ($this->elements as $element) {
150
            if ($element instanceof ScriptFeatureable) {
151
                $features[] = $element->getScriptFeatures();
152
            }
153
        }
154
155
        return ScriptFeature::collect($features);
156
    }
157
158
    /**
159
     * @return mixed
160
     */
161
    public function getX()
162
    {
163
        return $this->startX;
164
    }
165
166
    /**
167
     * @return mixed
168
     */
169
    public function getY()
170
    {
171
        return $this->startY;
172
    }
173
174
    /**
175
     * @return float
176
     */
177
    public function getHeight()
178
    {
179
        return $this->height;
180
    }
181
182
    /**
183
     * @param float $height
184
     */
185
    public function setHeight($height)
186
    {
187
        $this->height = $height;
188
    }
189
190
    /**
191
     * @param float $width
192
     * @return LayoutRow
193
     */
194
    public function setWidth($width)
195
    {
196
        $this->width = $width;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return float|int
203
     */
204
    public function getWidth()
205
    {
206
        return $this->width;
207
    }
208
209
    /**
210
     * @param Renderable $element
211
     */
212
    public function addChild(Renderable $element)
213
    {
214
        $this->elements[] = $element;
215
        $this->updateSize();
216
    }
217
218
    public function getChildren()
219
    {
220
        return $this->elements;
221
    }
222
223
224
    /**
225
     * @param string $class
226
     * @return LayoutRow
227
     */
228
    public function addClass($class)
229
    {
230
        $this->frameClasses[] = $class;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Add a new child
237
     *
238
     * @api
239
     * @param Renderable $child Child Control to add
240
     * @deprecated Use addChild()
241
     * @see        Container::addChild()
242
     */
243
    public function add(Renderable $child)
244
    {
245
246
    }
247
248
    /**
249
     * Add new children
250
     *
251
     * @api
252
     *
253
     * @param Renderable[] $children Child Controls to add
254
     *
255
     */
256
    public function addChildren(array $children)
257
    {
258
        foreach ($children as $child) {
259
            $this->addChild($child);
260
        }
261
    }
262
263
    /**
264
     * Remove all children
265
     *
266
     * @api
267
     *
268
     */
269
    public function removeAllChildren()
270
    {
271
        $this->width = 0;
272
        $this->height = 0;
273
        $this->elements = [];
274
275
        return $this;
276
    }
277
278
    /**
279
     * Remove all children
280
     *
281
     * @api
282
     * @deprecated Use removeAllChildren()
283
     * @see        Container::removeAllChildren()
284
     */
285
    public function removeChildren()
286
    {
287
288
    }
289
290
    /**
291
     * Get the Format
292
     *
293
     * @api
294
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
295
     * @deprecated Use Style
296
     * @see        Style
297
     */
298
    public function getFormat($createIfEmpty = true)
299
    {
300
301
    }
302
303
    /**
304
     * Set the Format
305
     *
306
     * @api
307
     * @param Format $format New Format
308
     * @deprecated Use Style
309
     * @see        Style
310
     */
311
    public function setFormat(Format $format = null)
312
    {
313
314
    }
315
316
    /**
317
     * @param float $sizeX
318
     * @param float $sizeY
319
     * @return LayoutRow
320
     */
321
    private function setSize($sizeX, $sizeY)
322
    {
323
        $this->width = $sizeX;
324
        $this->height = $sizeY;
325
326
        return $this;
327
328
    }
329
330
    /**
331
     * @param string $hAling
332
     * @param string $vAlign
333
     * @return LayoutRow
334
     */
335
    public function setAlign($hAling = "left", $vAlign = "top")
336
    {
337
        $this->hAlign = $hAling;
338
        $this->vAlign = $vAlign;
339
340
        return $this;
341
    }
342
343
    public function getHorizontalAlign()
344
    {
345
        return $this->hAlign;
346
    }
347
348
    public function getVerticalAlign()
349
    {
350
        return $this->vAlign;
351
    }
352
}
353