Complex classes like Framework 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 Framework, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class Framework |
||
56 | { |
||
57 | /** |
||
58 | * @static |
||
59 | * @access protected |
||
60 | * @var Framework |
||
61 | */ |
||
62 | static protected $instance; |
||
63 | |||
64 | /** |
||
65 | * @access protected |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $rootDirectory; |
||
69 | |||
70 | /** |
||
71 | * @access protected |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $moduleDirectories = array(); |
||
75 | |||
76 | /** |
||
77 | * @access protected |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $moduleNamespaces = array(); |
||
81 | |||
82 | /** |
||
83 | * @access protected |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $modules = array(); |
||
87 | |||
88 | /** |
||
89 | * @access protected |
||
90 | * @var \Zepi\Turbo\Manager\DataSourceManager |
||
91 | */ |
||
92 | protected $dataSourceManager; |
||
93 | |||
94 | /** |
||
95 | * @access protected |
||
96 | * @var \Zepi\Turbo\Manager\ModuleManager |
||
97 | */ |
||
98 | protected $moduleManager; |
||
99 | |||
100 | /** |
||
101 | * @access protected |
||
102 | * @var \Zepi\Turbo\Manager\RuntimeManager |
||
103 | */ |
||
104 | protected $runtimeManager; |
||
105 | |||
106 | /** |
||
107 | * @access protected |
||
108 | * @var \Zepi\Turbo\Manager\RouteManager |
||
109 | */ |
||
110 | protected $routeManager; |
||
111 | |||
112 | /** |
||
113 | * @access protected |
||
114 | * @var \Zepi\Turbo\Manager\RequestManager |
||
115 | */ |
||
116 | protected $requestManager; |
||
117 | |||
118 | /** |
||
119 | * @access protected |
||
120 | * @var \Zepi\Turbo\Manager\DependencyInjectionManager |
||
121 | */ |
||
122 | protected $dependencyInjectionManager; |
||
123 | |||
124 | /** |
||
125 | * @access protected |
||
126 | * @var \Zepi\Turbo\Request\RequestAbstract |
||
127 | */ |
||
128 | protected $request; |
||
129 | |||
130 | /** |
||
131 | * @access protected |
||
132 | * @var \Zepi\Turbo\Response\Response |
||
133 | */ |
||
134 | protected $response; |
||
135 | |||
136 | /** |
||
137 | * Constructs the object |
||
138 | * |
||
139 | * @access private |
||
140 | * @param string $rootDirectory |
||
141 | */ |
||
142 | 17 | private function __construct($rootDirectory) |
|
146 | |||
147 | /** |
||
148 | * Returns a instance of the Framework |
||
149 | * |
||
150 | * @static |
||
151 | * @access public |
||
152 | * @param string $rootDirectory |
||
153 | * @return Framework |
||
154 | */ |
||
155 | 17 | public static function getFrameworkInstance($rootDirectory) |
|
164 | |||
165 | /** |
||
166 | * Resets the framework |
||
167 | * |
||
168 | * @static |
||
169 | * @access public |
||
170 | */ |
||
171 | 19 | public static function resetFramework() |
|
175 | |||
176 | /** |
||
177 | * Returns the path to the framework directory. |
||
178 | * |
||
179 | * @access public |
||
180 | * @return string |
||
181 | */ |
||
182 | 1 | public function getRootDirectory() |
|
186 | |||
187 | /** |
||
188 | * Initializes the framework and creates all needed managers. |
||
189 | * |
||
190 | * @access protected |
||
191 | */ |
||
192 | 17 | protected function initializeFramework() |
|
215 | |||
216 | /** |
||
217 | * Returns the data source manager for the framework |
||
218 | * |
||
219 | * @access public |
||
220 | * @return \Zepi\Turbo\Manager\DataSourceManager |
||
221 | */ |
||
222 | 1 | public function getDataSourceManager() |
|
226 | |||
227 | /** |
||
228 | * Returns the module manager for the framework |
||
229 | * |
||
230 | * @access public |
||
231 | * @return \Zepi\Turbo\Manager\ModuleManager |
||
232 | */ |
||
233 | 6 | public function getModuleManager() |
|
237 | |||
238 | /** |
||
239 | * Returns the runtime manager for the framework |
||
240 | * |
||
241 | * @access public |
||
242 | * @return \Zepi\Turbo\Manager\RuntimeManager |
||
243 | */ |
||
244 | 3 | public function getRuntimeManager() |
|
248 | |||
249 | /** |
||
250 | * Returns the route manager for the framework |
||
251 | * |
||
252 | * @access public |
||
253 | * @return \Zepi\Turbo\Manager\RouteManager |
||
254 | */ |
||
255 | 2 | public function getRouteManager() |
|
259 | |||
260 | /** |
||
261 | * Returns the RequestManager object |
||
262 | * |
||
263 | * @access public |
||
264 | * @return \Zepi\Turbo\Manager\RequestManager |
||
265 | */ |
||
266 | 1 | public function getRequestManager() |
|
270 | |||
271 | /** |
||
272 | * Returns the DependencyInjectionManager object |
||
273 | * |
||
274 | * @access public |
||
275 | * @return \Zepi\Turbo\Manager\DependencyInjectionManager |
||
276 | */ |
||
277 | public function getDependencyInjectionManager() |
||
281 | |||
282 | /** |
||
283 | * Returns the request object for the request |
||
284 | * |
||
285 | * @access public |
||
286 | * @return \Zepi\Turbo\Request\RequestAbstract |
||
287 | */ |
||
288 | 3 | public function getRequest() |
|
292 | |||
293 | /** |
||
294 | * Returns the response for the request |
||
295 | * |
||
296 | * @access public |
||
297 | * @return \Zepi\Turbo\Response\Response |
||
298 | */ |
||
299 | 3 | public function getResponse() |
|
303 | |||
304 | /** |
||
305 | * Registers the global autloader. |
||
306 | * |
||
307 | * @access protected |
||
308 | */ |
||
309 | 17 | protected function registerAutoloader() |
|
313 | |||
314 | /** |
||
315 | * Prepares the class name and adds a backslash in front |
||
316 | * of the class name if there isn't a bachslash. |
||
317 | * |
||
318 | * @static |
||
319 | * @access public |
||
320 | * @param string $className |
||
321 | * @return string |
||
322 | */ |
||
323 | 26 | public static function prepareClassName($className) |
|
331 | |||
332 | /** |
||
333 | * Prepares the namespace and adds on both sides of the |
||
334 | * namespace the backslashes. |
||
335 | * |
||
336 | * @static |
||
337 | * @access public |
||
338 | * @param string $namespace |
||
339 | * @return string |
||
340 | */ |
||
341 | 19 | public static function prepareNamespace($namespace) |
|
353 | |||
354 | /** |
||
355 | * Framework autoloader: This function is called from the SPL Autoloader |
||
356 | * to load the correct class. If the class isn't in the framework the |
||
357 | * function will trying to load and initialize the module. |
||
358 | * |
||
359 | * @access public |
||
360 | * @param string $className |
||
361 | * |
||
362 | * @throws \Exception Cannot find the class "$className"! |
||
363 | */ |
||
364 | 6 | public function loadClass($className) |
|
391 | |||
392 | /** |
||
393 | * Initiates the given class name |
||
394 | * |
||
395 | * @param string $className |
||
396 | * @param array $additionalParameters |
||
397 | * @param boolean $shared |
||
398 | * @return object |
||
399 | */ |
||
400 | public function initiateObject($className, $additionalParameters = array(), $shared = false) |
||
404 | |||
405 | /** |
||
406 | * Returns an instance of an object. If the module for the object |
||
407 | * isn't initialized, the function will load the module and |
||
408 | * initialize the module. |
||
409 | * |
||
410 | * @access public |
||
411 | * @param string $className |
||
412 | * @return mixed |
||
413 | * |
||
414 | * @throws \Zepi\Turbo\Exception Cannot find the module for the given class name. |
||
415 | * @throws \Zepi\Turbo\Exception Instance isn't an object! |
||
416 | */ |
||
417 | 17 | public function getInstance($className) |
|
439 | |||
440 | /** |
||
441 | * Returns the instance of a framework object |
||
442 | * |
||
443 | * @access protected |
||
444 | * @param string $className |
||
445 | * @return mixed |
||
446 | * |
||
447 | * @throws \Zepi\Turbo\Exception Class "{className}" is not defined. |
||
448 | */ |
||
449 | 17 | protected function getCoreInstance($className) |
|
479 | |||
480 | /** |
||
481 | * Executes the framework. This executes the pre and post execution events. |
||
482 | * Between these two events we call the correct request event. The |
||
483 | * routing table from the RouteManager returns the needed event name. |
||
484 | * |
||
485 | * @access public |
||
486 | */ |
||
487 | 2 | public function execute() |
|
517 | } |
||
518 |