Complex classes like Flare 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 Flare, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Flare |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The Flare version. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | const VERSION = '0.9.x-dev'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Array of expected configuration keys |
||
| 19 | * with the absolute bare-minimum defaults. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $configurationKeys = [ |
||
| 24 | 'admin_title' => 'Laravel Flare', |
||
| 25 | 'admin_url' => 'admin', |
||
| 26 | 'admin_theme' => 'red', |
||
| 27 | 'admin' => [], |
||
| 28 | 'attributes' => [], |
||
| 29 | 'models' => [], |
||
| 30 | 'modules' => [], |
||
| 31 | 'widgets' => [], |
||
| 32 | 'permissions' => \LaravelFlare\Flare\Permissions\Permissions::class, |
||
| 33 | 'policies' => [], |
||
| 34 | 'show' => [ |
||
| 35 | 'github' => true, |
||
| 36 | 'login' => true, |
||
| 37 | 'notifications' => true, |
||
| 38 | 'version' => true, |
||
| 39 | ] |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array of Helper Methods. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $helpers = [ |
||
| 48 | 'admin' => \LaravelFlare\Flare\Admin\AdminManager::class, |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Application Instance |
||
| 53 | * |
||
| 54 | * @var \Illuminate\Foundation\Application |
||
| 55 | */ |
||
| 56 | protected $app; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Flare Configuration |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $config; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The Title of the Admin Panel |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $adminTitle; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Safe Title of the Admin Panel |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $safeAdminTitle; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Relative Base URL of Admin Panel |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $relativeAdminUrl; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * __construct. |
||
| 88 | * |
||
| 89 | * @param \Illuminate\Foundation\Application $app |
||
| 90 | */ |
||
| 91 | public function __construct($app) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the Application Instance. |
||
| 100 | * |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | public function app() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns a Flare configuration value(s). |
||
| 110 | * |
||
| 111 | * @param string $key |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | public function config($key) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns a Flare configuration value(s), falling back |
||
| 122 | * to the defined bare-minimum configuration defaults |
||
| 123 | * if, for whatever reason the config is undefined. |
||
| 124 | * |
||
| 125 | * @param string $key |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | public function getConfig($key) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Allow setting of the Flare config at runtime. |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function setConfig() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set the loaded config to the protected property. |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function setLoadedConfig() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | * |
||
| 161 | * @deprecated 0.9 Use getAdminTitle() instead. |
||
| 162 | */ |
||
| 163 | public function adminTitle() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns the defined Admin Title. |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getAdminTitle() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the Admin Title |
||
| 180 | * |
||
| 181 | * @param mixed $title |
||
| 182 | * |
||
| 183 | * @return void |
||
| 184 | */ |
||
| 185 | public function setAdminTitle($title = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | * |
||
| 193 | * @deprecated 0.9 Use getSafeAdminTitle() instead. |
||
| 194 | */ |
||
| 195 | public function safeAdminTitle() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the defined Admin Title, converted |
||
| 202 | * to a safer format (for <title> tags etc.). |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getSafeAdminTitle() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets the Safe Admin Title which is used |
||
| 213 | * in <title> tags etc. |
||
| 214 | * |
||
| 215 | * @param mixed $title |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function setSafeAdminTitle($title = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns URL to a path in the Admin Panel, using the |
||
| 226 | * Admin URL defined in the Flare Config. |
||
| 227 | * |
||
| 228 | * @param string $path |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function adminUrl($path = '') |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns URL to a path in the Admin Panel, using the |
||
| 239 | * Admin URL defined in the Flare Config. |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function relativeAdminUrl($path = '') |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns URL to a path in the Admin Panel, using the |
||
| 252 | * Admin URL defined in the Flare Config. |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getRelativeAdminUrl() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set the Flare Relative Admin URL. |
||
| 263 | * |
||
| 264 | * If the provided path is null the relative path provided |
||
| 265 | * with the getRelativeAdminUrl() method will return the |
||
| 266 | * configuration file default (or the Flare fallbacks). |
||
| 267 | * |
||
| 268 | * @param mixed $path |
||
| 269 | */ |
||
| 270 | public function setRelativeAdminUrl($path = null) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns URL to a path in the Flare Documentation. |
||
| 277 | * This is COMING SOON! |
||
| 278 | * |
||
| 279 | * @param string $path |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function docsUrl($path = '') |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Determines whether part of the Flare Admin Panel |
||
| 290 | * should be displayed or not and returns true / false. |
||
| 291 | * |
||
| 292 | * @param string $key |
||
| 293 | * |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | public function show($key = false) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Determines whether part of the Flare Admin Panel |
||
| 307 | * should be displayed or not and returns true / false. |
||
| 308 | * |
||
| 309 | * Accessor for getShow(). |
||
| 310 | * |
||
| 311 | * @param string $key |
||
| 312 | * |
||
| 313 | * @return boolean |
||
| 314 | */ |
||
| 315 | public function getShow($key = false) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the current Flare Version. |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function version() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns an array of all of the Available Attribute Types. |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | protected function availableAttributes() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Determines if an AttributeType class exists or not. |
||
| 354 | * |
||
| 355 | * @param string $type |
||
| 356 | * |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | protected function attributeTypeExists($type) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Render Attribute. |
||
| 366 | * |
||
| 367 | * @param string $action |
||
| 368 | * @param string $attribute |
||
| 369 | * @param string $field |
||
| 370 | * @param string $model |
||
| 371 | * @param string $modelManager |
||
| 372 | * |
||
| 373 | * @return \Illuminate\Http\Response |
||
| 374 | */ |
||
| 375 | public function renderAttribute($action, $attribute, $field, $model, $modelManager) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Resolves the Class of an Attribute and returns it as a string. |
||
| 392 | * |
||
| 393 | * @param string $type |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | protected function resolveAttributeClass($type) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Call a Helper Method. |
||
| 410 | * |
||
| 411 | * @param string $method |
||
| 412 | * @param mixed $parameters |
||
| 413 | * |
||
| 414 | * @return mixed |
||
| 415 | */ |
||
| 416 | protected function callHelperMethod($method, $parameters) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Provide access to Helper Methods. |
||
| 423 | * |
||
| 424 | * This provides an extensible way of adding helper classes |
||
| 425 | * which are registerable and available to adccess through |
||
| 426 | * the Flare Facade. |
||
| 427 | * |
||
| 428 | * @param string $method |
||
| 429 | * @param array $parameters |
||
| 430 | * |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | public function __call($method, $parameters) |
||
| 441 | } |
||
| 442 |
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..