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 |
||
31 | class ApplicationPackage implements RegistrationInterface |
||
32 | { |
||
33 | /** @var array $config */ |
||
34 | private $config; |
||
35 | |||
36 | /** @var Router $router */ |
||
37 | private $router; |
||
38 | |||
39 | /** |
||
40 | * ApplicationPackage constructor. |
||
41 | * @param array $config |
||
42 | * @param Router $router |
||
43 | */ |
||
44 | 10 | public function __construct(array $config, Router $router) |
|
49 | |||
50 | /** |
||
51 | * @param Container $c |
||
52 | * @throws \Bone\Exception |
||
53 | * @throws \Exception |
||
54 | */ |
||
55 | 10 | public function addToContainer(Container $c) |
|
72 | |||
73 | /** |
||
74 | * @param Container $c |
||
75 | */ |
||
76 | 10 | private function setConfigArray(Container $c) |
|
82 | |||
83 | /** |
||
84 | * @param Container $c |
||
85 | */ |
||
86 | 10 | private function setupViewEngine(Container $c) |
|
91 | |||
92 | /** |
||
93 | * @param Container $c |
||
94 | */ |
||
95 | 10 | private function setupRouter(Container $c) |
|
100 | |||
101 | /** |
||
102 | * @param Container $c |
||
103 | */ |
||
104 | 10 | private function setupPackages(Container $c) |
|
119 | |||
120 | /** |
||
121 | * @param string $packageName |
||
122 | * @param Container $c |
||
123 | */ |
||
124 | 10 | private function registerPackage(string $packageName, Container $c): void |
|
134 | |||
135 | /** |
||
136 | * @param RegistrationInterface $package |
||
137 | */ |
||
138 | 10 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
152 | |||
153 | /** |
||
154 | * @param RegistrationInterface $package |
||
155 | */ |
||
156 | 10 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
|
163 | |||
164 | /** |
||
165 | * @param RegistrationInterface $package |
||
166 | */ |
||
167 | 10 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
173 | |||
174 | /** |
||
175 | * @param RegistrationInterface $package |
||
176 | */ |
||
177 | 10 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
|
190 | |||
191 | /** |
||
192 | * @param Container $c |
||
193 | */ |
||
194 | 10 | private function initConsoleApp(Container $c): void |
|
198 | |||
199 | /** |
||
200 | * @param Container $c |
||
201 | */ |
||
202 | 10 | private function setupConsoleApp(Container $c): void |
|
207 | |||
208 | /** |
||
209 | * @param array $packages |
||
210 | * @param Container $c |
||
211 | */ |
||
212 | 10 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
|
213 | { |
||
214 | 10 | foreach ($packages as $packageName) { |
|
215 | 10 | if (class_exists($packageName)) { |
|
216 | /** @var RegistrationInterface $package */ |
||
217 | 10 | $package = new $packageName(); |
|
218 | |||
219 | 10 | if ($package instanceof EntityRegistrationInterface) { |
|
220 | 10 | $paths = $c['entity_paths']; |
|
221 | 10 | $paths[] = $package->getEntityPath(); |
|
222 | 10 | $c['entity_paths'] = $paths; |
|
223 | } |
||
224 | } |
||
225 | } |
||
226 | 10 | } |
|
227 | |||
228 | /** |
||
229 | * @param Container $c |
||
230 | * @throws \Bone\Exception |
||
231 | */ |
||
232 | 10 | private function setupTranslator(Container $c) |
|
238 | |||
239 | |||
240 | /** |
||
241 | * @param Container $c |
||
242 | * @throws \Bone\Exception |
||
243 | */ |
||
244 | 10 | private function setupPdoConnection(Container $c) |
|
249 | |||
250 | /** |
||
251 | * @param Container $c |
||
252 | */ |
||
253 | 10 | private function setupDownloadController(Container $c): void |
|
261 | |||
262 | /** |
||
263 | * @param Container $c |
||
264 | */ |
||
265 | 10 | private function setupRouteFirewall(Container $c): void |
|
270 | |||
271 | /** |
||
272 | * @param Container $c |
||
273 | * @throws \Exception |
||
274 | */ |
||
275 | 10 | private function setupLogs(Container $c) |
|
280 | |||
281 | /** |
||
282 | * @param Container $c |
||
283 | */ |
||
284 | 10 | private function setupVendorViewOverrides(Container $c): void |
|
295 | |||
296 | /** |
||
297 | * @param string $view |
||
298 | * @param string $folder |
||
299 | * @param Folders $registeredViews |
||
300 | */ |
||
301 | 10 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
|
309 | |||
310 | /** |
||
311 | * @param Container $c |
||
312 | */ |
||
313 | 10 | private function initMiddlewareStack(Container $c): void |
|
318 | |||
319 | /** |
||
320 | * @param Container $c |
||
321 | */ |
||
322 | 10 | private function setupMiddlewareStack(Container $c): void |
|
337 | } |
||
338 |