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 |
||
| 17 | abstract class AbstractRecord extends SchematicEntity |
||
| 18 | { |
||
| 19 | use SolidableTrait; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Set of schema sections needed to describe entity behaviour. |
||
| 23 | */ |
||
| 24 | const SH_PRIMARY_KEY = 0; |
||
| 25 | const SH_DEFAULTS = 1; |
||
| 26 | const SH_RELATIONS = 6; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Record behaviour definition. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $recordSchema = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Record field updates (changed values). This array contain set of initial property values if |
||
| 37 | * any of them changed. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $changes = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * AssociatedRelation bucket. Manages declared record relations. |
||
| 45 | * |
||
| 46 | * @var RelationBucket |
||
| 47 | */ |
||
| 48 | protected $relations; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Parent ORM instance, responsible for relation initialization and lazy loading operations. |
||
| 52 | * |
||
| 53 | * @invisible |
||
| 54 | * @var ORMInterface |
||
| 55 | */ |
||
| 56 | protected $orm; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param ORMInterface $orm |
||
| 60 | * @param array $data |
||
| 61 | * @param RelationBucket $relations |
||
| 62 | */ |
||
| 63 | public function __construct( |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get value of primary of model. Make sure to call isLoaded first! |
||
| 80 | * |
||
| 81 | * @return int|string|null |
||
| 82 | */ |
||
| 83 | public function primaryKey() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | * |
||
| 91 | * @throws RelationException |
||
| 92 | */ |
||
| 93 | public function getField(string $name, $default = null, bool $filter = true) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | * |
||
| 107 | * @param bool $registerChanges Track field changes. |
||
| 108 | * |
||
| 109 | * @throws RelationException |
||
| 110 | */ |
||
| 111 | public function setField( |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function hasField(string $name): bool |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | * |
||
| 147 | * @throws FieldException |
||
| 148 | * @throws RelationException |
||
| 149 | */ |
||
| 150 | public function __unset($offset) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | * |
||
| 169 | * Method does not check updates in nested relation, but only in primary record. |
||
| 170 | * |
||
| 171 | * @param string $field Check once specific field changes. |
||
| 172 | */ |
||
| 173 | public function hasChanges(string $field = null): bool |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function __debugInfo() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | protected function isNullable(string $field): bool |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | * |
||
| 235 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 236 | * |
||
| 237 | * @see CompositionDefinition |
||
| 238 | */ |
||
| 239 | protected function createAccessor( |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | protected function iocContainer() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Name of column used as primary key. |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function primaryColumn(): string |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Create set of fields to be sent to UPDATE statement. |
||
| 274 | * |
||
| 275 | * @param bool $skipPrimary Skip primary key |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | protected function packChanges(bool $skipPrimary = false): array |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Indicate that all updates done, reset dirty state. |
||
| 318 | */ |
||
| 319 | protected function flushChanges() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $name |
||
| 332 | */ |
||
| 333 | private function registerChange(string $name) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $name |
||
| 347 | * |
||
| 348 | * @throws FieldException |
||
| 349 | */ |
||
| 350 | private function assertField(string $name) |
||
| 360 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..