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 |
||
15 | abstract class Model extends BaseModel |
||
16 | { |
||
17 | use CassandraTypesTrait; |
||
18 | |||
19 | /** |
||
20 | * The connection name for the model. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $connection = 'cassandra'; |
||
25 | |||
26 | /** |
||
27 | * Indicates if the IDs are auto-incrementing. |
||
28 | * This is not possible in cassandra so we override this |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | public $incrementing = false; |
||
33 | |||
34 | /** |
||
35 | * The storage format of the model's time columns. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $timeFormat; |
||
40 | |||
41 | /** |
||
42 | * List of columns in primary key |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $primaryColumns = []; |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | 58 | public function newEloquentBuilder($query) |
|
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | 58 | protected function newBaseQueryBuilder() |
|
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | 13 | public function freshTimestamp() |
|
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | 13 | public function fromDateTime($value) |
|
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 8 | protected function asDateTime($value) |
|
104 | |||
105 | /** |
||
106 | * Get the table qualified key name. |
||
107 | * Cassandra does not support the table.column annotation so |
||
108 | * we override this |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 8 | public function getQualifiedKeyName() |
|
116 | |||
117 | /** |
||
118 | * Qualify the given column name by the model's table. |
||
119 | * |
||
120 | * @param string $column |
||
121 | * @return string |
||
122 | */ |
||
123 | 2 | public function qualifyColumn($column) |
|
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | 58 | public function __call($method, $parameters) |
|
140 | |||
141 | /** |
||
142 | * Create a new Eloquent Collection instance. |
||
143 | * |
||
144 | * @param Rows|array $rows |
||
145 | * |
||
146 | * @return Collection |
||
147 | * |
||
148 | * @throws \Exception |
||
149 | */ |
||
150 | 53 | public function newCassandraCollection($rows) |
|
169 | |||
170 | /** |
||
171 | * Determine if the new and old values for a given key are equivalent. |
||
172 | * |
||
173 | * @param string $key |
||
174 | * @param mixed $current |
||
175 | * |
||
176 | * @return bool |
||
177 | * |
||
178 | * @throws \Exception |
||
179 | */ |
||
180 | 13 | public function originalIsEquivalent($key, $current) |
|
206 | |||
207 | /** |
||
208 | * Get the value of the model's primary key. |
||
209 | * |
||
210 | * @return mixed |
||
211 | */ |
||
212 | 16 | public function getKey() |
|
222 | |||
223 | /** |
||
224 | * Cast an attribute to a native PHP type. |
||
225 | * |
||
226 | * @param string $key |
||
227 | * @param mixed $value |
||
228 | * |
||
229 | * @return mixed |
||
230 | * |
||
231 | * @throws \Exception |
||
232 | */ |
||
233 | 3 | protected function castAttribute($key, $value) |
|
247 | |||
248 | /** |
||
249 | * Get the format for time stored in database. |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 1 | public function getTimeFormat() |
|
257 | |||
258 | /** |
||
259 | * Get the format for time stored in database. |
||
260 | * |
||
261 | * @param string $format |
||
262 | * |
||
263 | * @return self |
||
264 | */ |
||
265 | public function setTimeFormat($format) |
||
271 | |||
272 | /** |
||
273 | * Save the model to the database. |
||
274 | * |
||
275 | * @param array $options |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 13 | public function save(array $options = []) |
|
365 | |||
366 | } |
||
367 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.