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 |
||
| 28 | class ApplicationPackage implements RegistrationInterface |
||
| 29 | { |
||
| 30 | /** @var array $config */ |
||
| 31 | private $config; |
||
| 32 | |||
| 33 | /** @var Router $router */ |
||
| 34 | private $router; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * ApplicationPackage constructor. |
||
| 38 | * @param array $config |
||
| 39 | * @param Router $router |
||
| 40 | */ |
||
| 41 | 10 | public function __construct(array $config, Router $router) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param Container $c |
||
| 49 | * @throws \Bone\Exception |
||
| 50 | * @throws \Exception |
||
| 51 | */ |
||
| 52 | 10 | public function addToContainer(Container $c) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param Container $c |
||
| 70 | */ |
||
| 71 | 10 | private function setConfigArray(Container $c) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param Container $c |
||
| 80 | */ |
||
| 81 | 10 | private function setupViewEngine(Container $c) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param Container $c |
||
| 89 | */ |
||
| 90 | 10 | private function setupPackages(Container $c) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $packageName |
||
| 108 | * @param Container $c |
||
| 109 | */ |
||
| 110 | 10 | private function registerPackage(string $packageName, Container $c): void |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param RegistrationInterface $package |
||
| 123 | */ |
||
| 124 | 10 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param RegistrationInterface $package |
||
| 141 | */ |
||
| 142 | 10 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param RegistrationInterface $package |
||
| 152 | */ |
||
| 153 | 10 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param RegistrationInterface $package |
||
| 162 | */ |
||
| 163 | 10 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param Container $c |
||
| 179 | */ |
||
| 180 | 10 | private function setupConsoleApp(Container $c): void |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @param array $packages |
||
| 196 | * @param Container $c |
||
| 197 | */ |
||
| 198 | 10 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param Container $c |
||
| 216 | * @throws \Bone\Exception |
||
| 217 | */ |
||
| 218 | 10 | private function setupTranslator(Container $c) |
|
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Container $c |
||
| 228 | * @throws \Bone\Exception |
||
| 229 | */ |
||
| 230 | 10 | private function setupPdoConnection(Container $c) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param Container $c |
||
| 238 | */ |
||
| 239 | 10 | private function setupDownloadController(Container $c): void |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param Container $c |
||
| 250 | */ |
||
| 251 | 10 | private function setupRouteFirewall(Container $c): void |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @param Container $c |
||
| 259 | * @throws \Exception |
||
| 260 | */ |
||
| 261 | 10 | private function setupLogs(Container $c) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param Container $c |
||
| 269 | */ |
||
| 270 | 10 | private function setupVendorViewOverrides(Container $c): void |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $view |
||
| 284 | * @param string $folder |
||
| 285 | * @param Folders $registeredViews |
||
| 286 | */ |
||
| 287 | 10 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 1 | public function getEntityPath(): string |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | 1 | public function hasEntityPath(): bool |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param Container $c |
||
| 314 | */ |
||
| 315 | 10 | private function initMiddlewareStack(Container $c): void |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param Container $c |
||
| 323 | */ |
||
| 324 | 10 | private function setupMiddlewareStack(Container $c): void |
|
| 339 | } |
||
| 340 |