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 | * Indicates if the IDs are auto-incrementing. |
||
| 17 | * This is not possible in cassandra so we override this |
||
| 18 | * |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | public $incrementing = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @inheritdoc |
||
| 25 | */ |
||
| 26 | 18 | public function newEloquentBuilder($query) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @inheritdoc |
||
| 33 | */ |
||
| 34 | 18 | protected function newBaseQueryBuilder() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritdoc |
||
| 43 | */ |
||
| 44 | 12 | public function freshTimestamp() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritdoc |
||
| 51 | */ |
||
| 52 | 12 | public function fromDateTime($value) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | 5 | protected function asDateTime($value) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @inheritdoc |
||
| 82 | */ |
||
| 83 | protected function originalIsNumericallyEquivalent($key) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the table qualified key name. |
||
| 101 | * Cassandra does not support the table.column annotation so |
||
| 102 | * we override this |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 8 | public function getQualifiedKeyName() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Qualify the given column name by the model's table. |
||
| 113 | * |
||
| 114 | * @param string $column |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 2 | public function qualifyColumn($column) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Set a given attribute on the model. |
||
| 124 | * |
||
| 125 | * @param string $key |
||
| 126 | * @param mixed $value |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | 12 | public function setAttribute($key, $value) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritdoc |
||
| 165 | */ |
||
| 166 | 18 | public function __call($method, $parameters) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Create a new Eloquent Collection instance. |
||
| 178 | * |
||
| 179 | * @param Rows $rows |
||
| 180 | * |
||
| 181 | * @return Collection |
||
| 182 | */ |
||
| 183 | 13 | public function newCassandraCollection(Rows $rows) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Determine if the new and old values for a given key are equivalent. |
||
| 190 | * |
||
| 191 | * @param string $key |
||
| 192 | * @param mixed $current |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 12 | public function originalIsEquivalent($key, $current) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Check if object is instance of any cassandra object types |
||
| 224 | * |
||
| 225 | * @param $obj |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 1 | protected function isCassandraObject($obj) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Check if object is instance of any cassandra object types |
||
| 246 | * |
||
| 247 | * @param $obj |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | protected function isCompareableCassandraObject($obj) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns comparable value from cassandra object type |
||
| 263 | * |
||
| 264 | * @param $obj |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | protected function valueFromCassandraObject($obj) |
||
| 297 | |||
| 298 | } |
||
| 299 |
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.