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 | 18 | * @var bool |
|
27 | */ |
||
28 | 18 | public $incrementing = false; |
|
29 | |||
30 | /** |
||
31 | * @inheritdoc |
||
32 | */ |
||
33 | public function newEloquentBuilder($query) |
||
37 | |||
38 | 18 | /** |
|
39 | * @inheritdoc |
||
40 | */ |
||
41 | protected function newBaseQueryBuilder() |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function freshTimestamp() |
||
55 | 12 | ||
56 | 12 | /** |
|
57 | * @inheritdoc |
||
58 | */ |
||
59 | public function fromDateTime($value) |
||
73 | 5 | ||
74 | 5 | /** |
|
75 | * @inheritdoc |
||
76 | */ |
||
77 | protected function asDateTime($value) |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | protected function originalIsNumericallyEquivalent($key) |
||
105 | |||
106 | 8 | /** |
|
107 | * Get the table qualified key name. |
||
108 | 8 | * Cassandra does not support the table.column annotation so |
|
109 | * we override this |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getQualifiedKeyName() |
||
117 | 2 | ||
118 | /** |
||
119 | 2 | * Qualify the given column name by the model's table. |
|
120 | * |
||
121 | * @param string $column |
||
122 | * @return string |
||
123 | */ |
||
124 | public function qualifyColumn($column) |
||
128 | |||
129 | 12 | /** |
|
130 | * Set a given attribute on the model. |
||
131 | * |
||
132 | * @param string $key |
||
133 | * @param mixed $value |
||
134 | 12 | * @return $this |
|
135 | */ |
||
136 | public function setAttribute($key, $value) |
||
169 | 18 | ||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | 18 | public function __call($method, $parameters) |
|
182 | |||
183 | 13 | /** |
|
184 | * Create a new Eloquent Collection instance. |
||
185 | 13 | * |
|
186 | * @param Rows|array $rows |
||
187 | * |
||
188 | * @return Collection |
||
189 | * |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | public function newCassandraCollection($rows) |
||
200 | |||
201 | 2 | /** |
|
202 | * Determine if the new and old values for a given key are equivalent. |
||
203 | 2 | * |
|
204 | 2 | * @param string $key |
|
205 | 2 | * @param mixed $current |
|
206 | * @return bool |
||
207 | 2 | */ |
|
208 | 2 | public function originalIsEquivalent($key, $current) |
|
234 | 1 | ||
235 | 1 | /** |
|
236 | 1 | * Check if object is instance of any cassandra object types |
|
237 | * |
||
238 | * @param $obj |
||
239 | * @return bool |
||
240 | 1 | */ |
|
241 | 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.