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