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 |
||
| 26 | class Model implements ModelInterface |
||
| 27 | {
|
||
| 28 | use NormalizeNamespaceTrait; |
||
| 29 | use GetTypeTrait; |
||
| 30 | |||
| 31 | protected $table; |
||
| 32 | protected $entity; |
||
| 33 | protected $alias; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var FieldInterface[] |
||
| 37 | */ |
||
| 38 | protected $fields = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var IndexInterface[] |
||
| 42 | */ |
||
| 43 | protected $indexes = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var RelationInterface[] |
||
| 47 | */ |
||
| 48 | protected $relations = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * |
||
| 53 | * @param string $entityClass |
||
| 54 | * @param string $table |
||
| 55 | * @param FieldInterface[] $fields |
||
| 56 | * @param IndexInterface[] $indexes |
||
| 57 | * @param RelationInterface[] $relations |
||
| 58 | * |
||
| 59 | * @throws ModelException |
||
| 60 | */ |
||
| 61 | public function __construct($entityClass, $table, array $fields, array $indexes = [], array $relations = []) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Assigns fields to model |
||
| 73 | * |
||
| 74 | * @param array $fields |
||
| 75 | * |
||
| 76 | * @throws ModelException |
||
| 77 | */ |
||
| 78 | protected function assignFields($fields) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Assigns indexes to model |
||
| 92 | * |
||
| 93 | * @param array $indexes |
||
| 94 | * |
||
| 95 | * @throws ModelException |
||
| 96 | */ |
||
| 97 | protected function assignIndexes($indexes) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Assigns relations to model |
||
| 120 | * |
||
| 121 | * @param array $relations |
||
| 122 | * |
||
| 123 | * @throws ModelException |
||
| 124 | */ |
||
| 125 | protected function assignRelations($relations) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns table |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function table() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns entity class name |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function entity() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns alias |
||
| 162 | * |
||
| 163 | * @param string $alias |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function alias($alias = null) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns true if model has field |
||
| 178 | * |
||
| 179 | * @param string $field |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function hasField($field) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns array containing field definition |
||
| 190 | * |
||
| 191 | * @return FieldInterface[] |
||
| 192 | */ |
||
| 193 | public function fields() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Asserts if model has field |
||
| 200 | * |
||
| 201 | * @param string $field |
||
| 202 | * |
||
| 203 | * @throws ModelException |
||
| 204 | */ |
||
| 205 | protected function assertField($field) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns field definition |
||
| 214 | * |
||
| 215 | * @param string $field |
||
| 216 | * |
||
| 217 | * @return FieldInterface |
||
| 218 | * @throws ModelException |
||
| 219 | */ |
||
| 220 | public function field($field) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns array containing names of primary indexes |
||
| 229 | * |
||
| 230 | * @return FieldInterface[] |
||
| 231 | */ |
||
| 232 | public function primaryFields() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns array of fields from indexes |
||
| 250 | * |
||
| 251 | * @return FieldInterface[] |
||
| 252 | */ |
||
| 253 | public function indexFields() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns true if index with set name is defined |
||
| 270 | * |
||
| 271 | * @param string $index |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function hasIndex($index) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns all index definitions |
||
| 282 | * |
||
| 283 | * @return IndexInterface[] |
||
| 284 | */ |
||
| 285 | public function indexes() |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns index definition |
||
| 293 | * |
||
| 294 | * @param string $index |
||
| 295 | * |
||
| 296 | * @return IndexInterface[] |
||
| 297 | * @throws ModelException |
||
| 298 | */ |
||
| 299 | public function index($index) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns all relation where field is listed as local key |
||
| 310 | * |
||
| 311 | * @param string $field |
||
| 312 | * |
||
| 313 | * @return RelationInterface[] |
||
| 314 | */ |
||
| 315 | public function referredIn($field) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Returns true if at last one relation is defined |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function hasRelations() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns true if relation to passed entity class is defined |
||
| 341 | * |
||
| 342 | * @param string $relationName |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function hasRelation($relationName) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns all relation definition |
||
| 353 | * |
||
| 354 | * @return RelationInterface[] |
||
| 355 | */ |
||
| 356 | public function relations() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns relation definition for passed entity class |
||
| 363 | * |
||
| 364 | * @param string $relationName |
||
| 365 | * |
||
| 366 | * @return RelationInterface |
||
| 367 | * @throws ModelException |
||
| 368 | */ |
||
| 369 | public function relation($relationName) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Finds relation by its name |
||
| 380 | * |
||
| 381 | * @param string $relationName |
||
| 382 | * |
||
| 383 | * @return RelationInterface |
||
| 384 | */ |
||
| 385 | protected function findRelationByName($relationName) |
||
| 395 | } |
||
| 396 |