Complex classes like BaseApp 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 BaseApp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class BaseApp |
||
26 | { |
||
27 | /** |
||
28 | * @const string |
||
29 | */ |
||
30 | const CHARSET = 'UTF-8'; |
||
31 | |||
32 | /** |
||
33 | * @var Config |
||
34 | */ |
||
35 | protected $config; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $environment; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $dicValues = []; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $dicObjects = []; |
||
51 | |||
52 | /** |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param array|Config $config |
||
56 | * @param string $environment Defaults to "production" |
||
57 | */ |
||
58 | 19 | public function __construct($config = [], $environment = 'production') |
|
70 | |||
71 | /** |
||
72 | * Initializes the application. |
||
73 | * |
||
74 | * Put initialization code that should always be run here, but (NB!) make sure |
||
75 | * no objects are actually instanced here, because then mock objects can't be |
||
76 | * injected in their place. Place object instance code in the preHandle method. |
||
77 | */ |
||
78 | 19 | protected function init() |
|
89 | |||
90 | /** |
||
91 | * @param array $phpSettings |
||
92 | * @param string $prefix |
||
93 | */ |
||
94 | 19 | protected function setPhpSettings($phpSettings, $prefix = '') |
|
105 | |||
106 | /** |
||
107 | * Register service provider. |
||
108 | * |
||
109 | * @param ServiceProviderInterface $serviceProvider |
||
110 | */ |
||
111 | 19 | public function register(ServiceProviderInterface $serviceProvider) |
|
115 | |||
116 | /** |
||
117 | * Pre handle method meant to be overridden in descendant classes (optional). |
||
118 | * |
||
119 | * This method is called before an request is handled. Object instance code should be |
||
120 | * place here and not in init() (more info about this at init()). |
||
121 | * |
||
122 | * @param Request $request |
||
123 | * @return Response|null |
||
124 | */ |
||
125 | 3 | protected function preHandle(Request $request) |
|
128 | |||
129 | /** |
||
130 | * Post route method meant to be overridden in descendant classes (optional). |
||
131 | * This method is called before an request is dispatched but after it's routed. This means that we know |
||
132 | * it's a valid route and have access to the route attributes at this stage. |
||
133 | * |
||
134 | * @param Request $request |
||
135 | * @return Response|null |
||
136 | */ |
||
137 | 1 | protected function postRoute(Request $request) |
|
140 | |||
141 | /** |
||
142 | * Handles an http request and returns a response. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * @return Response |
||
146 | */ |
||
147 | 4 | public function handle(Request $request) |
|
171 | |||
172 | /** |
||
173 | * Returns a response for no route / resource not found. |
||
174 | * |
||
175 | * @param Request $request |
||
176 | * @return Response |
||
177 | */ |
||
178 | 1 | protected function getNoRouteResponse(Request $request) |
|
182 | |||
183 | /** |
||
184 | * Post handle method meant to be overridden in descendant classes (optional). |
||
185 | * This method is called after an request has been handled but before |
||
186 | * the response is returned from the handle method. |
||
187 | * |
||
188 | * @param Request $request |
||
189 | */ |
||
190 | 2 | protected function postHandle(Request $request) |
|
193 | |||
194 | /** |
||
195 | * Set a DIC value. |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @param mixed $value Wrap objects in a closure for lazy loading |
||
199 | * @return BaseApp |
||
200 | */ |
||
201 | 19 | public function set($key, $value) |
|
208 | |||
209 | /** |
||
210 | * Check if a DIC value/object exists. |
||
211 | * |
||
212 | * @param string $key |
||
213 | * @return bool |
||
214 | */ |
||
215 | 19 | public function has($key) |
|
219 | |||
220 | /** |
||
221 | * @param string $key |
||
222 | * @return bool |
||
223 | */ |
||
224 | 2 | public function hasInstance($key) |
|
228 | |||
229 | /** |
||
230 | * Get the shared instance of a DIC object, or a DIC value if it's not an object. |
||
231 | * |
||
232 | * @param string $key |
||
233 | * @return mixed |
||
234 | */ |
||
235 | 19 | public function get($key) |
|
255 | |||
256 | /** |
||
257 | * Get new instance of a DIC object. |
||
258 | * |
||
259 | * @param string $key |
||
260 | * @return mixed |
||
261 | */ |
||
262 | 3 | public function getNew($key) |
|
272 | |||
273 | /** |
||
274 | * Destroy a DIC object instance. |
||
275 | * |
||
276 | * Will force a new object to be created on next call. |
||
277 | * |
||
278 | * @param string $key |
||
279 | */ |
||
280 | 1 | public function destroyInstance($key) |
|
284 | |||
285 | /** |
||
286 | * Destroy all DIC object instances. |
||
287 | * |
||
288 | * Will force new objects to be created on next call. |
||
289 | */ |
||
290 | 1 | public function destroyAllInstances() |
|
298 | |||
299 | /** |
||
300 | * Magic method to get or set DIC values. |
||
301 | * |
||
302 | * @param string $name |
||
303 | * @param array $arguments |
||
304 | * @return mixed |
||
305 | */ |
||
306 | 5 | public function __call($name, $arguments) |
|
330 | |||
331 | /** |
||
332 | * @return Config |
||
333 | */ |
||
334 | 19 | public function getConfig() |
|
338 | |||
339 | /** |
||
340 | * @return bool |
||
341 | */ |
||
342 | 19 | public function isCli() |
|
346 | |||
347 | /** |
||
348 | * @return Session |
||
349 | */ |
||
350 | 1 | public function getSession() |
|
354 | |||
355 | /** |
||
356 | * @return Router |
||
357 | */ |
||
358 | 3 | public function getRouter() |
|
362 | |||
363 | /** |
||
364 | * @return Request|null |
||
365 | */ |
||
366 | 2 | public function getRequest() |
|
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | 1 | public function getEnvironment() |
|
378 | } |
||
379 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.