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 |
||
13 | abstract class Model extends BaseModel |
||
14 | { |
||
15 | /** |
||
16 | * The connection name for the model. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $connection = 'cassandra'; |
||
21 | |||
22 | /** |
||
23 | * Indicates if the IDs are auto-incrementing. |
||
24 | * This is not possible in cassandra so we override this |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | public $incrementing = false; |
||
29 | |||
30 | /** |
||
31 | * @inheritdoc |
||
32 | */ |
||
33 | 30 | public function newEloquentBuilder($query) |
|
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | 30 | protected function newBaseQueryBuilder() |
|
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | 12 | public function freshTimestamp() |
|
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | 12 | public function fromDateTime($value) |
|
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | 5 | protected function asDateTime($value) |
|
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | protected function originalIsNumericallyEquivalent($key) |
||
105 | |||
106 | /** |
||
107 | * Get the table qualified key name. |
||
108 | * Cassandra does not support the table.column annotation so |
||
109 | * we override this |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 8 | public function getQualifiedKeyName() |
|
117 | |||
118 | /** |
||
119 | * Qualify the given column name by the model's table. |
||
120 | * |
||
121 | * @param string $column |
||
122 | * @return string |
||
123 | */ |
||
124 | 2 | public function qualifyColumn($column) |
|
128 | |||
129 | /** |
||
130 | * Set a given attribute on the model. |
||
131 | * |
||
132 | * @param string $key |
||
133 | * @param mixed $value |
||
134 | * @return $this |
||
135 | */ |
||
136 | 12 | public function setAttribute($key, $value) |
|
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | 30 | public function __call($method, $parameters) |
|
182 | |||
183 | /** |
||
184 | * Create a new Eloquent Collection instance. |
||
185 | * |
||
186 | * @param Rows|array $rows |
||
187 | * |
||
188 | * @return Collection |
||
189 | * |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | 25 | public function newCassandraCollection($rows) |
|
200 | |||
201 | /** |
||
202 | * Determine if the new and old values for a given key are equivalent. |
||
203 | * |
||
204 | * @param string $key |
||
205 | * @param mixed $current |
||
206 | * @return bool |
||
207 | */ |
||
208 | 12 | public function originalIsEquivalent($key, $current) |
|
234 | |||
235 | /** |
||
236 | * Check if object is instance of any cassandra object types |
||
237 | * |
||
238 | * @param $obj |
||
239 | * @return bool |
||
240 | */ |
||
241 | 1 | protected function isCassandraObject($obj) |
|
256 | |||
257 | /** |
||
258 | * Check if object is instance of any cassandra object types |
||
259 | * |
||
260 | * @param $obj |
||
261 | * @return bool |
||
262 | */ |
||
263 | protected function isCompareableCassandraObject($obj) |
||
273 | |||
274 | /** |
||
275 | * Returns comparable value from cassandra object type |
||
276 | * |
||
277 | * @param $obj |
||
278 | * @return mixed |
||
279 | */ |
||
280 | protected function valueFromCassandraObject($obj) |
||
310 | |||
311 | } |
||
312 |
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.