Complex classes like ModelManager 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 ModelManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Mascame\Artificer\Model; |
||
| 10 | class ModelManager |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var ModelSchema |
||
| 15 | */ |
||
| 16 | public $schema; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | public $models; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $columns; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \Illuminate\Database\Eloquent\Model |
||
| 30 | */ |
||
| 31 | public $model; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $class; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var |
||
| 40 | */ |
||
| 41 | public $name; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $keyname; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var |
||
| 50 | */ |
||
| 51 | public $table; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var |
||
| 55 | */ |
||
| 56 | public $fillable; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array|mixed |
||
| 60 | */ |
||
| 61 | protected $options = []; |
||
| 62 | protected $defaultOptions = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array|mixed |
||
| 66 | */ |
||
| 67 | public $relations = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var |
||
| 71 | */ |
||
| 72 | public static $current = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param ModelSchema $schema |
||
| 76 | */ |
||
| 77 | public function __construct(ModelSchema $schema) |
||
| 88 | |||
| 89 | |||
| 90 | public function share() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | private function getModelsData() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param $modelName |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function isHidden($modelName) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function hasGuarded() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Look for admin model config, if there is nothing fallback to Model property |
||
| 140 | * |
||
| 141 | * @return array|mixed |
||
| 142 | */ |
||
| 143 | public function getGuarded() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Look for admin model config, if there is nothing fallback to Model property |
||
| 150 | * |
||
| 151 | * @return array|mixed |
||
| 152 | */ |
||
| 153 | public function getFillable() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function hasFillable() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return int|null|string |
||
| 174 | */ |
||
| 175 | private function getCurrentModelName() |
||
| 189 | |||
| 190 | protected function prepareCurrentModel() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Fills all fields in config if they are not declared and applies default attributes |
||
| 204 | * |
||
| 205 | * @param $columns |
||
| 206 | * @param null $model |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | protected function addFieldOptions($columns, $model = null) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | private function getCurrentModelData() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $model |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | protected function isCurrent($modelName) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return ModelManager |
||
| 256 | */ |
||
| 257 | public static function getCurrent() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param null $model |
||
| 264 | * @return null |
||
| 265 | */ |
||
| 266 | public function getRouteName($model = null) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $modelName |
||
| 275 | */ |
||
| 276 | protected function setCurrent($modelName) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param null $model |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | public function getOptions($model = null) |
||
| 297 | |||
| 298 | public function getDefaultOptions() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Take care! This are the options reflected in config files. |
||
| 307 | * Processed guarded/fillable should used with their own getters. |
||
| 308 | * |
||
| 309 | * @param $key |
||
| 310 | * @param null $model |
||
| 311 | * @return mixed |
||
| 312 | */ |
||
| 313 | public function getOption($key, $default = null, $model = null) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return array|mixed |
||
| 323 | */ |
||
| 324 | public function getRelations() |
||
| 328 | |||
| 329 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.