Complex classes like Menu 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 Menu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class Menu extends Widget |
||
51 | { |
||
52 | /** |
||
53 | * @var array list of menu items. Each menu item should be an array of the following structure: |
||
54 | * |
||
55 | * - label: string, optional, specifies the menu item label. When [[encodeLabels]] is true, the label |
||
56 | * will be HTML-encoded. If the label is not specified, an empty string will be used. |
||
57 | * - encode: boolean, optional, whether this item`s label should be HTML-encoded. This param will override |
||
58 | * global [[encodeLabels]] param. |
||
59 | * - url: string or array, optional, specifies the URL of the menu item. It will be processed by [[Url::to]]. |
||
60 | * When this is set, the actual menu item content will be generated using [[linkTemplate]]; |
||
61 | * otherwise, [[labelTemplate]] will be used. |
||
62 | * - visible: boolean, optional, whether this menu item is visible. Defaults to true. |
||
63 | * - items: array, optional, specifies the sub-menu items. Its format is the same as the parent items. |
||
64 | * - active: boolean or Closure, optional, whether this menu item is in active state (currently selected). |
||
65 | * When using a closure, its signature should be `function ($item, $hasActiveChild, $isItemActive, $widget)`. |
||
66 | * Closure must return `true` if item should be marked as `active`, otherwise - `false`. |
||
67 | * If a menu item is active, its CSS class will be appended with [[activeCssClass]]. |
||
68 | * If this option is not set, the menu item will be set active automatically when the current request |
||
69 | * is triggered by `url`. For more details, please refer to [[isItemActive()]]. |
||
70 | * - template: string, optional, the template used to render the content of this menu item. |
||
71 | * The token `{url}` will be replaced by the URL associated with this menu item, |
||
72 | * and the token `{label}` will be replaced by the label of the menu item. |
||
73 | * If this option is not set, [[linkTemplate]] or [[labelTemplate]] will be used instead. |
||
74 | * - submenuTemplate: string, optional, the template used to render the list of sub-menus. |
||
75 | * The token `{items}` will be replaced with the rendered sub-menu items. |
||
76 | * If this option is not set, [[submenuTemplate]] will be used instead. |
||
77 | * - options: array, optional, the HTML attributes for the menu container tag. |
||
78 | */ |
||
79 | public $items = []; |
||
80 | /** |
||
81 | * @var array list of HTML attributes shared by all menu [[items]]. If any individual menu item |
||
82 | * specifies its `options`, it will be merged with this property before being used to generate the HTML |
||
83 | * attributes for the menu item tag. The following special options are recognized: |
||
84 | * |
||
85 | * - tag: string, defaults to "li", the tag name of the item container tags. |
||
86 | * Set to false to disable container tag. |
||
87 | * See also [[\yii\helpers\Html::tag()]]. |
||
88 | * |
||
89 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
90 | */ |
||
91 | public $itemOptions = []; |
||
92 | /** |
||
93 | * @var string the template used to render the body of a menu which is a link. |
||
94 | * In this template, the token `{url}` will be replaced with the corresponding link URL; |
||
95 | * while `{label}` will be replaced with the link text. |
||
96 | * This property will be overridden by the `template` option set in individual menu items via [[items]]. |
||
97 | */ |
||
98 | public $linkTemplate = '<a href="{url}">{label}</a>'; |
||
99 | /** |
||
100 | * @var string the template used to render the body of a menu which is NOT a link. |
||
101 | * In this template, the token `{label}` will be replaced with the label of the menu item. |
||
102 | * This property will be overridden by the `template` option set in individual menu items via [[items]]. |
||
103 | */ |
||
104 | public $labelTemplate = '{label}'; |
||
105 | /** |
||
106 | * @var string the template used to render a list of sub-menus. |
||
107 | * In this template, the token `{items}` will be replaced with the rendered sub-menu items. |
||
108 | */ |
||
109 | public $submenuTemplate = "\n<ul>\n{items}\n</ul>\n"; |
||
110 | /** |
||
111 | * @var bool whether the labels for menu items should be HTML-encoded. |
||
112 | */ |
||
113 | public $encodeLabels = true; |
||
114 | /** |
||
115 | * @var string the CSS class to be appended to the active menu item. |
||
116 | */ |
||
117 | public $activeCssClass = 'active'; |
||
118 | /** |
||
119 | * @var bool whether to automatically activate items according to whether their route setting |
||
120 | * matches the currently requested route. |
||
121 | * @see isItemActive() |
||
122 | */ |
||
123 | public $activateItems = true; |
||
124 | /** |
||
125 | * @var bool whether to activate parent menu items when one of the corresponding child menu items is active. |
||
126 | * The activated parent menu items will also have its CSS classes appended with [[activeCssClass]]. |
||
127 | */ |
||
128 | public $activateParents = false; |
||
129 | /** |
||
130 | * @var bool whether to hide empty menu items. An empty menu item is one whose `url` option is not |
||
131 | * set and which has no visible child menu items. |
||
132 | */ |
||
133 | public $hideEmptyItems = true; |
||
134 | /** |
||
135 | * @var array the HTML attributes for the menu's container tag. The following special options are recognized: |
||
136 | * |
||
137 | * - tag: string, defaults to "ul", the tag name of the item container tags. Set to false to disable container tag. |
||
138 | * See also [[\yii\helpers\Html::tag()]]. |
||
139 | * |
||
140 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
141 | */ |
||
142 | public $options = []; |
||
143 | /** |
||
144 | * @var string the CSS class that will be assigned to the first item in the main menu or each submenu. |
||
145 | * Defaults to null, meaning no such CSS class will be assigned. |
||
146 | */ |
||
147 | public $firstItemCssClass; |
||
148 | /** |
||
149 | * @var string the CSS class that will be assigned to the last item in the main menu or each submenu. |
||
150 | * Defaults to null, meaning no such CSS class will be assigned. |
||
151 | */ |
||
152 | public $lastItemCssClass; |
||
153 | /** |
||
154 | * @var string the route used to determine if a menu item is active or not. |
||
155 | * If not set, it will use the route of the current request. |
||
156 | * @see params |
||
157 | * @see isItemActive() |
||
158 | */ |
||
159 | public $route; |
||
160 | /** |
||
161 | * @var array the parameters used to determine if a menu item is active or not. |
||
162 | * If not set, it will use `$_GET`. |
||
163 | * @see route |
||
164 | * @see isItemActive() |
||
165 | */ |
||
166 | public $params; |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Renders the menu. |
||
171 | */ |
||
172 | 5 | public function run() |
|
188 | |||
189 | /** |
||
190 | * Recursively renders the menu items (without the container tag). |
||
191 | * @param array $items the menu items to be rendered recursively |
||
192 | * @return string the rendering result |
||
193 | */ |
||
194 | 5 | protected function renderItems($items) |
|
231 | |||
232 | /** |
||
233 | * Renders the content of a menu item. |
||
234 | * Note that the container and the sub-menus are not rendered here. |
||
235 | * @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item. |
||
236 | * @return string the rendering result |
||
237 | */ |
||
238 | 5 | protected function renderItem($item) |
|
255 | |||
256 | /** |
||
257 | * Normalizes the [[items]] property to remove invisible items and activate certain items. |
||
258 | * @param array $items the items to be normalized. |
||
259 | * @param bool $active whether there is an active child menu item. |
||
260 | * @return array the normalized menu items |
||
261 | */ |
||
262 | 5 | protected function normalizeItems($items, &$active) |
|
300 | |||
301 | /** |
||
302 | * Checks whether a menu item is active. |
||
303 | * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item. |
||
304 | * When the `url` option of a menu item is specified in terms of an array, its first element is treated |
||
305 | * as the route for the item and the rest of the elements are the associated parameters. |
||
306 | * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item |
||
307 | * be considered active. |
||
308 | * @param array $item the menu item to be checked |
||
309 | * @return bool whether the menu item is active |
||
310 | */ |
||
311 | 5 | protected function isItemActive($item) |
|
337 | } |
||
338 |