Complex classes like PluginController 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 PluginController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class PluginController |
||
12 | { |
||
13 | |||
14 | protected $list_bytype = array(); |
||
15 | protected $tmp_plugins = array(); |
||
16 | protected $plugin_cascade = array('default' => array(), 'local' => array(), 'protected' => array()); |
||
17 | protected $last_local_config_file = ''; |
||
18 | |||
19 | /** |
||
20 | * Populates the master list of plugins |
||
21 | */ |
||
22 | public function __construct() |
||
27 | |||
28 | /** |
||
29 | * Returns a list of available plugins of given type |
||
30 | * |
||
31 | * @param $type string, plugin_type name; |
||
32 | * the type of plugin to return, |
||
33 | * use empty string for all types |
||
34 | * @param $all bool; |
||
35 | * false to only return enabled plugins, |
||
36 | * true to return both enabled and disabled plugins |
||
37 | * |
||
38 | * @return array of |
||
39 | * - plugin names when $type = '' |
||
40 | * - or plugin component names when a $type is given |
||
41 | * |
||
42 | * @author Andreas Gohr <[email protected]> |
||
43 | */ |
||
44 | public function getList($type = '', $all = false) |
||
63 | |||
64 | /** |
||
65 | * Loads the given plugin and creates an object of it |
||
66 | * |
||
67 | * @author Andreas Gohr <[email protected]> |
||
68 | * |
||
69 | * @param $type string type of plugin to load |
||
70 | * @param $name string name of the plugin to load |
||
71 | * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance |
||
72 | * @param $disabled bool true to load even disabled plugins |
||
73 | * @return PluginInterface|null the plugin object or null on failure |
||
74 | */ |
||
75 | public function load($type, $name, $new = false, $disabled = false) |
||
129 | |||
130 | /** |
||
131 | * Whether plugin is disabled |
||
132 | * |
||
133 | * @param string $plugin name of plugin |
||
134 | * @return bool true disabled, false enabled |
||
135 | */ |
||
136 | public function isdisabled($plugin) |
||
140 | |||
141 | /** |
||
142 | * Disable the plugin |
||
143 | * |
||
144 | * @param string $plugin name of plugin |
||
145 | * @return bool true saving succeed, false saving failed |
||
146 | */ |
||
147 | public function disable($plugin) |
||
153 | |||
154 | /** |
||
155 | * Enable the plugin |
||
156 | * |
||
157 | * @param string $plugin name of plugin |
||
158 | * @return bool true saving succeed, false saving failed |
||
159 | */ |
||
160 | public function enable($plugin) |
||
166 | |||
167 | /** |
||
168 | * Returns directory name of plugin |
||
169 | * |
||
170 | * @param string $plugin name of plugin |
||
171 | * @return string name of directory |
||
172 | */ |
||
173 | public function get_directory($plugin) |
||
177 | |||
178 | /** |
||
179 | * Returns cascade of the config files |
||
180 | * |
||
181 | * @return array with arrays of plugin configs |
||
182 | */ |
||
183 | public function getCascade() |
||
187 | |||
188 | protected function _populateMasterList() |
||
213 | |||
214 | /** |
||
215 | * Includes the plugin config $files |
||
216 | * and returns the entries of the $plugins array set in these files |
||
217 | * |
||
218 | * @param array $files list of files to include, latter overrides previous |
||
219 | * @return array with entries of the $plugins arrays of the included files |
||
220 | */ |
||
221 | protected function checkRequire($files) |
||
231 | |||
232 | /** |
||
233 | * Save the current list of plugins |
||
234 | * |
||
235 | * @param bool $forceSave ; |
||
236 | * false to save only when config changed |
||
237 | * true to always save |
||
238 | * @return bool true saving succeed, false saving failed |
||
239 | */ |
||
240 | protected function saveList($forceSave = false) |
||
270 | |||
271 | /** |
||
272 | * Rebuild the set of local plugins |
||
273 | * |
||
274 | * @return array array of plugins to be saved in end($config_cascade['plugins']['local']) |
||
275 | */ |
||
276 | protected function rebuildLocal() |
||
292 | |||
293 | /** |
||
294 | * Build the list of plugins and cascade |
||
295 | * |
||
296 | */ |
||
297 | protected function loadConfig() |
||
320 | |||
321 | /** |
||
322 | * Returns a list of available plugin components of given type |
||
323 | * |
||
324 | * @param string $type plugin_type name; the type of plugin to return, |
||
325 | * @param bool $enabled true to return enabled plugins, |
||
326 | * false to return disabled plugins |
||
327 | * @return array of plugin components of requested type |
||
328 | */ |
||
329 | protected function _getListByType($type, $enabled) |
||
361 | |||
362 | /** |
||
363 | * Split name in a plugin name and a component name |
||
364 | * |
||
365 | * @param string $name |
||
366 | * @return array with |
||
367 | * - plugin name |
||
368 | * - and component name when available, otherwise empty string |
||
369 | */ |
||
370 | protected function _splitName($name) |
||
378 | |||
379 | /** |
||
380 | * Returns inverse boolean value of the input |
||
381 | * |
||
382 | * @param mixed $input |
||
383 | * @return bool inversed boolean value of input |
||
384 | */ |
||
385 | protected function negate($input) |
||
389 | } |
||
390 |