Complex classes like themes 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 themes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | trait themes { |
||
| 20 | /** |
||
| 21 | * @param \cs\Request $Request |
||
| 22 | * |
||
| 23 | * @return mixed |
||
| 24 | * |
||
| 25 | * @throws ExitException |
||
| 26 | */ |
||
| 27 | static function admin_themes_get ($Request) { |
||
| 28 | if ($Request->route_path(3) == 'update_dependencies') { |
||
| 29 | /** |
||
| 30 | * Get dependencies for theme during update |
||
| 31 | */ |
||
| 32 | return static::get_update_dependencies_for_theme($Request->route_path[2]); |
||
| 33 | } elseif ($Request->route_path(2) == 'current') { |
||
| 34 | /** |
||
| 35 | * Get current theme |
||
| 36 | */ |
||
| 37 | return static::get_current_theme(); |
||
| 38 | } else { |
||
| 39 | /** |
||
| 40 | * Get array of themes in extended form |
||
| 41 | */ |
||
| 42 | return static::get_themes_list(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | /** |
||
| 46 | * @param string $theme |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | * |
||
| 50 | * @throws ExitException |
||
| 51 | */ |
||
| 52 | protected static function get_update_dependencies_for_theme ($theme) { |
||
| 53 | $themes = get_files_list(THEMES, false, 'd'); |
||
| 54 | if (!in_array($theme, $themes, true)) { |
||
| 55 | throw new ExitException(404); |
||
| 56 | } |
||
| 57 | $tmp_location = TEMP.'/System/admin/'.Session::instance()->get_id().'.phar'; |
||
| 58 | $tmp_dir = "phar://$tmp_location"; |
||
| 59 | if ( |
||
| 60 | !file_exists(THEMES."/$theme/meta.json") || |
||
| 61 | !file_exists("$tmp_dir/meta.json") |
||
| 62 | ) { |
||
| 63 | throw new ExitException(400); |
||
| 64 | } |
||
| 65 | $existing_meta = file_get_json(THEMES."/$theme/meta.json"); |
||
| 66 | $new_meta = file_get_json("$tmp_dir/meta.json"); |
||
| 67 | if ( |
||
| 68 | $existing_meta['package'] !== $new_meta['package'] || |
||
| 69 | $existing_meta['category'] !== $new_meta['category'] |
||
| 70 | ) { |
||
| 71 | throw new ExitException(Language::instance()->this_is_not_theme_installer_file, 400); |
||
| 72 | } |
||
| 73 | $dependencies = []; |
||
| 74 | if (version_compare($new_meta['version'], $existing_meta['version'], '<')) { |
||
| 75 | $dependencies['update_older'] = [ |
||
| 76 | 'from' => $existing_meta['version'], |
||
| 77 | 'to' => $new_meta['version'] |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | return $dependencies; |
||
| 81 | } |
||
| 82 | /** |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | protected static function get_current_theme () { |
||
| 86 | return Config::instance()->core['theme']; |
||
| 87 | } |
||
| 88 | /** |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | protected static function get_themes_list () { |
||
| 92 | $themes = get_files_list(THEMES, false, 'd'); |
||
| 93 | asort($themes); |
||
| 94 | $themes_list = []; |
||
| 95 | foreach ($themes as $theme_name) { |
||
| 96 | $theme = [ |
||
| 97 | 'name' => $theme_name |
||
| 98 | ]; |
||
| 99 | /** |
||
| 100 | * Check if readme available |
||
| 101 | */ |
||
| 102 | static::check_theme_feature_availability($theme, 'readme'); |
||
| 103 | /** |
||
| 104 | * Check if license available |
||
| 105 | */ |
||
| 106 | static::check_theme_feature_availability($theme, 'license'); |
||
| 107 | if (file_exists(THEMES."/$theme_name/meta.json")) { |
||
| 108 | $theme['meta'] = file_get_json(THEMES."/$theme_name/meta.json"); |
||
| 109 | } |
||
| 110 | $themes_list[] = $theme; |
||
| 111 | } |
||
| 112 | return $themes_list; |
||
| 113 | } |
||
| 114 | /** |
||
| 115 | * @param array $theme |
||
| 116 | * @param string $feature |
||
| 117 | */ |
||
| 118 | protected static function check_theme_feature_availability (&$theme, $feature) { |
||
| 130 | /** |
||
| 131 | * @param \cs\Request $Request |
||
| 132 | * |
||
| 133 | * @throws ExitException |
||
| 134 | */ |
||
| 135 | static function admin_themes_put ($Request) { |
||
| 136 | if ($Request->route_path(2) == 'current') { |
||
| 137 | $theme = $Request->data('theme'); |
||
| 138 | if (!$theme) { |
||
| 139 | throw new ExitException(400); |
||
| 140 | } |
||
| 141 | /** |
||
| 142 | * Set current theme |
||
| 143 | */ |
||
| 144 | static::set_current_theme($theme); |
||
| 145 | } else { |
||
| 146 | throw new ExitException(400); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | /** |
||
| 150 | * Provides next events: |
||
| 151 | * admin/System/components/themes/current/before |
||
| 152 | * ['name' => theme_name] |
||
| 153 | * |
||
| 154 | * admin/System/components/themes/current/after |
||
| 155 | * ['name' => theme_name] |
||
| 156 | * |
||
| 157 | * @param string $theme |
||
| 158 | * |
||
| 159 | * @throws ExitException |
||
| 160 | */ |
||
| 161 | protected static function set_current_theme ($theme) { |
||
| 190 | /** |
||
| 191 | * Extract uploaded theme |
||
| 192 | * |
||
| 193 | * @throws ExitException |
||
| 194 | */ |
||
| 195 | static function admin_themes_extract () { |
||
| 213 | /** |
||
| 214 | * Update theme |
||
| 215 | * |
||
| 216 | * Provides next events: |
||
| 217 | * admin/System/components/themes/update/before |
||
| 218 | * ['name' => theme_name] |
||
| 219 | * |
||
| 220 | * admin/System/components/themes/update/after |
||
| 221 | * ['name' => theme_name] |
||
| 222 | * |
||
| 223 | * @param \cs\Request $Request |
||
| 224 | * |
||
| 225 | * @throws ExitException |
||
| 226 | */ |
||
| 227 | static function admin_themes_update ($Request) { |
||
| 274 | /** |
||
| 275 | * Delete theme completely |
||
| 276 | * |
||
| 277 | * @param \cs\Request $Request |
||
| 278 | * |
||
| 279 | * @throws ExitException |
||
| 280 | */ |
||
| 281 | static function admin_themes_delete ($Request) { |
||
| 296 | } |
||
| 297 |