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 Tree 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 Tree, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Tree extends NamedElement |
||
17 | { |
||
18 | protected $type = 'tree'; |
||
19 | |||
20 | protected $defaultValue = []; |
||
21 | |||
22 | protected $rootNodeValue = ''; |
||
23 | |||
24 | protected $nodesModelParentAttribute = 'parent_id'; |
||
25 | |||
26 | protected $nodesModelLabelAttribute; |
||
27 | |||
28 | protected $nodesModelValueAttribute; |
||
29 | |||
30 | protected $nodes; |
||
31 | |||
32 | public function __construct(string $name, string $title, $nodes = null) |
||
40 | |||
41 | public function getValue() |
||
59 | |||
60 | public function save() |
||
66 | |||
67 | View Code Duplication | public function finishSave() |
|
80 | |||
81 | protected function isNodesModel() |
||
85 | |||
86 | protected function isRelation() |
||
90 | |||
91 | public function getNodes() |
||
107 | |||
108 | /** |
||
109 | * @param $nodes |
||
110 | * @param $parentId |
||
111 | * |
||
112 | * @return \Illuminate\Support\Collection |
||
113 | */ |
||
114 | public function getNodesTree($nodes, $parentId) |
||
144 | |||
145 | public function setNodes($nodes) |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function setNodesFromModel() |
||
189 | |||
190 | /** |
||
191 | * @return Model |
||
192 | */ |
||
193 | View Code Duplication | protected function getNodesModel() |
|
212 | |||
213 | public function getNodesModelLabelAttribute() |
||
217 | |||
218 | public function setNodesModelLabelAttribute($value) |
||
224 | |||
225 | public function getNodesModelValueAttribute() |
||
229 | |||
230 | public function setNodesModelValueAttribute($value) |
||
236 | |||
237 | public function getNodesModelParentAttribute() |
||
241 | |||
242 | public function setNodesModelParentAttribute($value) |
||
248 | |||
249 | public function getRootNodeValue() |
||
253 | |||
254 | public function setRootNodeValue($value) |
||
260 | |||
261 | public function toArray() |
||
270 | } |
||
271 |
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.