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 | 9 | public function addToContainer(Container $c) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param Container $c |
||
| 75 | */ |
||
| 76 | 9 | private function setConfigArray(Container $c) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param Container $c |
||
| 85 | */ |
||
| 86 | 9 | private function setupViewEngine(Container $c) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param Container $c |
||
| 94 | */ |
||
| 95 | 9 | private function setupRouter(Container $c) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param Container $c |
||
| 103 | */ |
||
| 104 | 9 | private function setupPackages(Container $c) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $packageName |
||
| 122 | * @param Container $c |
||
| 123 | */ |
||
| 124 | 9 | private function registerPackage(string $packageName, Container $c): void |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param RegistrationInterface $package |
||
| 137 | */ |
||
| 138 | 9 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param RegistrationInterface $package |
||
| 155 | */ |
||
| 156 | 9 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param RegistrationInterface $package |
||
| 166 | */ |
||
| 167 | 9 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param RegistrationInterface $package |
||
| 176 | */ |
||
| 177 | 9 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param Container $c |
||
| 193 | */ |
||
| 194 | 9 | private function initConsoleApp(Container $c): void |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param Container $c |
||
| 201 | */ |
||
| 202 | 9 | private function setupConsoleApp(Container $c): void |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param array $packages |
||
| 210 | * @param Container $c |
||
| 211 | */ |
||
| 212 | 9 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param Container $c |
||
| 230 | * @throws \Bone\Exception |
||
| 231 | */ |
||
| 232 | 9 | private function setupTranslator(Container $c) |
|
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param Container $c |
||
| 242 | * @throws \Bone\Exception |
||
| 243 | */ |
||
| 244 | 9 | private function setupPdoConnection(Container $c) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param Container $c |
||
| 252 | */ |
||
| 253 | 9 | private function setupDownloadController(Container $c): void |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param Container $c |
||
| 264 | */ |
||
| 265 | 9 | private function setupRouteFirewall(Container $c): void |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param Container $c |
||
| 273 | * @throws \Exception |
||
| 274 | */ |
||
| 275 | 9 | private function setupLogs(Container $c) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param Container $c |
||
| 283 | */ |
||
| 284 | 9 | private function setupVendorViewOverrides(Container $c): void |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $view |
||
| 298 | * @param string $folder |
||
| 299 | * @param Folders $registeredViews |
||
| 300 | */ |
||
| 301 | 9 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param Container $c |
||
| 312 | */ |
||
| 313 | 9 | private function initMiddlewareStack(Container $c): void |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param Container $c |
||
| 321 | */ |
||
| 322 | 9 | private function setupMiddlewareStack(Container $c): void |
|
| 337 | } |
||
| 338 |