Complex classes like AbstractEntity 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 AbstractEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class AbstractEntity extends MutableObject implements |
||
| 21 | EntityInterface, |
||
| 22 | \JsonSerializable, |
||
| 23 | \IteratorAggregate, |
||
| 24 | AccessorInterface, |
||
| 25 | PublishableInterface |
||
| 26 | { |
||
| 27 | use EventsTrait; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Field mutators. |
||
| 31 | * |
||
| 32 | * @private |
||
| 33 | */ |
||
| 34 | const MUTATOR_GETTER = 'getter'; |
||
| 35 | const MUTATOR_SETTER = 'setter'; |
||
| 36 | const MUTATOR_ACCESSOR = 'accessor'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $fields = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param array $data |
||
| 45 | */ |
||
| 46 | public function __construct(array $data = []) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * AccessorInterface dependency. |
||
| 56 | * |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public function setValue($data) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * AccessorInterface dependency. |
||
| 66 | * |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function packValue() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function hasField(string $name): bool |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | * |
||
| 85 | * @param bool $filter If false, associated field setter or accessor will be ignored. |
||
| 86 | * |
||
| 87 | * @throws \Spiral\Models\Exceptions\AccessException |
||
| 88 | */ |
||
| 89 | public function setField(string $name, $value, bool $filter = true) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | * |
||
| 120 | * @param bool $filter If false, associated field getter will be ignored. |
||
| 121 | * |
||
| 122 | * @throws \Spiral\Models\Exceptions\AccessException |
||
| 123 | */ |
||
| 124 | public function getField(string $name, $default = null, bool $filter = true) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | * |
||
| 147 | * @see $fillable |
||
| 148 | * @see $secured |
||
| 149 | * @see isFillable() |
||
| 150 | * |
||
| 151 | * @param array|\Traversable $fields |
||
| 152 | * @param bool $all Fill all fields including non fillable. |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | * |
||
| 156 | * @throws \Spiral\Models\Exceptions\AccessException |
||
| 157 | */ |
||
| 158 | public function setFields($fields = [], bool $all = false) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | protected function getKeys(): array |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | * |
||
| 188 | * Every getter and accessor will be applied/constructed if filter argument set to true. |
||
| 189 | * |
||
| 190 | * @param bool $filter |
||
| 191 | * |
||
| 192 | * @throws \Spiral\Models\Exceptions\AccessException |
||
| 193 | */ |
||
| 194 | public function getFields(bool $filter = true): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param mixed $offset |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function __isset($offset) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param mixed $offset |
||
| 216 | * |
||
| 217 | * @return mixed |
||
| 218 | */ |
||
| 219 | public function __get($offset) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param mixed $offset |
||
| 226 | * @param mixed $value |
||
| 227 | */ |
||
| 228 | public function __set($offset, $value) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param mixed $offset |
||
| 235 | */ |
||
| 236 | public function __unset($offset) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function offsetExists($offset) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function offsetGet($offset) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function offsetSet($offset, $value) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function offsetUnset($offset) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function getIterator(): \Iterator |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Pack entity fields data into plain array. |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | * |
||
| 286 | * @throws \Spiral\Models\Exceptions\AccessException |
||
| 287 | */ |
||
| 288 | public function packFields(): array |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | * |
||
| 305 | * Include every composition public data into result. |
||
| 306 | */ |
||
| 307 | public function publicValue(): array |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Alias for packFields. |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function toArray(): array |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | * |
||
| 342 | * By default use publicFields to be json serialized. |
||
| 343 | */ |
||
| 344 | public function jsonSerialize() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Destruct data entity. |
||
| 351 | */ |
||
| 352 | public function __destruct() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Reset every field value. |
||
| 359 | */ |
||
| 360 | protected function flushFields() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Indication that field in public and can be presented in published data. |
||
| 367 | * |
||
| 368 | * @param string $field |
||
| 369 | * |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | abstract protected function isPublic(string $field): bool; |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Check if field is fillable. |
||
| 376 | * |
||
| 377 | * @param string $field |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | abstract protected function isFillable(string $field): bool; |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get mutator associated with given field. |
||
| 385 | * |
||
| 386 | * @param string $field |
||
| 387 | * @param string $type See MUTATOR_* constants |
||
| 388 | * |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | abstract protected function getMutator(string $field, string $type); |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Nullable fields would not require automatic accessor creation. |
||
| 395 | * |
||
| 396 | * @param string $field |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | protected function isNullable(string $field): bool |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Create instance of field accessor. |
||
| 407 | * |
||
| 408 | * @param mixed|string $accessor Might be entity implementation specific. |
||
| 409 | * @param string $name |
||
| 410 | * @param mixed $value |
||
| 411 | * @param array $context Custom accessor context. |
||
| 412 | * |
||
| 413 | * @return AccessorInterface|null |
||
| 414 | * |
||
| 415 | * @throws AccessException |
||
| 416 | * @throws EntityException |
||
| 417 | */ |
||
| 418 | protected function createAccessor( |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get value thought associated mutator. |
||
| 436 | * |
||
| 437 | * @param string $name |
||
| 438 | * @param bool $filter |
||
| 439 | * @param mixed $value |
||
| 440 | * |
||
| 441 | * @return mixed |
||
| 442 | */ |
||
| 443 | private function getMutated(string $name, bool $filter, $value) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set value thought associated mutator. |
||
| 461 | * |
||
| 462 | * @param string $name |
||
| 463 | * @param mixed $value |
||
| 464 | */ |
||
| 465 | private function setMutated(string $name, $value) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Set value in/thought associated accessor. |
||
| 482 | * |
||
| 483 | * @param string $name |
||
| 484 | * @param string|array $accessor Accessor definition (implementation specific). |
||
| 485 | * @param mixed $value |
||
| 486 | */ |
||
| 487 | private function setAccessed($accessor, string $name, $value) |
||
| 506 | } |