Complex classes like App 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 App, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class App |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Reference to instantiated controller object. |
||
| 22 | * |
||
| 23 | * @var object |
||
| 24 | */ |
||
| 25 | protected static $instance = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Available configuration files |
||
| 29 | * |
||
| 30 | * @var object |
||
| 31 | */ |
||
| 32 | private $files; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * System configuration |
||
| 36 | * |
||
| 37 | * @var object |
||
| 38 | */ |
||
| 39 | public $configuration; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * System service management |
||
| 43 | * |
||
| 44 | * @var object |
||
| 45 | */ |
||
| 46 | private $services; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Events |
||
| 50 | */ |
||
| 51 | private static $events; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Return value from application |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $output = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Namespaced controller path |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $class; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Instantiated class object |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $instantiatedClass; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Module being accessed |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $module; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Controller being accessed |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $controller; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Method being accessed |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private $method; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Params being passed |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $params; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Instantiated router class |
||
| 104 | * |
||
| 105 | * @var object |
||
| 106 | */ |
||
| 107 | private $router; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Instantiated request class |
||
| 111 | * |
||
| 112 | * @var object |
||
| 113 | */ |
||
| 114 | private $request; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Application bootstrap process |
||
| 118 | * |
||
| 119 | * The application registers the configuration in the app/config/core.php |
||
| 120 | * and then processes, and makes available the configured resources |
||
| 121 | */ |
||
| 122 | public function __construct() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Calls the proper shell for app execution |
||
| 146 | * |
||
| 147 | * @access private |
||
| 148 | */ |
||
| 149 | public function initialize() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * App preparation cycle |
||
| 167 | */ |
||
| 168 | private function prepare() |
||
| 188 | |||
| 189 | private function prepareServices() |
||
| 202 | |||
| 203 | public function getService($service, $new = false, $options = []) |
||
| 216 | |||
| 217 | public function setService($service, $class) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param mixed string with reference to config |
||
| 228 | * @return mixed bool or config values |
||
| 229 | */ |
||
| 230 | 28 | public function getConfiguration($config = null) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param $config mixed null|string |
||
| 258 | * @param null|object|array optional array of configuration data |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | * @throws Exception\StateException |
||
| 262 | */ |
||
| 263 | 18 | public function setConfiguration($config = null, $configObject = null) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Registers the session object |
||
| 275 | * |
||
| 276 | * @access private |
||
| 277 | */ |
||
| 278 | private function registerSession() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Verifies the provided application request is a valid request |
||
| 303 | * |
||
| 304 | * @access private |
||
| 305 | */ |
||
| 306 | private function processRequest() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Processes the application request |
||
| 323 | * |
||
| 324 | * @access private |
||
| 325 | */ |
||
| 326 | private function start() |
||
| 341 | /** |
||
| 342 | * Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called. |
||
| 343 | * |
||
| 344 | * @param string $event name |
||
| 345 | * @param mixed $value the optional value to pass to each callback |
||
| 346 | * @param mixed $callback the method or function to call - FALSE to remove all callbacks for event |
||
| 347 | */ |
||
| 348 | |||
| 349 | public static function addEvent($event, $callback = false) |
||
| 358 | |||
| 359 | public function callEvent($event, $method = false, $arguments = []) |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Prepare application return value into a string |
||
| 385 | * |
||
| 386 | * @access public |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function __toString() |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * Returns a reference of object once instantiated |
||
| 403 | * |
||
| 404 | * @access public |
||
| 405 | * @return object |
||
| 406 | */ |
||
| 407 | 28 | public static function getInstance() |
|
| 415 | } |
||
| 416 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..