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 |
||
10 | class PluginController |
||
11 | { |
||
12 | /** @var PluginController */ |
||
13 | protected static $instance; |
||
14 | |||
15 | /** The different types of plugins DokuWiki supports */ |
||
16 | const PLUGIN_TYPES = array('auth', 'admin','syntax','action','renderer', 'helper','remote'); |
||
17 | |||
18 | protected $list_bytype = array(); |
||
19 | protected $tmp_plugins = array(); |
||
20 | protected $plugin_cascade = array('default' => array(), 'local' => array(), 'protected' => array()); |
||
21 | protected $last_local_config_file = ''; |
||
22 | |||
23 | /** |
||
24 | * Populates the master list of plugins |
||
25 | * @param bool $usedGetInstance temporary to find deprecated uses |
||
26 | * @deprecated 2018-06-16 This constructor will be made private |
||
27 | */ |
||
28 | public function __construct($usedGetInstance=false) |
||
36 | |||
37 | /** |
||
38 | * Get the singleton instance of the Plugin Controller |
||
39 | * |
||
40 | * @param bool $init force a reload of the controller |
||
41 | * @return PluginController |
||
42 | */ |
||
43 | public static function getInstance($init=false) { |
||
50 | |||
51 | /** |
||
52 | * Returns a list of available plugins of given type |
||
53 | * |
||
54 | * @param $type string, plugin_type name; |
||
55 | * the type of plugin to return, |
||
56 | * use empty string for all types |
||
57 | * @param $all bool; |
||
58 | * false to only return enabled plugins, |
||
59 | * true to return both enabled and disabled plugins |
||
60 | * |
||
61 | * @return array of |
||
62 | * - plugin names when $type = '' |
||
63 | * - or plugin component names when a $type is given |
||
64 | * |
||
65 | * @author Andreas Gohr <[email protected]> |
||
66 | */ |
||
67 | public function getList($type = '', $all = false) |
||
86 | |||
87 | /** |
||
88 | * Loads the given plugin and creates an object of it |
||
89 | * |
||
90 | * @author Andreas Gohr <[email protected]> |
||
91 | * |
||
92 | * @param $type string type of plugin to load |
||
93 | * @param $name string name of the plugin to load |
||
94 | * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance |
||
95 | * @param $disabled bool true to load even disabled plugins |
||
96 | * @return PluginInterface|null the plugin object or null on failure |
||
97 | */ |
||
98 | public function load($type, $name, $new = false, $disabled = false) |
||
151 | |||
152 | /** |
||
153 | * Whether plugin is disabled |
||
154 | * |
||
155 | * @param string $plugin name of plugin |
||
156 | * @return bool true disabled, false enabled |
||
157 | * @deprecated in favor of the more sensible isEnabled where the return value matches the enabled state |
||
158 | */ |
||
159 | public function isDisabled($plugin) |
||
164 | |||
165 | /** |
||
166 | * Check whether plugin is disabled |
||
167 | * |
||
168 | * @param string $plugin name of plugin |
||
169 | * @return bool true enabled, false disabled |
||
170 | */ |
||
171 | public function isEnabled($plugin) |
||
175 | |||
176 | /** |
||
177 | * Disable the plugin |
||
178 | * |
||
179 | * @param string $plugin name of plugin |
||
180 | * @return bool true saving succeed, false saving failed |
||
181 | */ |
||
182 | public function disable($plugin) |
||
188 | |||
189 | /** |
||
190 | * Enable the plugin |
||
191 | * |
||
192 | * @param string $plugin name of plugin |
||
193 | * @return bool true saving succeed, false saving failed |
||
194 | */ |
||
195 | public function enable($plugin) |
||
201 | |||
202 | /** |
||
203 | * Returns cascade of the config files |
||
204 | * |
||
205 | * @return array with arrays of plugin configs |
||
206 | */ |
||
207 | public function getCascade() |
||
211 | |||
212 | protected function _populateMasterList() |
||
237 | |||
238 | /** |
||
239 | * Includes the plugin config $files |
||
240 | * and returns the entries of the $plugins array set in these files |
||
241 | * |
||
242 | * @param array $files list of files to include, latter overrides previous |
||
243 | * @return array with entries of the $plugins arrays of the included files |
||
244 | */ |
||
245 | protected function checkRequire($files) |
||
255 | |||
256 | /** |
||
257 | * Save the current list of plugins |
||
258 | * |
||
259 | * @param bool $forceSave ; |
||
260 | * false to save only when config changed |
||
261 | * true to always save |
||
262 | * @return bool true saving succeed, false saving failed |
||
263 | */ |
||
264 | protected function saveList($forceSave = false) |
||
294 | |||
295 | /** |
||
296 | * Rebuild the set of local plugins |
||
297 | * |
||
298 | * @return array array of plugins to be saved in end($config_cascade['plugins']['local']) |
||
299 | */ |
||
300 | protected function rebuildLocal() |
||
316 | |||
317 | /** |
||
318 | * Build the list of plugins and cascade |
||
319 | * |
||
320 | */ |
||
321 | protected function loadConfig() |
||
344 | |||
345 | /** |
||
346 | * Returns a list of available plugin components of given type |
||
347 | * |
||
348 | * @param string $type plugin_type name; the type of plugin to return, |
||
349 | * @param bool $enabled true to return enabled plugins, |
||
350 | * false to return disabled plugins |
||
351 | * @return array of plugin components of requested type |
||
352 | */ |
||
353 | protected function _getListByType($type, $enabled) |
||
384 | |||
385 | /** |
||
386 | * Split name in a plugin name and a component name |
||
387 | * |
||
388 | * @param string $name |
||
389 | * @return array with |
||
390 | * - plugin name |
||
391 | * - and component name when available, otherwise empty string |
||
392 | */ |
||
393 | protected function _splitName($name) |
||
401 | |||
402 | /** |
||
403 | * Returns inverse boolean value of the input |
||
404 | * |
||
405 | * @param mixed $input |
||
406 | * @return bool inversed boolean value of input |
||
407 | */ |
||
408 | protected function negate($input) |
||
412 | } |
||
413 |