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 |
||
| 31 | class Plugin extends CakePlugin |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Plugin bootstrap file. |
||
| 36 | */ |
||
| 37 | const FILE_BOOTSTRAP = 'bootstrap.php'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Plugin routes file. |
||
| 41 | */ |
||
| 42 | const FILE_ROUTES = 'routes.php'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The plugin manifest file name. |
||
| 46 | */ |
||
| 47 | const PLUGIN_MANIFEST = 'plugin.manifest.php'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Hold plugin data. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected static $_data = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Holds a list of all plugin events from manifest file. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected static $_eventList = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Manifest callback list. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected static $_manifestEvents = [ |
||
| 69 | 'Controller.initialize', |
||
| 70 | 'Controller.beforeRender', |
||
| 71 | 'Controller.beforeFilter', |
||
| 72 | 'Controller.beforeRedirect', |
||
| 73 | 'Controller.afterFilter', |
||
| 74 | 'View.initialize', |
||
| 75 | 'View.beforeRenderFile', |
||
| 76 | 'View.afterRenderFile', |
||
| 77 | 'View.beforeRender', |
||
| 78 | 'View.afterRender', |
||
| 79 | 'View.beforeLayout', |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get plugin manifest data. |
||
| 84 | * |
||
| 85 | * @param string $plugin |
||
| 86 | * @param null|string $key |
||
| 87 | * @return Data |
||
| 88 | */ |
||
| 89 | public static function getData($plugin, $key = null) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get plugin locale path. |
||
| 109 | * |
||
| 110 | * @param string $plugin |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public static function getLocalePath($plugin) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get absolute plugin manifest file path. |
||
| 120 | * |
||
| 121 | * @param string $plugin |
||
| 122 | * @return null|string |
||
| 123 | */ |
||
| 124 | public static function getManifestPath($plugin) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Check plugin need migration. |
||
| 135 | * |
||
| 136 | * @param string $plugin |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public static function hasMigration($plugin) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Load the plugin. |
||
| 156 | * |
||
| 157 | * @param array|string $plugin |
||
| 158 | * @param array $config |
||
| 159 | */ |
||
| 160 | public static function load($plugin, array $config = []) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Load list plugin. |
||
| 175 | * |
||
| 176 | * @param array $plugins |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public static function loadList(array $plugins) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Call plugin manifest callbacks. |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | public static function manifestEvent() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Unload the plugin. |
||
| 212 | * |
||
| 213 | * @param null|string $plugin |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public static function unload($plugin = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Registration plugin manifest callbacks. |
||
| 234 | * |
||
| 235 | * @param string $plugin |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | protected static function _addManifestCallback($plugin) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Check plugin data. |
||
| 250 | * |
||
| 251 | * @param string $plugin |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | protected static function _checkData($plugin) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Find plugin dir in registered paths. |
||
| 261 | * |
||
| 262 | * @param string $name |
||
| 263 | * @return null|string |
||
| 264 | */ |
||
| 265 | protected static function _findPlugin($name) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get plugin configuration for load plugin. |
||
| 288 | * |
||
| 289 | * @param string $path |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | protected static function _getConfigForLoad($path) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get current plugin data. |
||
| 313 | * |
||
| 314 | * @param array $data |
||
| 315 | * @param null|string $key |
||
| 316 | * @return Data |
||
| 317 | */ |
||
| 318 | protected static function _getPluginData(array $data, $key = null) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Check manifest param on callable. |
||
| 329 | * |
||
| 330 | * @param string $name |
||
| 331 | * @param string $plugin |
||
| 332 | * @param mixed $callback |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | protected static function _isCallablePluginData($name, $plugin, $callback) |
||
| 346 | } |
||
| 347 |