Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Mascame\Artificer\Model; |
||
10 | class Model |
||
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) |
||
78 | { |
||
79 | $this->schema = $schema; |
||
80 | $this->relations = new ModelRelation(); |
||
81 | |||
82 | if (Str::startsWith(Route::currentRouteName(), 'admin.model.')) { |
||
83 | $this->prepareCurrentModel(); |
||
84 | } |
||
85 | |||
86 | $this->share(); |
||
87 | } |
||
88 | |||
89 | |||
90 | public function share() |
||
91 | { |
||
92 | View::share('tables', $this->schema->tables); |
||
93 | View::share('models', $this->models = $this->getModelsData()); |
||
94 | View::share('model', $this->getCurrentModelData()); |
||
95 | } |
||
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() |
||
157 | |||
158 | /** |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function hasFillable() |
||
165 | |||
166 | /** |
||
167 | * @return int|null|string |
||
168 | */ |
||
169 | private function getCurrentModelName() |
||
183 | |||
184 | protected function prepareCurrentModel() |
||
195 | |||
196 | /** |
||
197 | * Fills all fields in config if they are not declared and applies default attributes |
||
198 | * |
||
199 | * @param $columns |
||
200 | * @param null $model |
||
201 | * @return mixed |
||
202 | */ |
||
203 | protected function addFieldOptions($columns, $model = null) { |
||
218 | |||
219 | /** |
||
220 | * @return array |
||
221 | */ |
||
222 | private function getCurrentModelData() |
||
234 | |||
235 | /** |
||
236 | * @param $model |
||
237 | * @return bool |
||
238 | */ |
||
239 | protected function isCurrent($modelName) |
||
247 | |||
248 | /** |
||
249 | * @return Model |
||
250 | */ |
||
251 | public static function getCurrent() |
||
255 | |||
256 | /** |
||
257 | * @param null $model |
||
258 | * @return null |
||
259 | */ |
||
260 | public function getRouteName($model = null) |
||
266 | |||
267 | /** |
||
268 | * @param $modelName |
||
269 | */ |
||
270 | protected function setCurrent($modelName) |
||
275 | |||
276 | /** |
||
277 | * @param null $model |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public function getOptions($model = null) |
||
291 | |||
292 | public function getDefaultOptions() |
||
298 | |||
299 | /** |
||
300 | * Take care! This are the options reflected in config files. |
||
301 | * Processed guarded/fillable should used with their own getters. |
||
302 | * |
||
303 | * @param $key |
||
304 | * @param null $model |
||
305 | * @return mixed |
||
306 | */ |
||
307 | public function getOption($key, $default = null, $model = null) |
||
314 | |||
315 | /** |
||
316 | * @return array|mixed |
||
317 | */ |
||
318 | public function getRelations() |
||
322 | |||
323 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.