Complex classes like ClassyHierarchy 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 ClassyHierarchy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class ClassyHierarchy { |
||
|
|
|||
| 8 | |||
| 9 | /** |
||
| 10 | * Stores current request for multiple use |
||
| 11 | * |
||
| 12 | * @var string/null |
||
| 13 | */ |
||
| 14 | protected static $current_request = null; |
||
| 15 | |||
| 16 | |||
| 17 | /** |
||
| 18 | * Protected function to get current request type |
||
| 19 | * |
||
| 20 | * @return string |
||
| 21 | */ |
||
| 22 | protected static function check_request() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns current request type |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public static function get_current_request() { |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns file's absolute path |
||
| 86 | * |
||
| 87 | * @param string $type view/scope |
||
| 88 | * @param string $view path to view ex: "post/archive" |
||
| 89 | * @return string full fule path |
||
| 90 | */ |
||
| 91 | public static function get_file_path($type = 'view', $view) { |
||
| 92 | |||
| 93 | if ($type == 'view') { |
||
| 94 | |||
| 95 | $folder = ClassyView::$folder; |
||
| 96 | |||
| 97 | return THEME_PATH . $folder . '/' . $view . '.blade.php'; |
||
| 98 | |||
| 99 | } elseif ($type == 'scope') { |
||
| 100 | |||
| 101 | $folder = ClassyScope::$folder; |
||
| 102 | |||
| 103 | return THEME_PATH . $folder . '/' . $view . '.php'; |
||
| 104 | |||
| 105 | } |
||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Checks if view exists |
||
| 112 | * |
||
| 113 | * @param string $type view/scope |
||
| 114 | * @param string $view in blade path format, ex: layout/header |
||
| 115 | * @return boolean true/false |
||
| 116 | */ |
||
| 117 | public static function file_exists($type = 'view', $file) { |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns view name for render, based on type of request |
||
| 130 | * |
||
| 131 | * @param string $type view/scope |
||
| 132 | * @param string $type |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public static function get_available_file($type = 'view', $page) { |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns list of filenames to check, based on type of request |
||
| 156 | * |
||
| 157 | * @param string $type |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | private static function get_request_hierarchy_list($type) { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Checks if this is classy custom template |
||
| 369 | * |
||
| 370 | * @return boolean |
||
| 371 | */ |
||
| 372 | public static function is_classy_template() { |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns classy template name or boolean if this is not classy template |
||
| 380 | * |
||
| 381 | * @return mixed |
||
| 382 | */ |
||
| 383 | public static function get_classy_template() { |
||
| 394 | |||
| 395 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.