| Conditions | 4 |
| Paths | 2 |
| Total Lines | 51 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 77 | public function searchThroughAdminPages($term) |
||
| 78 | { |
||
| 79 | // Can we generate this in a different way, or move these out of this method |
||
| 80 | $adminPages = collect([ |
||
| 81 | [ 'label' => 'Dashboard', 'url' => route('chief.back.dashboard'), 'permission' => null, 'tags' => ['home'], ], |
||
| 82 | [ 'label' => 'Menu', 'url' => route('chief.back.menus.index'), 'permission' => 'update-page', 'tags' => ['navigatie'], ], |
||
| 83 | [ 'label' => 'Media', 'url' => route('chief.mediagallery.index'), 'permission' => 'update-page', 'tags' => ['mediagalerij', 'mediabibliotheek', 'assets'], ], |
||
| 84 | [ 'label' => 'Teksten', 'url' => route('squanto.index'), 'permission' => 'view-squanto', 'tags' => ['squanto', 'mediagalerij', 'mediabibliotheek', 'assets'], ], |
||
| 85 | [ 'label' => 'Sitemap', 'url' => route('chief.back.sitemap.show'), 'permission' => null, 'tags' => [], ], |
||
| 86 | [ 'label' => 'Admins', 'url' => route('chief.back.users.index'), 'permission' => 'view-user', 'tags' => [], ], |
||
| 87 | [ 'label' => 'Rechten', 'url' => route('chief.back.roles.index'), 'permission' => 'view-role', 'tags' => ['roles'], ], |
||
| 88 | [ 'label' => 'Settings', 'url' => route('chief.back.settings.edit'), 'permission' => 'update-setting', 'tags' => ['instellingen'], ], |
||
| 89 | [ 'label' => 'Audit', 'url' => route('chief.back.audit.index'), 'permission' => 'view-audit', 'tags' => [], ], |
||
| 90 | [ 'label' => chiefAdmin()->firstname, 'url' => route('chief.back.you.edit'), 'permission' => 'update-you', 'tags' => ['account'], ], |
||
| 91 | [ 'label' => 'Logout', 'url' => route('chief.back.logout'), 'permission' => null, 'tags' => [], ], |
||
| 92 | ]); |
||
| 93 | |||
| 94 | $results = $adminPages->filter(function ($adminPage) use ($term) { |
||
| 95 | // TODO: check if current user has necessary permissions to view page |
||
| 96 | // if(! chiefAdmin()->hasPermissionTo($adminPage['permission'])) { |
||
| 97 | // return false; |
||
| 98 | // } |
||
| 99 | |||
| 100 | // Check if label contains search term |
||
| 101 | if (Str::contains(Str::lower($adminPage['label']), $term)) { |
||
| 102 | return true; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Check if any of tags contain search term |
||
| 106 | if (collect($adminPage['tags'])->contains(function ($tag) use ($term) { |
||
| 107 | return Str::contains(Str::lower($tag), $term); |
||
| 108 | })) { |
||
| 109 | return true; |
||
| 110 | }; |
||
| 111 | |||
| 112 | return false; |
||
| 113 | })->map(function ($adminPage) { |
||
| 114 | return [ |
||
| 115 | 'label' => $adminPage['label'], |
||
| 116 | 'url' => $adminPage['url'], |
||
| 117 | ]; |
||
| 118 | })->toArray(); |
||
| 119 | |||
| 120 | if (count($results) === 0) { |
||
| 121 | return []; |
||
| 122 | } |
||
| 123 | |||
| 124 | return [ |
||
| 125 | [ |
||
| 126 | 'label' => 'Admin', |
||
| 127 | 'results' => $results, |
||
| 128 | ], |
||
| 149 |