Complex classes like MageClass 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 MageClass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MageClass implements Mageable |
||
12 | { |
||
13 | public function __construct() |
||
17 | |||
18 | /** |
||
19 | * @return string |
||
20 | */ |
||
21 | public function getVersion() |
||
25 | |||
26 | /** |
||
27 | * @return array |
||
28 | */ |
||
29 | public function getVersionInfo() |
||
33 | |||
34 | /** |
||
35 | * @return string |
||
36 | */ |
||
37 | public function getEdition() |
||
41 | |||
42 | /** |
||
43 | * @param $key |
||
44 | * @param $value |
||
45 | * @param bool $graceful |
||
46 | */ |
||
47 | public function register($key, $value, $graceful = false) |
||
51 | |||
52 | /** |
||
53 | * @param string $key |
||
54 | * @return mixed |
||
55 | */ |
||
56 | public function registry($key) |
||
60 | |||
61 | /** |
||
62 | * @param string $key |
||
63 | */ |
||
64 | public function unregister($key) |
||
68 | |||
69 | /** |
||
70 | * @param string $type |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getBaseDir($type = 'base') |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Parameter type can be: |
||
81 | * - etc |
||
82 | * - controllers |
||
83 | * - sql |
||
84 | * - data |
||
85 | * - locale |
||
86 | * @param $type |
||
87 | * @param $moduleName |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getModuleDir($type, $moduleName) |
||
94 | |||
95 | /** |
||
96 | * @param $path |
||
97 | * @param null $store |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function getStoreConfig($path, $store = null) |
||
104 | |||
105 | /** |
||
106 | * @param $path |
||
107 | * @param null $store |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function getStoreConfigFlag($path, $store = null) |
||
114 | |||
115 | /** |
||
116 | * @return \Mage_Core_Model_Config |
||
117 | */ |
||
118 | public function getConfig() |
||
122 | |||
123 | /** |
||
124 | * @return \Varien_Event_Collection |
||
125 | */ |
||
126 | public function getEvents() |
||
130 | |||
131 | /** |
||
132 | * @param $name |
||
133 | * @return \Mage_Core_Helper_Abstract |
||
134 | */ |
||
135 | public function helper($name) |
||
139 | |||
140 | /** |
||
141 | * @param string $modelClass |
||
142 | * @param array $arguments |
||
143 | * @return false|\Mage_Core_Model_Abstract |
||
144 | */ |
||
145 | public function getModel($modelClass = '', $arguments = array()) |
||
149 | |||
150 | /** |
||
151 | * Set all my data to defaults |
||
152 | * |
||
153 | */ |
||
154 | public function reset() |
||
158 | |||
159 | /** |
||
160 | * Set application root absolute path |
||
161 | * |
||
162 | * @param string $appRoot |
||
163 | * @throws Mage_Core_Exception |
||
164 | */ |
||
165 | public function setRoot($appRoot = '') |
||
169 | |||
170 | /** |
||
171 | * Retrieve application root absolute path |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getRoot() |
||
179 | |||
180 | /** |
||
181 | * Varien Objects Cache |
||
182 | * |
||
183 | * @param string $key optional, if specified will load this key |
||
184 | * @return Varien_Object_Cache |
||
185 | */ |
||
186 | public function objects($key = null) |
||
190 | |||
191 | /** |
||
192 | * Get base URL path by type |
||
193 | * |
||
194 | * @param string $type |
||
195 | * @param null|bool $secure |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getBaseUrl($type = "link", $secure = null) |
||
202 | |||
203 | /** |
||
204 | * Generate url by route and parameters |
||
205 | * @param string $route |
||
206 | * @param array $params |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getUrl($route = '', $params = array()) |
||
213 | |||
214 | /** |
||
215 | * Get design package singleton |
||
216 | * |
||
217 | * @return Mage_Core_Model_Design_Package |
||
218 | */ |
||
219 | public function getDesign() |
||
223 | |||
224 | /** |
||
225 | * Add observer to even object |
||
226 | * |
||
227 | * @param string $eventName |
||
228 | * @param callback $callback |
||
229 | * @param array $data |
||
230 | * @param string $observerName |
||
231 | * @return \Varien_Event_Collection |
||
232 | */ |
||
233 | public function addObserver($eventName, $callback, $data = array(), $observerName = '', $observerClass = '') |
||
237 | |||
238 | /** |
||
239 | * Dispatch event |
||
240 | * |
||
241 | * Calls all observer callbacks registered for this event |
||
242 | * and multiple observers matching event name pattern |
||
243 | * |
||
244 | * @param string $name |
||
245 | * @param array $data |
||
246 | * @return Mage_Core_Model_App |
||
247 | */ |
||
248 | public function dispatchEvent($name, array $data = array()) |
||
252 | |||
253 | /** |
||
254 | * Retrieve model object singleton |
||
255 | * |
||
256 | * @param string $modelClass |
||
257 | * @param array $arguments |
||
258 | * @return Mage_Core_Model_Abstract |
||
259 | */ |
||
260 | public function getSingleton($modelClass = '', array $arguments = array()) |
||
264 | |||
265 | /** |
||
266 | * Retrieve object of resource model |
||
267 | * |
||
268 | * @param string $modelClass |
||
269 | * @param array $arguments |
||
270 | * @return Object |
||
271 | */ |
||
272 | public function getResourceModel($modelClass, $arguments = array()) |
||
276 | |||
277 | /** |
||
278 | * Retrieve Controller instance by ClassName |
||
279 | * |
||
280 | * @param string $class |
||
281 | * @param Mage_Core_Controller_Request_Http $request |
||
282 | * @param Mage_Core_Controller_Response_Http $response |
||
283 | * @param array $invokeArgs |
||
284 | * @return Mage_Core_Controller_Front_Action |
||
285 | */ |
||
286 | public function getControllerInstance($class, $request, $response, array $invokeArgs = array()) |
||
290 | |||
291 | /** |
||
292 | * Retrieve resource vodel object singleton |
||
293 | * |
||
294 | * @param string $modelClass |
||
295 | * @param array $arguments |
||
296 | * @return object |
||
297 | */ |
||
298 | public function getResourceSingleton($modelClass = '', array $arguments = array()) |
||
302 | |||
303 | /** |
||
304 | * Retrieve resource helper object |
||
305 | * |
||
306 | * @param string $moduleName |
||
307 | * @return Mage_Core_Model_Resource_Helper_Abstract |
||
308 | */ |
||
309 | public function getResourceHelper($moduleName) |
||
313 | |||
314 | /** |
||
315 | * Return new exception by module to be thrown |
||
316 | * |
||
317 | * @param string $module |
||
318 | * @param string $message |
||
319 | * @param integer $code |
||
320 | * @return Mage_Core_Exception |
||
321 | */ |
||
322 | public function exception($module = 'Mage_Core', $message = '', $code = 0) |
||
326 | |||
327 | /** |
||
328 | * Throw Exception |
||
329 | * |
||
330 | * @param string $message |
||
331 | * @param string $messageStorage |
||
332 | * @throws Mage_Core_Exception |
||
333 | */ |
||
334 | public function throwException($message, $messageStorage = null) |
||
338 | |||
339 | /** |
||
340 | * Get initialized application object. |
||
341 | * |
||
342 | * @param string $code |
||
343 | * @param string $type |
||
344 | * @param string|array $options |
||
345 | * @return Mage_Core_Model_App |
||
346 | */ |
||
347 | public function app($code = '', $type = 'store', $options = array()) |
||
351 | |||
352 | /** |
||
353 | * @param string $code |
||
354 | * @param string $type |
||
355 | * @param array $options |
||
356 | * @param string|array $modules |
||
357 | */ |
||
358 | public function init($code = '', $type = 'store', $options = array(), $modules = array()) |
||
362 | |||
363 | /** |
||
364 | * Front end main entry point |
||
365 | * |
||
366 | * @param string $code |
||
367 | * @param string $type |
||
368 | * @param string|array $options |
||
369 | */ |
||
370 | public function run($code = '', $type = 'store', $options = array()) |
||
374 | |||
375 | /** |
||
376 | * Retrieve application installation flag |
||
377 | * |
||
378 | * @param string|array $options |
||
379 | * @return bool |
||
380 | */ |
||
381 | public function isInstalled($options = array()) |
||
385 | |||
386 | /** |
||
387 | * log facility |
||
388 | * |
||
389 | * @param string $message |
||
390 | * @param integer $level |
||
391 | * @param string $file |
||
392 | * @param bool $forceLog |
||
393 | */ |
||
394 | public function log($message, $level = null, $file = '', $forceLog = false) |
||
398 | |||
399 | /** |
||
400 | * Write exception to log |
||
401 | * |
||
402 | * @param Exception $e |
||
403 | */ |
||
404 | public function logException(Exception $e) |
||
408 | |||
409 | /** |
||
410 | * Set enabled developer mode |
||
411 | * |
||
412 | * @param bool $mode |
||
413 | * @return bool |
||
414 | */ |
||
415 | public function setIsDeveloperMode($mode) |
||
419 | |||
420 | /** |
||
421 | * Retrieve enabled developer mode |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | public function getIsDeveloperMode() |
||
429 | |||
430 | /** |
||
431 | * Display exception |
||
432 | * |
||
433 | * @param Exception $e |
||
434 | */ |
||
435 | public function printException(Exception $e, $extra = '') |
||
439 | |||
440 | /** |
||
441 | * Define system folder directory url by virtue of running script directory name |
||
442 | * Try to find requested folder by shifting to domain root directory |
||
443 | * |
||
444 | * @param string $folder |
||
445 | * @param boolean $exitIfNot |
||
446 | * @return string |
||
447 | */ |
||
448 | public function getScriptSystemUrl($folder, $exitIfNot = false) |
||
452 | |||
453 | /** |
||
454 | * Set is downloader flag |
||
455 | * |
||
456 | * @param bool $flag |
||
457 | */ |
||
458 | public function setIsDownloader($flag = true) |
||
462 | } |
||
463 |