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 HtmlMenu 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 HtmlMenu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class HtmlMenu extends HtmlSemCollection { |
||
29 | use AttachedTrait; |
||
30 | private $_itemHeader; |
||
31 | |||
32 | public function __construct($identifier, $items=array()) { |
||
36 | |||
37 | /** |
||
38 | * Sets the menu type |
||
39 | * @param string $type one of text,item |
||
40 | * @return \Ajax\semantic\html\collections\HtmlMenu |
||
41 | */ |
||
42 | public function setType($type="") { |
||
45 | |||
46 | public function setActiveItem($index) { |
||
53 | |||
54 | private function getItemToInsert($item) { |
||
61 | |||
62 | private function afterInsert($item) { |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * {@inheritDoc} |
||
74 | * |
||
75 | * @see \Ajax\common\html\html5\HtmlCollection::addItem() |
||
76 | */ |
||
77 | public function addItem($item) { |
||
84 | |||
85 | /** |
||
86 | * |
||
87 | * {@inheritDoc} |
||
88 | * |
||
89 | * @see \Ajax\common\html\HtmlCollection::insertItem() |
||
90 | */ |
||
91 | public function insertItem($item, $position=0) { |
||
95 | |||
96 | public function generateMenuAsItem($menu, $header=null) { |
||
112 | |||
113 | public function addMenuAsItem($menu, $header=null) { |
||
116 | |||
117 | public function addPopupAsItem($value, $identifier, $content="") { |
||
126 | |||
127 | View Code Duplication | public function addDropdownAsItem($value, $items=NULL) { |
|
135 | |||
136 | /** |
||
137 | * |
||
138 | * {@inheritDoc} |
||
139 | * |
||
140 | * @see \Ajax\common\html\html5\HtmlCollection::createItem() |
||
141 | */ |
||
142 | protected function createItem($value) { |
||
148 | |||
149 | public function setInverted() { |
||
152 | |||
153 | public function setSecondary($value=true) { |
||
160 | |||
161 | public function setVertical() { |
||
164 | |||
165 | public function setPosition($value="right") { |
||
168 | |||
169 | public function setPointing($value=Direction::NONE) { |
||
172 | |||
173 | public function asTab($vertical=false) { |
||
181 | |||
182 | public function asPagination() { |
||
188 | |||
189 | public function setFixed() { |
||
192 | |||
193 | public function setFluid() { |
||
196 | |||
197 | public function setCompact() { |
||
200 | |||
201 | /* |
||
202 | * (non-PHPdoc) |
||
203 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
204 | */ |
||
205 | public function fromDatabaseObject($object, $function) { |
||
212 | |||
213 | /** |
||
214 | * Defines the menu width |
||
215 | * @param int $width |
||
216 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
217 | */ |
||
218 | View Code Duplication | public function setWidth($width) { |
|
225 | |||
226 | public function addImage($identifier, $src="", $alt="") { |
||
229 | |||
230 | public static function vertical($identifier, $items=array()) { |
||
233 | |||
234 | public function getItemHeader() { |
||
237 | |||
238 | public function setHasContainer(){ |
||
241 | |||
242 | public function run(JsUtils $js){ |
||
247 | } |
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.