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 |
||
| 18 | abstract class AbstractRecord extends SchematicEntity |
||
| 19 | { |
||
| 20 | use SolidableTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Set of schema sections needed to describe entity behaviour. |
||
| 24 | */ |
||
| 25 | const SH_PRIMARY_KEY = 0; |
||
| 26 | const SH_DEFAULTS = 1; |
||
| 27 | const SH_RELATIONS = 6; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Record behaviour definition. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $recordSchema = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Record field updates (changed values). This array contain set of initial property values if |
||
| 38 | * any of them changed. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $changes = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * AssociatedRelation bucket. Manages declared record relations. |
||
| 46 | * |
||
| 47 | * @var RelationBucket |
||
| 48 | */ |
||
| 49 | protected $relations; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Parent ORM instance, responsible for relation initialization and lazy loading operations. |
||
| 53 | * |
||
| 54 | * @invisible |
||
| 55 | * @var ORMInterface |
||
| 56 | */ |
||
| 57 | protected $orm; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param ORMInterface $orm |
||
| 61 | * @param array $data |
||
| 62 | * @param RelationBucket $relations |
||
| 63 | */ |
||
| 64 | public function __construct( |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get value of primary of model. Make sure to call isLoaded first, getting PK before entity |
||
| 81 | * have been saved is not possible. |
||
| 82 | * |
||
| 83 | * @return int|string|null |
||
| 84 | * |
||
| 85 | * @throws RecordException When PK is empty. |
||
| 86 | */ |
||
| 87 | public function primaryKey() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | * |
||
| 100 | * @throws RelationException |
||
| 101 | */ |
||
| 102 | public function getField(string $name, $default = null, bool $filter = true) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | * |
||
| 116 | * @param bool $registerChanges Track field changes. |
||
| 117 | * |
||
| 118 | * @throws RelationException |
||
| 119 | */ |
||
| 120 | public function setField( |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function hasField(string $name): bool |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | * |
||
| 156 | * @throws FieldException |
||
| 157 | * @throws RelationException |
||
| 158 | */ |
||
| 159 | public function __unset($offset) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | * |
||
| 178 | * Method does not check updates in nested relation, but only in primary record. |
||
| 179 | * |
||
| 180 | * @param string $field Check once specific field changes. |
||
| 181 | */ |
||
| 182 | public function hasChanges(string $field = null): bool |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | public function __debugInfo() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | * |
||
| 230 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 231 | * |
||
| 232 | * @see CompositionDefinition |
||
| 233 | */ |
||
| 234 | protected function createAccessor( |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | protected function iocContainer() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Name of column used as primary key. |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function primaryColumn(): string |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Create set of fields to be sent to UPDATE statement. |
||
| 269 | * |
||
| 270 | * @param bool $skipPrimary Skip primary key |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | protected function packChanges(bool $skipPrimary = false): array |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Indicate that all updates done, reset dirty state. |
||
| 312 | */ |
||
| 313 | protected function flushChanges() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $name |
||
| 326 | */ |
||
| 327 | private function registerChange(string $name) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $name |
||
| 341 | * |
||
| 342 | * @throws FieldException |
||
| 343 | */ |
||
| 344 | private function assertField(string $name) |
||
| 354 | } |
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..