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 BaseTrait 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 BaseTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait BaseTrait { |
||
18 | protected $_variations=[ ]; |
||
19 | protected $_states=[ ]; |
||
20 | protected $_baseClass; |
||
21 | |||
22 | abstract protected function setPropertyCtrl($name, $value, $typeCtrl); |
||
23 | |||
24 | abstract protected function addToPropertyCtrl($name, $value, $typeCtrl); |
||
25 | |||
26 | abstract protected function addToPropertyCtrlCheck($name, $value, $typeCtrl); |
||
27 | |||
28 | abstract public function addToProperty($name, $value, $separator=" "); |
||
29 | |||
30 | abstract public function setProperty($name, $value); |
||
31 | |||
32 | abstract public function addContent($content,$before=false); |
||
33 | |||
34 | abstract public function onCreate($jsCode); |
||
35 | |||
36 | public function addVariation($variation) { |
||
39 | |||
40 | public function addState($state) { |
||
43 | |||
44 | public function setVariation($variation) { |
||
48 | |||
49 | View Code Duplication | public function setVariations($variations) { |
|
58 | |||
59 | public function setState($state) { |
||
63 | |||
64 | public function addVariations($variations=array()) { |
||
72 | |||
73 | public function addStates($states=array()) { |
||
81 | |||
82 | View Code Duplication | public function setStates($states) { |
|
91 | |||
92 | public function addIcon($icon, $before=true) { |
||
95 | |||
96 | public function addSticky($context="body"){ |
||
100 | |||
101 | /** |
||
102 | * |
||
103 | * {@inheritDoc} |
||
104 | * |
||
105 | * @see \Ajax\common\html\HtmlSingleElement::setSize() |
||
106 | */ |
||
107 | public function setSize($size) { |
||
110 | |||
111 | /** |
||
112 | * show it is currently unable to be interacted with |
||
113 | * @param boolean $disable |
||
114 | * @return HtmlSemDoubleElement |
||
115 | */ |
||
116 | public function setDisabled($disable=true) { |
||
121 | |||
122 | /** |
||
123 | * |
||
124 | * @param string $color |
||
125 | * @return HtmlSemDoubleElement |
||
126 | */ |
||
127 | public function setColor($color) { |
||
130 | |||
131 | /** |
||
132 | * |
||
133 | * @return HtmlSemDoubleElement |
||
134 | */ |
||
135 | public function setFluid() { |
||
138 | |||
139 | /** |
||
140 | * |
||
141 | * @return HtmlSemDoubleElement |
||
142 | */ |
||
143 | public function asHeader(){ |
||
146 | |||
147 | /** |
||
148 | * show it is currently the active user selection |
||
149 | * @return HtmlSemDoubleElement |
||
150 | */ |
||
151 | public function setActive($value=true){ |
||
156 | |||
157 | public function setAttached($value=true){ |
||
162 | |||
163 | /** |
||
164 | * can be formatted to appear on dark backgrounds |
||
165 | */ |
||
166 | public function setInverted($recursive=true) { |
||
181 | |||
182 | public function setCircular() { |
||
185 | |||
186 | public function setFloated($direction="right") { |
||
189 | |||
190 | public function floatRight() { |
||
193 | |||
194 | public function floatLeft() { |
||
197 | |||
198 | public function getBaseClass() { |
||
201 | |||
202 | protected function addBehavior(&$array,$key,$value,$before="",$after=""){ |
||
214 | |||
215 | public function getVariations() { |
||
218 | |||
219 | public function getStates() { |
||
222 | |||
223 | /* |
||
224 | protected function addBehavior(&$array,$key,$value,$before="",$after=""){ |
||
225 | echo $key.":".$this->_self->identifier."<br>"; |
||
226 | |||
227 | if(\is_string($value)){ |
||
228 | if(isset($array[$key])){ |
||
229 | $p=JString::replaceAtFirstAndLast($array[$key], $before, "", $after, ""); |
||
230 | $array[$key]=$before.$p.$value.$after; |
||
231 | }else |
||
232 | $array[$key]=$before.$value.$after; |
||
233 | }else{ |
||
234 | if(isset($array[$key])){ |
||
235 | if(!\is_array($array[$key])){ |
||
236 | $array[$key]=[$array[$key]]; |
||
237 | } |
||
238 | $array[$key][]=$value; |
||
239 | }else{ |
||
240 | $array[$key]=$value; |
||
241 | } |
||
242 | } |
||
243 | return $this; |
||
244 | }*/ |
||
245 | } |
||
246 |
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.