| Conditions | 12 |
| Paths | 1 |
| Total Lines | 81 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 39 | function menu($menus) |
||
|
|
|||
| 40 | { |
||
| 41 | // Retrieve current user (for permissions) |
||
| 42 | $user = Auth::user(); |
||
| 43 | |||
| 44 | $templates = [ |
||
| 45 | "menu" => ' |
||
| 46 | <ul class="sidebar-menu" data-widget="tree"> |
||
| 47 | %s |
||
| 48 | </ul> |
||
| 49 | ', |
||
| 50 | |||
| 51 | "menu_row" => ' |
||
| 52 | <li class="%s"> |
||
| 53 | %s |
||
| 54 | </li> |
||
| 55 | ', // class, link . submenu |
||
| 56 | |||
| 57 | "menu_caret" => ' |
||
| 58 | <span class="pull-right-container"> |
||
| 59 | <i class="fa fa-angle-left pull-right"></i> |
||
| 60 | </span>', |
||
| 61 | |||
| 62 | "menu_subrow" => ' |
||
| 63 | <ul class="treeview-menu"> |
||
| 64 | %s |
||
| 65 | </ul> |
||
| 66 | ', |
||
| 67 | |||
| 68 | "menu_link" => ' |
||
| 69 | <a href="%s"><i class="%s text-red"></i><span>%s</span>%s</a> |
||
| 70 | ' // url, icon, title |
||
| 71 | ]; |
||
| 72 | |||
| 73 | $traverse = function ($rows) use (&$traverse, $templates, $user) { |
||
| 74 | $menuString = ""; |
||
| 75 | $hasActive = false; |
||
| 76 | foreach ($rows as $menu) { |
||
| 77 | if (!empty($menu->permission) and !$user->hasPermission($menu->permission)) { |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | $hasActive = false; |
||
| 82 | $submenu = ""; |
||
| 83 | $authorized = true; |
||
| 84 | |||
| 85 | if ($menu->children->count() > 0) { |
||
| 86 | list($submenuString, $hasActive) = $traverse($menu->children); |
||
| 87 | $submenu = ""; |
||
| 88 | if (!empty($submenuString)) { |
||
| 89 | $submenu = sprintf($templates['menu_subrow'], $submenuString); |
||
| 90 | } else { |
||
| 91 | $authorized = false; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $menu_caret = (!empty($submenu) ? $templates['menu_caret'] : ''); |
||
| 96 | $link = sprintf($templates['menu_link'], |
||
| 97 | (!empty($menu->route) ? url($menu->route) : '#'), |
||
| 98 | (!empty($menu->icon) ? $menu->icon : 'fa fa-circle-o'), |
||
| 99 | //trans('back-project::base.dashboard') |
||
| 100 | \Lang::has('back-project::menu.' . $menu->title) ? __('back-project::menu.' . $menu->title) : $menu->title, |
||
| 101 | $menu_caret |
||
| 102 | ); |
||
| 103 | $class = (!empty($submenu) ? 'treeview' : ''); |
||
| 104 | $current_url = \Route::current()->uri(); |
||
| 105 | |||
| 106 | if ($authorized) { |
||
| 107 | $menuString .= sprintf($templates['menu_row'], $class, $link . $submenu); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | return [ |
||
| 112 | $menuString, |
||
| 113 | $hasActive |
||
| 114 | ]; |
||
| 115 | }; |
||
| 116 | |||
| 117 | list($menu, $hasActive) = $traverse($menus); |
||
| 118 | return sprintf($templates['menu'], $menu); |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.