Complex classes like ApplicationPackage 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 ApplicationPackage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ApplicationPackage implements RegistrationInterface |
||
33 | { |
||
34 | /** @var array $config */ |
||
35 | private $config; |
||
36 | |||
37 | /** @var Router $router */ |
||
38 | private $router; |
||
39 | |||
40 | /** |
||
41 | * ApplicationPackage constructor. |
||
42 | * @param array $config |
||
43 | * @param Router $router |
||
44 | 10 | */ |
|
45 | public function __construct(array $config, Router $router) |
||
50 | |||
51 | /** |
||
52 | * @param Container $c |
||
53 | * @throws \Bone\Exception |
||
54 | * @throws \Exception |
||
55 | 10 | */ |
|
56 | public function addToContainer(Container $c) |
||
73 | |||
74 | /** |
||
75 | * @param Container $c |
||
76 | 10 | */ |
|
77 | private function setConfigArray(Container $c) |
||
83 | |||
84 | /** |
||
85 | * @param Container $c |
||
86 | 10 | */ |
|
87 | private function setupViewEngine(Container $c) |
||
92 | |||
93 | /** |
||
94 | * @param Container $c |
||
95 | 10 | */ |
|
96 | private function setupRouter(Container $c) |
||
101 | |||
102 | /** |
||
103 | * @param Container $c |
||
104 | 10 | */ |
|
105 | private function setupPackages(Container $c) |
||
120 | |||
121 | /** |
||
122 | * @param string $packageName |
||
123 | * @param Container $c |
||
124 | 10 | */ |
|
125 | private function registerPackage(string $packageName, Container $c): void |
||
136 | |||
137 | /** |
||
138 | 10 | * @param RegistrationInterface $package |
|
139 | */ |
||
140 | 10 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
154 | |||
155 | /** |
||
156 | 10 | * @param RegistrationInterface $package |
|
157 | */ |
||
158 | 10 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
|
165 | |||
166 | /** |
||
167 | 10 | * @param RegistrationInterface $package |
|
168 | */ |
||
169 | 10 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
175 | |||
176 | /** |
||
177 | 10 | * @param RegistrationInterface $package |
|
178 | */ |
||
179 | 10 | private function registerViews(RegistrationInterface $package, Container $c): void |
|
191 | |||
192 | /** |
||
193 | * @param RegistrationInterface $package |
||
194 | 10 | */ |
|
195 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
||
208 | |||
209 | /** |
||
210 | * @param Container $c |
||
211 | */ |
||
212 | 10 | private function initConsoleApp(Container $c): void |
|
216 | |||
217 | 10 | /** |
|
218 | * @param Container $c |
||
219 | 10 | */ |
|
220 | 10 | private function setupConsoleApp(Container $c): void |
|
225 | |||
226 | 10 | /** |
|
227 | * @param array $packages |
||
228 | * @param Container $c |
||
229 | */ |
||
230 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
||
245 | |||
246 | 10 | /** |
|
247 | 10 | * @param Container $c |
|
248 | 10 | * @throws \Bone\Exception |
|
249 | */ |
||
250 | private function setupTranslator(Container $c) |
||
256 | 10 | ||
257 | 10 | ||
258 | 10 | /** |
|
259 | 10 | * @param Container $c |
|
260 | 10 | * @throws \Bone\Exception |
|
261 | */ |
||
262 | private function setupPdoConnection(Container $c) |
||
267 | 10 | ||
268 | 10 | /** |
|
269 | 10 | * @param Container $c |
|
270 | */ |
||
271 | private function setupDownloadController(Container $c): void |
||
279 | 10 | ||
280 | /** |
||
281 | * @param Container $c |
||
282 | */ |
||
283 | private function setupRouteFirewall(Container $c): void |
||
288 | 10 | ||
289 | 10 | /** |
|
290 | * @param Container $c |
||
291 | 10 | * @throws \Exception |
|
292 | 10 | */ |
|
293 | private function setupLogs(Container $c) |
||
298 | |||
299 | /** |
||
300 | * @param Container $c |
||
301 | 10 | */ |
|
302 | private function setupVendorViewOverrides(Container $c): void |
||
313 | 10 | ||
314 | /** |
||
315 | 10 | * @param string $view |
|
316 | 10 | * @param string $folder |
|
317 | 10 | * @param Folders $registeredViews |
|
318 | */ |
||
319 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
||
327 | 10 | ||
328 | 10 | /** |
|
329 | 10 | * @param Container $c |
|
330 | 10 | */ |
|
331 | 10 | private function initMiddlewareStack(Container $c): void |
|
336 | 10 | ||
337 | /** |
||
338 | * @param Container $c |
||
339 | */ |
||
340 | private function setupMiddlewareStack(Container $c): void |
||
355 | } |
||
356 |