Complex classes like Relation 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 Relation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | abstract class Relation extends Component implements |
||
| 30 | RelationInterface, |
||
| 31 | \Countable, |
||
| 32 | \IteratorAggregate, |
||
| 33 | \JsonSerializable |
||
| 34 | { |
||
| 35 | /* |
||
| 36 | * {@} table aliases. |
||
| 37 | */ |
||
| 38 | use AliasTrait; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Relation type, required to fetch record class from relation definition. |
||
| 42 | */ |
||
| 43 | const RELATION_TYPE = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Indication that relation represent multiple records (HAS_MANY relations). |
||
| 47 | */ |
||
| 48 | const MULTIPLE = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Indication that relation data has been loaded from databases. |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $loaded = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Pre-loaded relation data, can be loaded while parent record, or later. Real data instance |
||
| 59 | * will be constructed on demand and will keep it pre-loaded context between calls. |
||
| 60 | * |
||
| 61 | * @see Record::setContext() |
||
| 62 | * |
||
| 63 | * @var array|null |
||
| 64 | */ |
||
| 65 | protected $data = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Instance of constructed EntityInterface of RecordIterator. |
||
| 69 | * |
||
| 70 | * @invisible |
||
| 71 | * |
||
| 72 | * @var mixed|EntityInterface|RecordIterator |
||
| 73 | */ |
||
| 74 | protected $instance = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Parent Record caused relation to be created. |
||
| 78 | * |
||
| 79 | * @var RecordInterface |
||
| 80 | */ |
||
| 81 | protected $parent = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Relation definition fetched from ORM schema. Must already be normalized by RelationSchema. |
||
| 85 | * |
||
| 86 | * @invisible |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $definition = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @invisible |
||
| 94 | * |
||
| 95 | * @var ORM |
||
| 96 | */ |
||
| 97 | protected $orm = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param ORM $orm |
||
| 101 | * @param RecordInterface $parent |
||
| 102 | * @param array $definition Relation definition, must be normalized by relation |
||
| 103 | * schema. |
||
| 104 | * @param mixed $data Pre-loaded relation data. |
||
| 105 | * @param bool $loaded Indication that relation data has been loaded. |
||
| 106 | */ |
||
| 107 | public function __construct( |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function isLoaded() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | * |
||
| 132 | * Relation will automatically create related record if relation is not nullable. Usually |
||
| 133 | * applied for has one relations ($user->profile). |
||
| 134 | */ |
||
| 135 | public function getRelated() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | public function associate(EntityInterface $related = null) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | public function saveRelated($validate = true) |
||
| 204 | { |
||
| 205 | if (empty($instance = $this->getRelated())) { |
||
| 206 | //Nothing to save |
||
| 207 | return true; |
||
| 208 | } |
||
| 209 | |||
| 210 | if (static::MULTIPLE) { |
||
| 211 | /** |
||
| 212 | * @var RecordIterator|EntityInterface[] |
||
| 213 | */ |
||
| 214 | foreach ($instance as $record) { |
||
|
|
|||
| 215 | if (!$this->saveEntity($record, $validate)) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | return true; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $this->saveEntity($instance, $validate); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function reset(array $data = [], $loaded = false) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function isValid() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function hasErrors() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * List of errors associated with parent field, every field must have only one error assigned. |
||
| 282 | * |
||
| 283 | * @param bool $reset Clean errors after receiving every message. |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function getErrors($reset = false) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get selector associated with relation. |
||
| 314 | * |
||
| 315 | * @param array $where |
||
| 316 | * |
||
| 317 | * @return RecordSelector |
||
| 318 | */ |
||
| 319 | public function find(array $where = []) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | * |
||
| 327 | * Use getRelation() method to count pre-loaded data. |
||
| 328 | * |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | public function count() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Perform iterator on pre-loaded data. Use relation selector to iterate thought custom relation |
||
| 338 | * query. |
||
| 339 | * |
||
| 340 | * @return RecordEntity|RecordEntity[]|RecordIterator |
||
| 341 | */ |
||
| 342 | public function getIterator() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Bypassing call to created selector. |
||
| 349 | * |
||
| 350 | * @param string $method |
||
| 351 | * @param array $arguments |
||
| 352 | * |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | public function __call($method, array $arguments) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritdoc} |
||
| 362 | */ |
||
| 363 | public function jsonSerialize() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | protected function container() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Class name of outer record. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | protected function getClass() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Mount relation keys to parent or children records to ensure their connection. Method called |
||
| 388 | * when record requests relation save. |
||
| 389 | * |
||
| 390 | * @param EntityInterface $record |
||
| 391 | * |
||
| 392 | * @return EntityInterface |
||
| 393 | */ |
||
| 394 | abstract protected function mountRelation(EntityInterface $record); |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Convert pre-loaded relation data to record iterator record. |
||
| 398 | * |
||
| 399 | * @return RecordIterator |
||
| 400 | */ |
||
| 401 | protected function createIterator() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Convert pre-loaded relation data to active record record. |
||
| 408 | * |
||
| 409 | * @return RecordEntity |
||
| 410 | */ |
||
| 411 | protected function createRecord() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Create empty record to be associated with non nullable relation. |
||
| 418 | * |
||
| 419 | * @return RecordEntity |
||
| 420 | */ |
||
| 421 | protected function emptyRecord() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Load relation data based on created selector. |
||
| 431 | * |
||
| 432 | * @return array|null |
||
| 433 | */ |
||
| 434 | protected function loadData() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Internal ORM relation method used to create valid selector used to pre-load relation data or |
||
| 456 | * create custom query based on relation options. |
||
| 457 | * |
||
| 458 | * Must be redeclarated in child implementations. |
||
| 459 | * |
||
| 460 | * @return RecordSelector |
||
| 461 | */ |
||
| 462 | protected function createSelector() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Loadable when parent is loaded as well. |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | protected function isLoadable() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Save simple related entity. |
||
| 479 | * |
||
| 480 | * @param EntityInterface $entity |
||
| 481 | * @param bool $validate |
||
| 482 | * |
||
| 483 | * @return bool|void |
||
| 484 | */ |
||
| 485 | private function saveEntity(EntityInterface $entity, $validate) |
||
| 506 | } |
||
| 507 |