Complex classes like Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Plugin extends CakePlugin |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Plugin bootstrap file. |
||
| 35 | */ |
||
| 36 | const FILE_BOOTSTRAP = 'bootstrap.php'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Plugin routes file. |
||
| 40 | */ |
||
| 41 | const FILE_ROUTES = 'routes.php'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The plugin manifest file name. |
||
| 45 | */ |
||
| 46 | const PLUGIN_MANIFEST = 'plugin.manifest.php'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Hold plugin data. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected static $_data = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Holds a list of all plugin events from manifest file. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected static $_eventList = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Manifest callback list. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected static $_manifestEvents = [ |
||
| 68 | 'Controller.initialize', |
||
| 69 | 'Controller.beforeRender', |
||
| 70 | 'Controller.beforeFilter', |
||
| 71 | 'Controller.beforeRedirect', |
||
| 72 | 'Controller.afterFilter', |
||
| 73 | 'View.initialize', |
||
| 74 | 'View.beforeRenderFile', |
||
| 75 | 'View.afterRenderFile', |
||
| 76 | 'View.beforeRender', |
||
| 77 | 'View.afterRender', |
||
| 78 | 'View.beforeLayout', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get plugin manifest data. |
||
| 83 | * |
||
| 84 | * @param string $plugin |
||
| 85 | * @param null|string $key |
||
| 86 | * @return Data |
||
| 87 | */ |
||
| 88 | public static function getData($plugin, $key = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get plugin locale path. |
||
| 108 | * |
||
| 109 | * @param string $plugin |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public static function getLocalePath($plugin) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get absolute plugin manifest file path. |
||
| 119 | * |
||
| 120 | * @param string $plugin |
||
| 121 | * @return null|string |
||
| 122 | */ |
||
| 123 | public static function getManifestPath($plugin) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check manifest event. |
||
| 134 | * |
||
| 135 | * @param string $name Plugin name. |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public static function hasManifestEvent($name) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Load the plugin. |
||
| 145 | * |
||
| 146 | * @param array|string $plugin |
||
| 147 | * @param array $config |
||
| 148 | */ |
||
| 149 | public static function load($plugin, array $config = []) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Load list plugin. |
||
| 164 | * |
||
| 165 | * @param array $plugins |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | public static function loadList(array $plugins) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Call plugin manifest callbacks. |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public static function manifestEvent() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Unload the plugin. |
||
| 201 | * |
||
| 202 | * @param null|string $plugin |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public static function unload($plugin = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Registration plugin manifest callbacks. |
||
| 223 | * |
||
| 224 | * @param string $plugin |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | protected static function _addManifestCallback($plugin) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Check plugin data. |
||
| 239 | * |
||
| 240 | * @param string $plugin |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | protected static function _checkData($plugin) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Find plugin dir in registered paths. |
||
| 250 | * |
||
| 251 | * @param string $name |
||
| 252 | * @return null|string |
||
| 253 | */ |
||
| 254 | protected static function _findPlugin($name) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get plugin configuration for load plugin. |
||
| 277 | * |
||
| 278 | * @param string $path |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected static function _getConfigForLoad($path) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get current plugin data. |
||
| 302 | * |
||
| 303 | * @param array $data |
||
| 304 | * @param null|string $key |
||
| 305 | * @return Data |
||
| 306 | */ |
||
| 307 | protected static function _getPluginData(array $data, $key = null) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check manifest param on callable. |
||
| 318 | * |
||
| 319 | * @param string $name |
||
| 320 | * @param string $plugin |
||
| 321 | * @param mixed $callback |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | protected static function _isCallablePluginData($name, $plugin, $callback) |
||
| 335 | } |
||
| 336 |