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 |
||
| 31 | class Menu |
||
| 32 | { |
||
| 33 | /** @var HttpReq */ |
||
| 34 | protected $req; |
||
| 35 | |||
| 36 | /** @var array Will hold the created $context */ |
||
| 37 | protected $menuContext = []; |
||
| 38 | |||
| 39 | /** @var string Used for profile menu for own / any */ |
||
| 40 | protected $permissionSet; |
||
| 41 | |||
| 42 | /** @var bool If we found the menu item selected */ |
||
| 43 | protected $foundSection = false; |
||
| 44 | |||
| 45 | /** @var string Current area */ |
||
| 46 | protected $currentArea = ''; |
||
| 47 | |||
| 48 | /** @var null|string The current subaction of the system */ |
||
| 49 | protected $currentSubaction = ''; |
||
| 50 | |||
| 51 | /** @var array Will hold the selected menu data that is returned to the caller */ |
||
| 52 | private $includeData = []; |
||
| 53 | |||
| 54 | /** @var int Unique menu number */ |
||
| 55 | private $maxMenuId = 0; |
||
| 56 | |||
| 57 | /** @var MenuOptions Holds menu options */ |
||
| 58 | private $menuOptions; |
||
| 59 | |||
| 60 | /** @var array Holds menu definition structure set by addSection */ |
||
| 61 | private $menuData = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Initial processing for the menu |
||
| 65 | * |
||
| 66 | * @param HttpReq|null $req |
||
| 67 | */ |
||
| 68 | 41 | public function __construct(HttpReq $req = null) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Create a menu |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | * @throws Elk_Exception |
||
| 96 | */ |
||
| 97 | 40 | public function prepareMenu(): array |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Prepares tabs for the template. |
||
| 129 | * |
||
| 130 | * This should be called after the area is dispatched, because areas |
||
| 131 | * are usually in their own file. Those files, once dispatched to, hold |
||
| 132 | * some data for the tabs which must be specially combined with subaction |
||
| 133 | * data for everything to work properly. |
||
| 134 | * |
||
| 135 | * Seems complicated, yes. |
||
| 136 | */ |
||
| 137 | 1 | public function prepareTabData(): void |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Process the menuData array passed to the class |
||
| 159 | * |
||
| 160 | * - Only processes areas that are enabled and that the user has permissions |
||
| 161 | */ |
||
| 162 | 40 | protected function processMenuData(): void |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Determines if the user has the permissions to access the section/area |
||
| 180 | * |
||
| 181 | * If said item did not provide any permission to check, fullly |
||
| 182 | * unfettered access is assumed. |
||
| 183 | * |
||
| 184 | * The profile areas are a bit different in that each permission is |
||
| 185 | * divided into two sets: "own" for owner and "any" for everyone else. |
||
| 186 | * |
||
| 187 | * @param MenuItem $obj area or section being checked |
||
| 188 | * |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | 39 | private function checkPermissions(MenuItem $obj): bool |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Checks if the area has a label or not |
||
| 209 | * |
||
| 210 | * @param string $areaId |
||
| 211 | * @param MenuArea $area |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | 38 | private function areaHasLabel(string $areaId, MenuArea $area): bool |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Main processing for creating the menu items for all sections |
||
| 224 | * |
||
| 225 | * @param string $sectionId |
||
| 226 | * @param MenuSection $section |
||
| 227 | */ |
||
| 228 | 38 | protected function processSectionAreas(string $sectionId, MenuSection $section): void |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Checks the menu item to see if it is the currently selected one |
||
| 263 | * |
||
| 264 | * @param string $sectionId |
||
| 265 | * @param string $areaId |
||
| 266 | * @param MenuArea $area |
||
| 267 | */ |
||
| 268 | 38 | private function checkCurrentSection(string $sectionId, string $areaId, MenuArea $area): void |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $sectionId |
||
| 282 | * @param string $areaId |
||
| 283 | * @param MenuArea $area |
||
| 284 | */ |
||
| 285 | 38 | private function setFirstAreaCurrent(string $sectionId, string $areaId, MenuArea $area): void |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Simply sets the current area |
||
| 296 | * |
||
| 297 | * @param string $sectionId |
||
| 298 | * @param string $areaId |
||
| 299 | * @param MenuArea $area |
||
| 300 | */ |
||
| 301 | 38 | private function setAreaCurrent(string $sectionId, string $areaId, MenuArea $area): void |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param MenuItem $obj |
||
| 311 | * @param integer $idx |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 38 | private function parseCounter(MenuItem $obj, int $idx): string |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Sets the various section ID items |
||
| 333 | * |
||
| 334 | * What it does: |
||
| 335 | * - If the ID is not set, sets it and sets the section title |
||
| 336 | * - Sets the section title |
||
| 337 | * |
||
| 338 | * @param string $sectionId |
||
| 339 | * @param MenuSection $section |
||
| 340 | */ |
||
| 341 | 38 | private function setSectionContext(string $sectionId, MenuSection $section): void |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Sets the various area items |
||
| 354 | * |
||
| 355 | * What it does: |
||
| 356 | * - If the ID is not set, sets it and sets the area title |
||
| 357 | * - Sets the area title |
||
| 358 | * |
||
| 359 | * @param string $sectionId |
||
| 360 | * @param string $areaId |
||
| 361 | * @param MenuArea $area |
||
| 362 | */ |
||
| 363 | 38 | private function setAreaContext(string $sectionId, string $areaId, MenuArea $area): void |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Set the URL for the menu item |
||
| 374 | * |
||
| 375 | * @param string $sectionId |
||
| 376 | * @param string $areaId |
||
| 377 | * @param MenuArea $area |
||
| 378 | */ |
||
| 379 | 38 | private function setAreaUrl(string $sectionId, string $areaId, MenuArea $area): void |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Set the menu icon |
||
| 390 | * |
||
| 391 | * @param string $sectionId |
||
| 392 | * @param string $areaId |
||
| 393 | * @param MenuArea $area |
||
| 394 | */ |
||
| 395 | 38 | private function setAreaIcon(string $sectionId, string $areaId, MenuArea $area): void |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Processes all of the subsections for a menu item |
||
| 420 | * |
||
| 421 | * @param string $sectionId |
||
| 422 | * @param string $areaId |
||
| 423 | * @param MenuArea $area |
||
| 424 | */ |
||
| 425 | 38 | protected function processAreaSubsections(string $sectionId, string $areaId, MenuArea $area): void |
|
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $sectionId |
||
| 454 | * @param string $areaId |
||
| 455 | * @param string $subId |
||
| 456 | * @param MenuSubsection $sub |
||
| 457 | */ |
||
| 458 | 38 | private function setSubsSectionUrl(string $sectionId, string $areaId, string $subId, MenuSubsection $sub): void |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Set the current subsection |
||
| 469 | * |
||
| 470 | * @param string $subId |
||
| 471 | * @param MenuSubsection $sub |
||
| 472 | */ |
||
| 473 | 6 | private function setCurrentSubSection(string $subId, MenuSubsection $sub): void |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Ensures that the current subsection is set. |
||
| 489 | * |
||
| 490 | * @param string $areaId |
||
| 491 | * @param array $subSections |
||
| 492 | */ |
||
| 493 | 38 | private function setDefaultSubSection(string $areaId, array $subSections): void |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Checks and updates base and section urls |
||
| 503 | */ |
||
| 504 | 40 | private function setActiveButtons(): void |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Add the base menu options for this menu |
||
| 523 | * |
||
| 524 | * @param array $menuOptions an array of options that can be used to override some default |
||
| 525 | * behaviours. See MenuOptions for details. |
||
| 526 | */ |
||
| 527 | 41 | public function addOptions(array $menuOptions): void |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @param string $id |
||
| 534 | * @param MenuSection $section |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | 41 | public function addSection(string $id, MenuSection $section): Menu |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Finalizes items so the computed menu can be used |
||
| 547 | * |
||
| 548 | * What it does: |
||
| 549 | * - Sets the menu layer in the template stack |
||
| 550 | * - Loads context with the computed menu context |
||
| 551 | * - Sets current subaction and current max menu id |
||
| 552 | */ |
||
| 553 | 38 | public function setContext(): void |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Delete a menu. |
||
| 573 | * |
||
| 574 | * Checks to see if this menu been loaded into context |
||
| 575 | * and, if so, resets $context['max_menu_id'] back to the |
||
| 576 | * last known menu (if any) and remove the template layer |
||
| 577 | * if there aren't any other known menus. |
||
| 578 | */ |
||
| 579 | 21 | public function destroy(): void |
|
| 601 | } |
||
| 602 |