Complex classes like TbBaseMenu 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 TbBaseMenu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class TbBaseMenu extends CMenu { |
||
| 18 | /** |
||
| 19 | *### .getDividerCssClass() |
||
| 20 | * |
||
| 21 | * Returns the divider css class. |
||
| 22 | * @return string the class name |
||
| 23 | */ |
||
| 24 | abstract public function getDividerCssClass(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | *### .getDropdownCssClass() |
||
| 28 | * |
||
| 29 | * Returns the dropdown css class. |
||
| 30 | * @return string the class name |
||
| 31 | */ |
||
| 32 | abstract public function getDropdownCssClass(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | *### .isVertical() |
||
| 36 | * |
||
| 37 | * Returns whether this is a vertical menu. |
||
| 38 | * @return boolean the result |
||
| 39 | */ |
||
| 40 | abstract public function isVertical(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | *### .renderMenu() |
||
| 44 | * |
||
| 45 | * Renders the menu items. |
||
| 46 | * |
||
| 47 | * @param array $items menu items. Each menu item will be an array with at least two elements: 'label' and 'active'. |
||
| 48 | * It may have three other optional elements: 'items', 'linkOptions' and 'itemOptions'. |
||
| 49 | */ |
||
| 50 | protected function renderMenu($items) { |
||
| 51 | |||
| 52 | $n = count($items); |
||
| 53 | |||
| 54 | if ($n > 0) { |
||
| 55 | echo CHtml::openTag('ul', $this->htmlOptions) . "\n"; |
||
| 56 | |||
| 57 | $count = 0; |
||
| 58 | foreach ($items as $item) { |
||
| 59 | $count++; |
||
| 60 | |||
| 61 | if (isset($item['divider'])) { |
||
| 62 | echo "<li class=\"{$this->getDividerCssClass()}\"></li>\n"; |
||
| 63 | } else { |
||
| 64 | $options = isset($item['itemOptions']) ? $item['itemOptions'] : array(); |
||
| 65 | $classes = array(); |
||
| 66 | |||
| 67 | if ($item['active'] && $this->activeCssClass != '') { |
||
| 68 | $classes[] = $this->activeCssClass; |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($count === 1 && $this->firstItemCssClass !== null) { |
||
| 72 | $classes[] = $this->firstItemCssClass; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($count === $n && $this->lastItemCssClass !== null) { |
||
| 76 | $classes[] = $this->lastItemCssClass; |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($this->itemCssClass !== null) { |
||
| 80 | $classes[] = $this->itemCssClass; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (isset($item['items'])) { |
||
| 84 | $classes[] = $this->getDropdownCssClass(); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (isset($item['disabled'])) { |
||
| 88 | $classes[] = 'disabled'; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!empty($classes)) { |
||
| 92 | $classes = implode(' ', $classes); |
||
| 93 | if (!empty($options['class'])) { |
||
| 94 | $options['class'] .= ' ' . $classes; |
||
| 95 | } else { |
||
| 96 | $options['class'] = $classes; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | echo CHtml::openTag('li', $options) . "\n"; |
||
| 101 | |||
| 102 | $menu = $this->renderMenuItem($item); |
||
| 103 | |||
| 104 | if (isset($this->itemTemplate) || isset($item['template'])) { |
||
| 105 | $template = isset($item['template']) ? $item['template'] : $this->itemTemplate; |
||
| 106 | echo strtr($template, array('{menu}' => $menu)); |
||
| 107 | } else { |
||
| 108 | echo $menu; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (isset($item['items']) && !empty($item['items'])) { |
||
| 112 | $dropdownOptions = array( |
||
| 113 | 'encodeLabel' => false, |
||
| 114 | 'htmlOptions' => isset($item['submenuOptions']) ? $item['submenuOptions'] |
||
| 115 | : $this->submenuHtmlOptions, |
||
| 116 | 'items' => $item['items'], |
||
| 117 | ); |
||
| 118 | $dropdownOptions['id'] = isset($dropdownOptions['htmlOptions']['id']) ? |
||
| 119 | $dropdownOptions['htmlOptions']['id'] : null; |
||
| 120 | $this->controller->widget('booster.widgets.TbDropdown', $dropdownOptions); |
||
| 121 | } |
||
| 122 | |||
| 123 | echo "</li>\n"; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | echo "</ul>\n"; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | *### .renderMenuItem() |
||
| 133 | * |
||
| 134 | * Renders the content of a menu item. |
||
| 135 | * Note that the container and the sub-menus are not rendered here. |
||
| 136 | * |
||
| 137 | * @param array $item the menu item to be rendered. Please see {@link items} on what data might be in the item. |
||
| 138 | * |
||
| 139 | * @return string the rendered item |
||
| 140 | */ |
||
| 141 | protected function renderMenuItem($item) { |
||
| 142 | |||
| 143 | if($this->linkLabelWrapper !== null) { |
||
| 144 | $item['label'] = CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']); |
||
| 145 | } |
||
| 146 | if (isset($item['icon'])) { |
||
| 147 | if (strpos($item['icon'], 'icon') === false && strpos($item['icon'], 'fa') === false) { |
||
| 148 | $item['icon'] = 'glyphicon glyphicon-' . implode(' glyphicon-', explode(' ', $item['icon'])); |
||
| 149 | $item['label'] = "<span class='" . $item['icon'] . "'></span>\r\n" . $item['label']; |
||
| 150 | } else { |
||
| 151 | $item['label'] = "<i class='" . $item['icon'] . "'></i>\r\n" . $item['label']; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if (!isset($item['linkOptions'])) { |
||
| 156 | $item['linkOptions'] = array(); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (isset($item['items']) && !empty($item['items'])) { |
||
| 160 | if (empty($item['url'])) { |
||
| 161 | $item['url'] = '#'; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (isset($item['linkOptions']['class'])) { |
||
| 165 | $item['linkOptions']['class'] .= ' dropdown-toggle'; |
||
| 166 | } else { |
||
| 167 | $item['linkOptions']['class'] = 'dropdown-toggle'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $item['linkOptions']['data-toggle'] = 'dropdown'; |
||
| 171 | $item['label'] .= ' <span class="caret"></span>'; |
||
| 172 | } |
||
| 173 | |||
| 174 | if (isset($item['url'])) { |
||
| 175 | return CHtml::link($item['label'], $item['url'], $item['linkOptions']); |
||
| 176 | } else { |
||
| 177 | return $item['label']; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | *### .normalizeItems() |
||
| 183 | * |
||
| 184 | * Normalizes the {@link items} property so that the 'active' state is properly identified for every menu item. |
||
| 185 | * |
||
| 186 | * @param array $items the items to be normalized. |
||
| 187 | * @param string $route the route of the current request. |
||
| 188 | * @param boolean $active whether there is an active child menu item. |
||
| 189 | * |
||
| 190 | * @return array the normalized menu items |
||
| 191 | */ |
||
| 192 | protected function normalizeItems($items, $route, &$active) |
||
| 224 | } |
||
| 225 |