Completed
Pull Request — master (#123)
by De Cramer
12:37
created

layoutRow   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 303
Duplicated Lines 3.3 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 48.86%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 30
lcom 1
cbo 3
dl 10
loc 303
c 2
b 0
f 1
ccs 43
cts 88
cp 0.4886
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A updateSize() 0 13 3
A setPosition() 0 5 1
A setX() 0 6 1
A setY() 0 6 1
A render() 0 18 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 4 1
A add() 0 4 1
A addChildren() 0 4 1
A removeAllChildren() 0 4 1
A removeChildren() 0 4 1
A getFormat() 0 4 1
A setFormat() 0 4 1
A setSize() 0 5 1
A setAlign() 0 5 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\Controls\Quad;
8
use FML\Elements\Format;
9
use FML\Script\Features\ScriptFeature;
10
use FML\Types\Container;
11
use FML\Types\Renderable;
12
use FML\Types\ScriptFeatureable;
13
14
class layoutRow implements Renderable, ScriptFeatureable, Container
15
{
16
17
    protected $frameClasses = [];
18
    /**
19
     * @var float|int
20
     */
21
    protected $height = 0;
22
    /**
23
     * @var float|int
24
     */
25
    protected $width = 0;
26
27
    /** @var Control[] */
28
    protected $elements = [];
29
30
    /**
31
     * @var float|int
32
     */
33
    protected $margin = 1;
34
35
    /**
36
     * @var float|int
37
     */
38
    protected $startX;
39
40
    /**
41
     * @var float|int
42
     */
43
    protected $startY;
44
45
    protected $hAlign = "left";
46
    protected $vAlign = "top";
47
48
49
    /**
50
     * layoutLine constructor.
51
     * @param float $startX
52
     * @param float $startY
53
     * @param object[] $elements
54
     * @param int $margin
55
     * @throws \Exception
56
     */
57 1
    public function __construct($startX, $startY, $elements = [], $margin = 0)
58
    {
59 1
        if (!is_array($elements)) {
60
            throw new \Exception('not an array');
61
        }
62
63 1
        $this->margin = $margin;
64 1
        $this->elements = $elements;
65 1
        $this->setPosition($startX, $startY);
66 1
        $this->updateSize();
67 1
    }
68
69 1
    protected function updateSize()
70
    {
71 1
        $sizeX = 0;
72 1
        $sizeY = 0;
73 1
        foreach ($this->elements as $idx => $element) {
74 1
            $sizeY += $element->getY() + $element->getHeight() + $this->margin;
75
76 1
            if (abs($element->getX()) + $element->getWidth() > $sizeX) {
77 1
                $sizeX = abs($element->getX()) + $element->getWidth();
78
            }
79
        }
80 1
        $this->setSize($sizeX, $sizeY);
81 1
    }
82
83 1
    public function setPosition($x, $y)
84
    {
85 1
        $this->startX = $x;
86 1
        $this->startY = $y;
87 1
    }
88
89
    /**
90
     * @param mixed $startX
91
     * @return
92
     */
93
    public function setX($startX)
94
    {
95
        $this->startX = $startX;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param mixed $startY
102
     * @return
103
     */
104
    public function setY($startY)
105
    {
106
        $this->startY = $startY;
107
108
        return $this;
109
    }
110
111
112
    /**
113
     * Render the XML element
114
     *
115
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
116
     * @return \DOMElement
117
     */
118 1
    public function render(\DOMDocument $domDocument)
119
    {
120 1
        $frame = new Frame();
121 1
        $frame->setAlign($this->hAlign, $this->vAlign);
122 1
        $frame->setPosition($this->startX, $this->startY);
123 1
        $frame->addClasses($this->frameClasses);
124
125 1
        $startY = 0;
126
127 1
        foreach ($this->elements as $idx => $element) {
128 1
            $pos = $element->getY();
0 ignored issues
show
Unused Code introduced by
$pos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129 1
            $element->setY($startY);
130 1
            $startY -= $element->getHeight() - $this->margin;
131 1
            $frame->addChild($element);
132
        }
133
134 1
        return $frame->render($domDocument);
135
    }
136
137
    /**
138
     * Get the Script Features
139
     *
140
     * @return ScriptFeature[]
141
     */
142 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...
143
    {
144
        $features = [];
145
        foreach ($this->elements as $element) {
146
            if ($element instanceof ScriptFeatureable) {
147
                $features[] = $element->getScriptFeatures();
148
            }
149
        }
150
151
        return ScriptFeature::collect($features);
152
    }
153
154
    /**
155
     * @return mixed
156
     */
157
    public function getX()
158
    {
159
        return $this->startX;
160
    }
161
162
    /**
163
     * @return mixed
164
     */
165
    public function getY()
166
    {
167
        return $this->startY;
168
    }
169
170
    /**
171
     * @return float
172
     */
173
    public function getHeight()
174
    {
175
        return $this->height;
176
    }
177
178
    /**
179
     * @param float $height
180
     */
181
    public function setHeight($height)
182
    {
183
        $this->height = $height;
184
    }
185
186
    /**
187
     * @param float $width
188
     * @return layoutRow
189
     */
190
    public function setWidth($width)
191
    {
192
        $this->width = $width;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return float|int
199
     */
200
    public function getWidth()
201
    {
202
        return $this->width;
203
    }
204
205
    /**
206
     * @param Renderable $element
207
     */
208 1
    public function addChild(Renderable $element)
209
    {
210 1
        $this->elements[] = $element;
211 1
        $this->updateSize();
212 1
    }
213
214
    public function getChildren()
215
    {
216
        return $this->elements;
217
    }
218
219
220 1
    public function addClass($class)
221
    {
222 1
        $this->frameClasses[] = $class;
223 1
    }
224
225
    /**
226
     * Add a new child
227
     *
228
     * @api
229
     * @param Renderable $child Child Control to add
230
     * @return static
231
     * @deprecated Use addChild()
232
     * @see        Container::addChild()
233
     */
234
    public function add(Renderable $child)
235
    {
236
        // TODO: Implement add() method.
237
    }
238
239
    /**
240
     * Add new children
241
     *
242
     * @api
243
     * @param Renderable[] $children Child Controls to add
244
     * @return static
245
     */
246
    public function addChildren(array $children)
247
    {
248
        // TODO: Implement addChildren() method.
249
    }
250
251
    /**
252
     * Remove all children
253
     *
254
     * @api
255
     * @return static
256
     */
257
    public function removeAllChildren()
258
    {
259
        // TODO: Implement removeAllChildren() method.
260
    }
261
262
    /**
263
     * Remove all children
264
     *
265
     * @api
266
     * @return static
267
     * @deprecated Use removeAllChildren()
268
     * @see        Container::removeAllChildren()
269
     */
270
    public function removeChildren()
271
    {
272
        // TODO: Implement removeChildren() method.
273
    }
274
275
    /**
276
     * Get the Format
277
     *
278
     * @api
279
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
280
     * @return Format
281
     * @deprecated Use Style
282
     * @see        Style
283
     */
284
    public function getFormat($createIfEmpty = true)
285
    {
286
        // TODO: Implement getFormat() method.
287
    }
288
289
    /**
290
     * Set the Format
291
     *
292
     * @api
293
     * @param Format $format New Format
294
     * @return static
295
     * @deprecated Use Style
296
     * @see        Style
297
     */
298
    public function setFormat(Format $format = null)
299
    {
300
        // TODO: Implement setFormat() method.
301
    }
302
303 1
    private function setSize($sizeX, $sizeY)
304
    {
305 1
        $this->width = $sizeX;
306 1
        $this->height = $sizeY;
307 1
    }
308
309
    public function setAlign($hAling = "left", $vAlign = "top")
310
    {
311
        $this->halign = $hAling;
0 ignored issues
show
Bug introduced by
The property halign does not seem to exist. Did you mean hAlign?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
312
        $this->valign = $vAlign;
0 ignored issues
show
Bug introduced by
The property valign does not seem to exist. Did you mean vAlign?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
313
    }
314
315
316
}
317