Complex classes like AbstractRecord 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 AbstractRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class AbstractRecord extends SchematicEntity |
||
| 22 | { |
||
| 23 | use SolidableTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Set of schema sections needed to describe entity behaviour. |
||
| 27 | */ |
||
| 28 | const SH_PRIMARY_KEY = 0; |
||
| 29 | const SH_DEFAULTS = 1; |
||
| 30 | const SH_RELATIONS = 5; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Record behaviour definition. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $recordSchema = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Record field updates (changed values). This array contain set of initial property values if |
||
| 41 | * any of them changed. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $changes = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * AssociatedRelation bucket. Manages declared record relations. |
||
| 49 | * |
||
| 50 | * @var RelationMap |
||
| 51 | */ |
||
| 52 | protected $relations; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Parent ORM instance, responsible for relation initialization and lazy loading operations. |
||
| 56 | * |
||
| 57 | * @invisible |
||
| 58 | * @var ORMInterface |
||
| 59 | */ |
||
| 60 | protected $orm; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param ORMInterface $orm |
||
| 64 | * @param array $data |
||
| 65 | * @param RelationMap $relations |
||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get value of primary of model. Make sure to call isLoaded first! |
||
| 84 | * |
||
| 85 | * Attention, this method MIGHT return null even when isLoaded() returns true, this situation |
||
| 86 | * is possible when record is scheduled for save or update but transaction/unit-of-work not |
||
| 87 | * executed yet. |
||
| 88 | * |
||
| 89 | * @return int|string|null |
||
| 90 | */ |
||
| 91 | public function primaryKey() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | * |
||
| 99 | * @throws RelationException |
||
| 100 | */ |
||
| 101 | public function getField(string $name, $default = null, bool $filter = true) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | * |
||
| 115 | * @param bool $registerChanges Track field changes. |
||
| 116 | * |
||
| 117 | * @throws RelationException |
||
| 118 | */ |
||
| 119 | public function setField( |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function hasField(string $name): bool |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | * |
||
| 155 | * @throws AccessException |
||
| 156 | * @throws RelationException |
||
| 157 | */ |
||
| 158 | public function __unset($offset) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | * |
||
| 177 | * Method does not check updates in nested relation, but only in primary record. |
||
| 178 | * |
||
| 179 | * @param string $field Check once specific field changes. |
||
| 180 | */ |
||
| 181 | public function hasChanges(string $field = null): bool |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function __debugInfo() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function __toString() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | protected function isNullable(string $field): bool |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | * |
||
| 253 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 254 | * |
||
| 255 | * @see CompositionDefinition |
||
| 256 | */ |
||
| 257 | protected function createAccessor( |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | protected function iocContainer() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Name of column used as primary key. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function primaryColumn(): string |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Create set of fields to be sent to UPDATE statement. |
||
| 292 | * |
||
| 293 | * @param bool $skipPrimary Skip primary key |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | protected function packChanges(bool $skipPrimary = false): array |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Indicate that all updates done, reset dirty state. |
||
| 336 | */ |
||
| 337 | protected function flushChanges() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $name |
||
| 350 | */ |
||
| 351 | private function registerChange(string $name) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $name |
||
| 365 | * |
||
| 366 | * @throws AccessException |
||
| 367 | */ |
||
| 368 | private function assertField(string $name) |
||
| 378 | } |
||
| 379 |