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:
Complex classes like LayoutLine often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LayoutLine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class LayoutLine implements Renderable, ScriptFeatureable, Container |
||
14 | { |
||
15 | protected $frameClasses = []; |
||
16 | |||
17 | protected $frameId = null; |
||
18 | |||
19 | /** @var float */ |
||
20 | protected $width = 0.; |
||
21 | |||
22 | /** @var float */ |
||
23 | protected $height = 0.; |
||
24 | |||
25 | /** @var Control[] */ |
||
26 | protected $elements = []; |
||
27 | |||
28 | /** @var float */ |
||
29 | private $margin = 2.; |
||
30 | /** |
||
31 | * @var float |
||
32 | */ |
||
33 | protected $startX = 0.; |
||
34 | /** |
||
35 | * @var float |
||
36 | */ |
||
37 | protected $startY = 0.; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $hAlign = "left"; |
||
41 | |||
42 | /** @var string */ |
||
43 | protected $vAlign = "top"; |
||
44 | |||
45 | /** |
||
46 | * layoutLine constructor. |
||
47 | * @param float $startX |
||
48 | * @param float $startY |
||
49 | * @param object[] $elements |
||
50 | * @param float $margin |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | public function __construct($startX, $startY, $elements = [], $margin = 0.) |
||
72 | |||
73 | /** |
||
74 | * Render the XML element |
||
75 | * |
||
76 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
77 | * @return \DOMElement |
||
78 | */ |
||
79 | public function render(\DOMDocument $domDocument) |
||
136 | |||
137 | /** |
||
138 | * @param Control $element |
||
139 | * @return double |
||
140 | */ |
||
141 | private function getRelativeStartPosition($element) |
||
157 | |||
158 | /** |
||
159 | * @param Control $element |
||
160 | * @return float|int |
||
161 | */ |
||
162 | private function getStartPosition($element) |
||
175 | |||
176 | /** |
||
177 | * Get the Script Features |
||
178 | * |
||
179 | * @return ScriptFeature[] |
||
180 | */ |
||
181 | View Code Duplication | public function getScriptFeatures() |
|
192 | |||
193 | /** |
||
194 | * @param mixed $startX |
||
195 | * @return LayoutLine |
||
196 | */ |
||
197 | public function setX($startX) |
||
203 | |||
204 | /** |
||
205 | * @param mixed $startY |
||
206 | * @return LayoutLine |
||
207 | */ |
||
208 | public function setY($startY) |
||
214 | |||
215 | /** |
||
216 | * @return float |
||
217 | */ |
||
218 | public function getX() |
||
222 | |||
223 | /** |
||
224 | * @return double |
||
225 | */ |
||
226 | public function getY() |
||
230 | |||
231 | /** |
||
232 | * @return float |
||
233 | */ |
||
234 | public function getWidth() |
||
238 | |||
239 | /** |
||
240 | * @param float $width |
||
241 | */ |
||
242 | public function setWidth($width) |
||
246 | |||
247 | /** |
||
248 | * @return float |
||
249 | */ |
||
250 | public function getHeight() |
||
254 | |||
255 | /** |
||
256 | * @param float $height |
||
257 | */ |
||
258 | public function setHeight($height) |
||
262 | |||
263 | /** |
||
264 | * @param Renderable $element |
||
265 | */ |
||
266 | public function addChild(Renderable $element) |
||
274 | |||
275 | public function getChildren() |
||
279 | |||
280 | |||
281 | public function addClass($class) |
||
285 | |||
286 | /** |
||
287 | * Add a new child |
||
288 | * |
||
289 | * @api |
||
290 | * @param Renderable $child Child Control to add |
||
291 | * @return void |
||
292 | * @deprecated Use addChild() |
||
293 | * @see Container::addChild() |
||
294 | */ |
||
295 | public function add(Renderable $child) |
||
299 | |||
300 | /** |
||
301 | * Add new children |
||
302 | * |
||
303 | * @api |
||
304 | * @param Renderable[] $children Child Controls to add |
||
305 | * @return void |
||
306 | */ |
||
307 | public function addChildren(array $children) |
||
313 | |||
314 | /** |
||
315 | * Remove all children |
||
316 | * |
||
317 | * @api |
||
318 | * @return static |
||
319 | */ |
||
320 | public function removeAllChildren() |
||
328 | |||
329 | /** |
||
330 | * Remove all children |
||
331 | * |
||
332 | * @api |
||
333 | * @return void |
||
334 | * @deprecated Use removeAllChildren() |
||
335 | * @see Container::removeAllChildren() |
||
336 | */ |
||
337 | public function removeChildren() |
||
341 | |||
342 | /** |
||
343 | * Get the Format |
||
344 | * |
||
345 | * @api |
||
346 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
347 | * @return void |
||
348 | * @deprecated Use Style |
||
349 | * @see Style |
||
350 | */ |
||
351 | public function getFormat($createIfEmpty = true) |
||
355 | |||
356 | /** |
||
357 | * Set the Format |
||
358 | * |
||
359 | * @api |
||
360 | * @param Format $format New Format |
||
361 | * @return void |
||
362 | * @deprecated Use Style |
||
363 | * @see Style |
||
364 | */ |
||
365 | public function setFormat(Format $format = null) |
||
369 | |||
370 | public function setAlign($hAling = "left", $vAlign = "top") |
||
377 | |||
378 | |||
379 | public function setPosition($x, $y) |
||
386 | |||
387 | /** |
||
388 | * @return null|string |
||
389 | */ |
||
390 | public function getId() |
||
394 | |||
395 | /** |
||
396 | * @param null|string $frameId |
||
397 | * @return LayoutLine |
||
398 | */ |
||
399 | public function setId($frameId) |
||
405 | |||
406 | /** |
||
407 | * @return string |
||
408 | */ |
||
409 | public function getHorizontalAlign(): string |
||
413 | |||
414 | /** |
||
415 | * @param string $hAlign |
||
416 | * @return LayoutLine |
||
417 | */ |
||
418 | public function setHorizontalAlign(string $hAlign) |
||
424 | |||
425 | } |
||
426 |
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.