Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Application |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $rootDir; |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $configPath; |
||
| 22 | /** |
||
| 23 | * @var \stdClass |
||
| 24 | */ |
||
| 25 | private $config; |
||
| 26 | /** |
||
| 27 | * @var \CloudControl\Cms\storage\Storage |
||
| 28 | */ |
||
| 29 | private $storage; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \CloudControl\Cms\cc\Request |
||
| 33 | */ |
||
| 34 | private $request; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $matchedSitemapItems = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $applicationComponents = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Application constructor. |
||
| 48 | * @param string $rootDir |
||
| 49 | * @param string $configPath |
||
| 50 | */ |
||
| 51 | public function __construct($rootDir, $configPath) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Initialize the config |
||
| 75 | * |
||
| 76 | * @throws \Exception |
||
| 77 | */ |
||
| 78 | private function config() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Initialize the storage |
||
| 91 | */ |
||
| 92 | private function storage() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param Request $request |
||
| 99 | * @throws \Exception |
||
| 100 | */ |
||
| 101 | private function redirectMatching($request) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Loop through sitemap items and see if one matches the requestUri. |
||
| 128 | * If it does, add it tot the matchedSitemapItems array |
||
| 129 | * |
||
| 130 | * @param Request $request |
||
| 131 | */ |
||
| 132 | private function sitemapMatching($request) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Loop through all application components and run them |
||
| 158 | * |
||
| 159 | * @throws \Exception |
||
| 160 | */ |
||
| 161 | private function runApplicationComponents() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Loop through all (matched) sitemap components and run them |
||
| 173 | * |
||
| 174 | * @throws \Exception |
||
| 175 | */ |
||
| 176 | private function runSitemapComponents() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $class |
||
| 191 | * @param string $template |
||
| 192 | * @param array $parameters |
||
| 193 | * @param \stdClass|null $matchedSitemapItem |
||
| 194 | * |
||
| 195 | * @return Component |
||
| 196 | * @throws \Exception |
||
| 197 | */ |
||
| 198 | private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Loop through all application components and render them |
||
| 220 | */ |
||
| 221 | private function renderApplicationComponents() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Loop through all (matched) sitemap components and render them |
||
| 230 | */ |
||
| 231 | private function renderSitemapComponents() |
||
| 242 | |||
| 243 | public function getAllApplicationComponentParameters() |
||
| 252 | |||
| 253 | public function unlockApplicationComponentParameters() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set the default caching of pages to 2 days |
||
| 263 | */ |
||
| 264 | public function setCachingHeaders() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getTemplateDir() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getStorageDir() |
||
| 285 | |||
| 286 | public function getApplicationComponents() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getRootDir() |
||
| 298 | |||
| 299 | private function setExceptionHandler() |
||
| 305 | |||
| 306 | private function urlMatching() |
||
| 311 | |||
| 312 | private function run() |
||
| 317 | |||
| 318 | private function render() |
||
| 323 | |||
| 324 | private function startServices() |
||
| 329 | } |
||
| 330 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: