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 | 50 | public function newEloquentBuilder($query) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritdoc |
||
| 40 | */ |
||
| 41 | 50 | protected function newBaseQueryBuilder() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | 13 | public function freshTimestamp() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @inheritdoc |
||
| 58 | */ |
||
| 59 | 13 | 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 | 14 | public function setAttribute($key, $value) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritdoc |
||
| 172 | */ |
||
| 173 | 50 | 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 | 45 | public function newCassandraCollection($rows) |
|
| 211 | 13 | ||
| 212 | /** |
||
| 213 | * Determine if the new and old values for a given key are equivalent. |
||
| 214 | 2 | * |
|
| 215 | * @param string $key |
||
| 216 | 2 | * @param mixed $current |
|
| 217 | 2 | * @return bool |
|
| 218 | 2 | */ |
|
| 219 | public function originalIsEquivalent($key, $current) |
||
| 245 | 1 | ||
| 246 | 1 | /** |
|
| 247 | 1 | * Check if object is instance of any cassandra object types |
|
| 248 | 1 | * |
|
| 249 | 1 | * @param $obj |
|
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | protected function isCassandraObject($obj) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Check if object is instance of any cassandra object types |
||
| 270 | * |
||
| 271 | * @param $obj |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | protected function isCompareableCassandraObject($obj) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns comparable value from cassandra object type |
||
| 287 | * |
||
| 288 | * @param $obj |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | protected function valueFromCassandraObject($obj) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get the value of the model's primary key. |
||
| 324 | * |
||
| 325 | * @return mixed |
||
| 326 | */ |
||
| 327 | public function getKey() |
||
| 336 | |||
| 337 | } |
||
| 338 |
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.