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) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param Container $c |
||
| 74 | */ |
||
| 75 | 10 | private function setConfigArray(Container $c) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param Container $c |
||
| 84 | */ |
||
| 85 | 10 | private function setupViewEngine(Container $c) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param Container $c |
||
| 93 | */ |
||
| 94 | 10 | private function setupRouter(Container $c) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param Container $c |
||
| 102 | */ |
||
| 103 | 10 | private function setupPackages(Container $c) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $packageName |
||
| 121 | * @param Container $c |
||
| 122 | */ |
||
| 123 | 10 | private function registerPackage(string $packageName, Container $c): void |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param RegistrationInterface $package |
||
| 136 | */ |
||
| 137 | 10 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param RegistrationInterface $package |
||
| 154 | */ |
||
| 155 | 10 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param RegistrationInterface $package |
||
| 165 | */ |
||
| 166 | 10 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param RegistrationInterface $package |
||
| 175 | */ |
||
| 176 | 10 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param Container $c |
||
| 192 | */ |
||
| 193 | 10 | private function setupConsoleApp(Container $c): void |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $packages |
||
| 201 | * @param Container $c |
||
| 202 | */ |
||
| 203 | 10 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param Container $c |
||
| 221 | * @throws \Bone\Exception |
||
| 222 | */ |
||
| 223 | 10 | private function setupTranslator(Container $c) |
|
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * @param Container $c |
||
| 233 | * @throws \Bone\Exception |
||
| 234 | */ |
||
| 235 | 10 | private function setupPdoConnection(Container $c) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param Container $c |
||
| 243 | */ |
||
| 244 | 10 | private function setupDownloadController(Container $c): void |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param Container $c |
||
| 255 | */ |
||
| 256 | 10 | private function setupRouteFirewall(Container $c): void |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param Container $c |
||
| 264 | * @throws \Exception |
||
| 265 | */ |
||
| 266 | 10 | private function setupLogs(Container $c) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param Container $c |
||
| 274 | */ |
||
| 275 | 10 | private function setupVendorViewOverrides(Container $c): void |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $view |
||
| 289 | * @param string $folder |
||
| 290 | * @param Folders $registeredViews |
||
| 291 | */ |
||
| 292 | 10 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 1 | public function getEntityPath(): string |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | 1 | public function hasEntityPath(): bool |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param Container $c |
||
| 319 | */ |
||
| 320 | 10 | private function initMiddlewareStack(Container $c): void |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param Container $c |
||
| 328 | */ |
||
| 329 | 10 | private function setupMiddlewareStack(Container $c): void |
|
| 344 | } |
||
| 345 |