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 |
||
| 30 | class ApplicationPackage implements RegistrationInterface |
||
| 31 | { |
||
| 32 | /** @var array $config */ |
||
| 33 | private $config; |
||
| 34 | |||
| 35 | /** @var Router $router */ |
||
| 36 | private $router; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * ApplicationPackage constructor. |
||
| 40 | * @param array $config |
||
| 41 | * @param Router $router |
||
| 42 | */ |
||
| 43 | 10 | public function __construct(array $config, Router $router) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param Container $c |
||
| 51 | * @throws \Bone\Exception |
||
| 52 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | 10 | public function addToContainer(Container $c) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param Container $c |
||
| 73 | */ |
||
| 74 | 10 | private function setConfigArray(Container $c) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param Container $c |
||
| 83 | */ |
||
| 84 | 10 | private function setupViewEngine(Container $c) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param Container $c |
||
| 92 | */ |
||
| 93 | 10 | private function setupRouter(Container $c) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param Container $c |
||
| 101 | */ |
||
| 102 | 10 | private function setupPackages(Container $c) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $packageName |
||
| 120 | * @param Container $c |
||
| 121 | */ |
||
| 122 | 10 | private function registerPackage(string $packageName, Container $c): void |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param RegistrationInterface $package |
||
| 135 | */ |
||
| 136 | 10 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param RegistrationInterface $package |
||
| 153 | */ |
||
| 154 | 10 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param RegistrationInterface $package |
||
| 164 | */ |
||
| 165 | 10 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param RegistrationInterface $package |
||
| 174 | */ |
||
| 175 | 10 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param Container $c |
||
| 191 | */ |
||
| 192 | 10 | private function setupConsoleApp(Container $c): void |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $packages |
||
| 200 | * @param Container $c |
||
| 201 | */ |
||
| 202 | 10 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param Container $c |
||
| 220 | * @throws \Bone\Exception |
||
| 221 | */ |
||
| 222 | 10 | private function setupTranslator(Container $c) |
|
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @param Container $c |
||
| 232 | * @throws \Bone\Exception |
||
| 233 | */ |
||
| 234 | 10 | private function setupPdoConnection(Container $c) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param Container $c |
||
| 242 | */ |
||
| 243 | 10 | private function setupDownloadController(Container $c): void |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param Container $c |
||
| 254 | */ |
||
| 255 | 10 | private function setupRouteFirewall(Container $c): void |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param Container $c |
||
| 263 | * @throws \Exception |
||
| 264 | */ |
||
| 265 | 10 | private function setupLogs(Container $c) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param Container $c |
||
| 273 | */ |
||
| 274 | 10 | private function setupVendorViewOverrides(Container $c): void |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $view |
||
| 288 | * @param string $folder |
||
| 289 | * @param Folders $registeredViews |
||
| 290 | */ |
||
| 291 | 10 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 1 | public function getEntityPath(): string |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 1 | public function hasEntityPath(): bool |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param Container $c |
||
| 318 | */ |
||
| 319 | 10 | private function initMiddlewareStack(Container $c): void |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param Container $c |
||
| 327 | */ |
||
| 328 | 10 | private function setupMiddlewareStack(Container $c): void |
|
| 343 | } |
||
| 344 |