Passed
Pull Request — master (#863)
by Diego
02:53
created

MenuItemHelper   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 39
c 3
b 0
f 0
dl 0
loc 179
ccs 52
cts 52
cp 1
rs 9.1199
wmc 41

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isLink() 0 4 3
A isHeader() 0 3 2
A isLegacySearch() 0 5 3
A isValidSidebarItem() 0 6 4
A isValidNavbarItem() 0 5 3
A isSidebarItem() 0 6 4
A isNavbarRightItem() 0 5 3
A isSearchItem() 0 4 2
A isNavbarCustomSearch() 0 5 3
A isNavbarLeftItem() 0 5 3
A isSubmenu() 0 5 3
A isAllowed() 0 5 3
A isNavbarSearch() 0 4 2
A isNavbarUserItem() 0 5 3

How to fix   Complexity   

Complex Class

Complex classes like MenuItemHelper 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.

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 MenuItemHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Helpers;
4
5
/**
6
 * TODO: On the future, all menu items should have a type property. We can use
7
 * the type property to easy distinguish the item type and avoid guessing it by
8
 * they properties.
9
 */
10
class MenuItemHelper
11
{
12
    /**
13
     * Check if a menu item is a header.
14
     *
15
     * @param mixed $item
16
     * @return bool
17
     */
18 52
    public static function isHeader($item)
19
    {
20 52
        return is_string($item) || isset($item['header']);
21
    }
22
23
    /**
24
     * Check if a menu item is a link.
25
     *
26
     * @param mixed $item
27
     * @return bool
28
     */
29 4
    public static function isLink($item)
30
    {
31 4
        return isset($item['text']) &&
32 4
               (isset($item['url']) || isset($item['route']));
33
    }
34
35
    /**
36
     * Check if a menu item is a legacy search bar.
37
     *
38
     * @param mixed $item
39
     * @return bool
40
     */
41 54
    public static function isLegacySearch($item)
42
    {
43 54
        return isset($item['text']) &&
44 54
               isset($item['search']) &&
45 54
               $item['search'];
46
    }
47
48
    /**
49
     * Check if a menu item is a navbar custom search bar.
50
     *
51
     * @param mixed $item
52
     * @return bool
53
     */
54 52
    public static function isNavbarCustomSearch($item)
55
    {
56 52
        return isset($item['text']) &&
57 52
               isset($item['type']) &&
58 52
               $item['type'] === 'navbar-search';
59
    }
60
61
    /**
62
     * Check if a menu item is a navbar search item (legacy or new).
63
     *
64
     * @param mixed $item
65
     * @return bool
66
     */
67 52
    public static function isNavbarSearch($item)
68
    {
69 52
        return self::isLegacySearch($item) ||
70 52
               self::isNavbarCustomSearch($item);
71
    }
72
73
    /**
74
     * Check if a menu item is a submenu.
75
     *
76
     * @param mixed $item
77
     * @return bool
78
     */
79 56
    public static function isSubmenu($item)
80
    {
81 56
        return isset($item['text']) &&
82 56
               isset($item['submenu']) &&
83 56
               is_array($item['submenu']);
84
    }
85
86
    /**
87
     * Check if a menu item is allowed to be shown.
88
     *
89
     * @param mixed $item
90
     * @return bool
91
     */
92 56
    public static function isAllowed($item)
93
    {
94 56
        $isAllowed = ! (isset($item['restricted']) && $item['restricted']);
95
96 56
        return $item && $isAllowed;
97
    }
98
99
    /**
100
     * Check if a menu item is a search item (for sidebar or navbar).
101
     *
102
     * @param mixed $item
103
     * @return bool
104
     */
105 50
    public static function isSearchItem($item)
106
    {
107 50
        return self::isLegacySearch($item) ||
108 50
               self::isNavbarSearch($item);
109
    }
110
111
    /**
112
     * Check if a menu item is valid for the sidebar section.
113
     *
114
     * @param mixed $item
115
     * @return bool
116
     */
117 18
    public static function isValidSidebarItem($item)
118
    {
119 18
        return self::isHeader($item) ||
120 18
               self::isLegacySearch($item) ||
121 18
               self::isSubmenu($item) ||
122 18
               self::isLink($item);
123
    }
124
125
    /**
126
     * Check if a menu item is valid for the navbar section.
127
     *
128
     * @param mixed $item
129
     * @return bool
130
     */
131 20
    public static function isValidNavbarItem($item)
132
    {
133 20
        return self::isNavbarSearch($item) ||
134 20
               self::isSubmenu($item) ||
135 20
               self::isLink($item);
136
    }
137
138
    /**
139
     * Check if a menu item belongs to the left section of the navbar.
140
     *
141
     * @param mixed $item
142
     * @return bool
143
     */
144 18
    public static function isNavbarLeftItem($item)
145
    {
146 18
        return self::isValidNavbarItem($item) &&
147 18
               isset($item['topnav']) &&
148 18
               $item['topnav'];
149
    }
150
151
    /**
152
     * Check if a menu item belongs to the right section of the navbar.
153
     *
154
     * @param mixed $item
155
     * @return bool
156
     */
157 19
    public static function isNavbarRightItem($item)
158
    {
159 19
        return self::isValidNavbarItem($item) &&
160 19
               isset($item['topnav_right']) &&
161 19
               $item['topnav_right'];
162
    }
163
164
    /**
165
     * Check if a menu item belongs to the user menu section of the navbar.
166
     *
167
     * @param mixed $item
168
     * @return bool
169
     */
170 19
    public static function isNavbarUserItem($item)
171
    {
172 19
        return self::isValidNavbarItem($item) &&
173 19
               isset($item['topnav_user']) &&
174 19
               $item['topnav_user'];
175
    }
176
177
    /**
178
     * Check if a menu item belongs to the sidebar.
179
     *
180
     * @param mixed $item
181
     * @return bool
182
     */
183 18
    public static function isSidebarItem($item)
184
    {
185 18
        return self::isValidSidebarItem($item) &&
186 18
               ! self::isNavbarLeftItem($item) &&
187 18
               ! self::isNavbarRightItem($item) &&
188 18
               ! self::isNavbarUserItem($item);
189
    }
190
}
191