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 |
||
| 10 | class Application |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $rootDir; |
||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $configPath; |
||
| 20 | /** |
||
| 21 | * @var \stdClass |
||
| 22 | */ |
||
| 23 | private $config; |
||
| 24 | /** |
||
| 25 | * @var \CloudControl\Cms\storage\Storage |
||
| 26 | */ |
||
| 27 | private $storage; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \CloudControl\Cms\cc\Request |
||
| 31 | */ |
||
| 32 | private $request; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $matchedSitemapItems = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $applicationComponents = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Application constructor. |
||
| 46 | * @param string $rootDir |
||
| 47 | * @param string $configPath |
||
| 48 | */ |
||
| 49 | public function __construct($rootDir, $configPath) |
||
| 50 | { |
||
| 51 | $this->rootDir = $rootDir; |
||
| 52 | $this->configPath = $configPath; |
||
| 53 | |||
| 54 | $this->config(); |
||
| 55 | $this->storage(); |
||
| 56 | |||
| 57 | $this->request = new Request(); |
||
| 58 | |||
| 59 | $this->setExceptionHandler(); |
||
| 60 | $this->urlMatching(); |
||
| 61 | |||
| 62 | $this->getApplicationComponents(); |
||
| 63 | |||
| 64 | $this->run(); |
||
| 65 | $this->render(); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Initialize the config |
||
| 70 | * |
||
| 71 | * @throws \Exception |
||
| 72 | */ |
||
| 73 | private function config() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initialize the storage |
||
| 86 | */ |
||
| 87 | private function storage() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param Request $request |
||
| 94 | * @throws \Exception |
||
| 95 | */ |
||
| 96 | private function redirectMatching($request) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Loop through sitemap items and see if one matches the requestUri. |
||
| 123 | * If it does, add it tot the matchedSitemapItems array |
||
| 124 | * |
||
| 125 | * @param Request $request |
||
| 126 | */ |
||
| 127 | private function sitemapMatching($request) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Loop through all application components and run them |
||
| 153 | * |
||
| 154 | * @throws \Exception |
||
| 155 | */ |
||
| 156 | private function runApplicationComponents() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Loop through all (matched) sitemap components and run them |
||
| 168 | * |
||
| 169 | * @throws \Exception |
||
| 170 | */ |
||
| 171 | private function runSitemapComponents() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $class |
||
| 186 | * @param string $template |
||
| 187 | * @param array $parameters |
||
| 188 | * @param \stdClass|null $matchedSitemapItem |
||
| 189 | * |
||
| 190 | * @return Component |
||
| 191 | * @throws \Exception |
||
| 192 | */ |
||
| 193 | private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Loop through all application components and render them |
||
| 215 | */ |
||
| 216 | private function renderApplicationComponents() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Loop through all (matched) sitemap components and render them |
||
| 225 | */ |
||
| 226 | private function renderSitemapComponents() |
||
| 237 | |||
| 238 | public function getAllApplicationComponentParameters() |
||
| 247 | |||
| 248 | public function unlockApplicationComponentParameters() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set the default caching of pages to 2 days |
||
| 258 | */ |
||
| 259 | public function setCachingHeaders() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getTemplateDir() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getStorageDir() |
||
| 280 | |||
| 281 | public function getApplicationComponents() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getRootDir() |
||
| 293 | |||
| 294 | private function setExceptionHandler() |
||
| 300 | |||
| 301 | private function urlMatching() |
||
| 306 | |||
| 307 | private function run() |
||
| 312 | |||
| 313 | private function render() |
||
| 318 | } |
||
| 319 | } |
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: