Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Helper |
||
| 18 | { |
||
| 19 | private static $_userRoutes = []; |
||
| 20 | private static $_defaultRoutes; |
||
| 21 | private static $_routes; |
||
| 22 | |||
| 23 | public static function getRegisteredRoutes() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get assigned routes by default roles |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | protected static function getDefaultRoutes() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get assigned routes of user. |
||
| 71 | * @param integer $userId |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public static function getRoutesByUser($userId) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Check access route for user. |
||
| 101 | * @param string|array $route |
||
| 102 | * @param integer|User $user |
||
| 103 | * @return boolean |
||
| 104 | */ |
||
| 105 | public static function checkRoute($route, $params = [], $user = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Normalize route |
||
| 146 | * @param string $route Plain route string |
||
| 147 | * @param boolean|array $advanced Array containing the advanced configuration. Defaults to false. |
||
| 148 | * @return string Normalized route string |
||
| 149 | */ |
||
| 150 | protected static function normalizeRoute($route, $advanced = false) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Filter menu items |
||
| 172 | * @param array $items |
||
| 173 | * @param integer|User $user |
||
| 174 | */ |
||
| 175 | public static function filter($items, $user = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Filter menu recursive |
||
| 185 | * @param array $items |
||
| 186 | * @param integer|User $user |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | protected static function filterRecursive($items, $user) |
||
| 190 | { |
||
| 191 | $result = []; |
||
| 192 | foreach ($items as $i => $item) { |
||
| 193 | $url = ArrayHelper::getValue($item, 'url', '#'); |
||
| 194 | $allow = is_array($url) ? static::checkRoute($url[0], array_slice($url, 1), $user) : true; |
||
| 195 | |||
| 196 | if (isset($item['items']) && is_array($item['items'])) { |
||
| 197 | $subItems = self::filterRecursive($item['items'], $user); |
||
| 198 | if (count($subItems)) { |
||
| 199 | $allow = true; |
||
| 200 | } |
||
| 201 | $item['items'] = $subItems; |
||
| 202 | } |
||
| 203 | if ($allow && !($url == '#' && empty($item['items']))) { |
||
| 204 | $result[$i] = $item; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | return $result; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Filter action column button. Use with [[yii\grid\GridView]] |
||
| 212 | * ```php |
||
| 213 | * 'columns' => [ |
||
| 214 | * ... |
||
| 215 | * [ |
||
| 216 | * 'class' => 'yii\grid\ActionColumn', |
||
| 217 | * 'template' => Helper::filterActionColumn(['view','update','activate']) |
||
| 218 | * ] |
||
| 219 | * ], |
||
| 220 | * ``` |
||
| 221 | * @param array|string $buttons |
||
| 222 | * @param integer|User $user |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public static function filterActionColumn($buttons = [], $user = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Use to invalidate cache. |
||
| 243 | */ |
||
| 244 | public static function invalidate() |
||
| 250 | } |
||
| 251 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: