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 ElggMenuBuilder 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 ElggMenuBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class ElggMenuBuilder { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var \ElggMenuItem[] |
||
| 13 | */ |
||
| 14 | protected $menu = array(); |
||
| 15 | |||
| 16 | protected $selected = null; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * \ElggMenuBuilder constructor |
||
| 20 | * |
||
| 21 | * @param \ElggMenuItem[] $menu Array of \ElggMenuItem objects |
||
| 22 | */ |
||
| 23 | public function __construct(array $menu) { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get a prepared menu array |
||
| 29 | * |
||
| 30 | * @param mixed $sort_by Method to sort the menu by. @see \ElggMenuBuilder::sort() |
||
| 31 | * @return array |
||
| 32 | */ |
||
| 33 | public function getMenu($sort_by = 'text') { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get the selected menu item |
||
| 50 | * |
||
| 51 | * @return \ElggMenuItem |
||
| 52 | */ |
||
| 53 | public function getSelected() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Select menu items for the current context |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | protected function selectFromContext() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Group the menu items into sections |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | protected function setupSections() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Create trees for each menu section |
||
| 101 | * |
||
| 102 | * @internal The tree is doubly linked (parent and children links) |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | protected function setupTrees() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Find the menu item that is currently selected |
||
| 177 | * |
||
| 178 | * @return \ElggMenuItem |
||
| 179 | */ |
||
| 180 | protected function findSelected() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sort the menu sections and trees |
||
| 204 | * |
||
| 205 | * @param mixed $sort_by Sort type as string or php callback |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | protected function sort($sort_by) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Compare two menu items by their display text |
||
| 263 | * HTML tags are stripped before comparison |
||
| 264 | * |
||
| 265 | * @param \ElggMenuItem $a Menu item |
||
| 266 | * @param \ElggMenuItem $b Menu item |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public static function compareByText($a, $b) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Compare two menu items by their identifiers |
||
| 282 | * |
||
| 283 | * @param \ElggMenuItem $a Menu item |
||
| 284 | * @param \ElggMenuItem $b Menu item |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public static function compareByName($a, $b) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Compare two menu items by their priority |
||
| 300 | * |
||
| 301 | * @param \ElggMenuItem $a Menu item |
||
| 302 | * @param \ElggMenuItem $b Menu item |
||
| 303 | * @return bool |
||
| 304 | * @since 1.9.0 |
||
| 305 | */ |
||
| 306 | View Code Duplication | public static function compareByPriority($a, $b) { |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Compare two menu items by their priority |
||
| 318 | * |
||
| 319 | * @param \ElggMenuItem $a Menu item |
||
| 320 | * @param \ElggMenuItem $b Menu item |
||
| 321 | * @return bool |
||
| 322 | * @deprecated 1.9 Use compareByPriority() |
||
| 323 | */ |
||
| 324 | View Code Duplication | public static function compareByWeight($a, $b) { |
|
| 334 | } |
||
| 335 |