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 |
||
| 22 | abstract class AbstractEntity extends MutableObject implements |
||
| 23 | EntityInterface, |
||
| 24 | \JsonSerializable, |
||
| 25 | \IteratorAggregate, |
||
| 26 | AccessorInterface, |
||
| 27 | PublishableInterface |
||
| 28 | { |
||
| 29 | use EventsTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Field mutators. |
||
| 33 | * |
||
| 34 | * @private |
||
| 35 | */ |
||
| 36 | const MUTATOR_GETTER = 'getter'; |
||
| 37 | const MUTATOR_SETTER = 'setter'; |
||
| 38 | const MUTATOR_ACCESSOR = 'accessor'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $fields = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param array $fields |
||
| 47 | */ |
||
| 48 | public function __construct(array $fields = []) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * AccessorInterface dependency. |
||
| 58 | * |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function stateValue($data) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * AccessorInterface dependency. |
||
| 68 | * |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function packValue() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function hasField(string $name): bool |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | * |
||
| 87 | * @param bool $filter If false, associated field setter or accessor will be ignored. |
||
| 88 | * |
||
| 89 | * @throws AccessorExceptionInterface |
||
| 90 | * @throws FieldException |
||
| 91 | */ |
||
| 92 | public function setField(string $name, $value, bool $filter = true) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | * |
||
| 123 | * @param bool $filter If false, associated field getter will be ignored. |
||
| 124 | * |
||
| 125 | * @throws AccessorExceptionInterface |
||
| 126 | */ |
||
| 127 | public function getField(string $name, $default = null, bool $filter = true) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | * |
||
| 150 | * @see $fillable |
||
| 151 | * @see $secured |
||
| 152 | * @see isFillable() |
||
| 153 | * |
||
| 154 | * @param array|\Traversable $fields |
||
| 155 | * @param bool $all Fill all fields including non fillable. |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | * |
||
| 159 | * @throws AccessorExceptionInterface |
||
| 160 | */ |
||
| 161 | public function setFields($fields = [], bool $all = false) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function getKeys(): array |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | * |
||
| 191 | * Every getter and accessor will be applied/constructed if filter argument set to true. |
||
| 192 | * |
||
| 193 | * @param bool $filter |
||
| 194 | * |
||
| 195 | * @throws AccessorExceptionInterface |
||
| 196 | */ |
||
| 197 | public function getFields(bool $filter = true): array |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param mixed $offset |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function __isset($offset) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param mixed $offset |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | public function __get($offset) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param mixed $offset |
||
| 229 | * @param mixed $value |
||
| 230 | */ |
||
| 231 | public function __set($offset, $value) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param mixed $offset |
||
| 238 | */ |
||
| 239 | public function __unset($offset) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function offsetExists($offset) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function offsetGet($offset) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | public function offsetSet($offset, $value) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function offsetUnset($offset) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function getIterator(): \Iterator |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Pack entity fields data into plain array. |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | * |
||
| 289 | * @throws AccessorExceptionInterface |
||
| 290 | */ |
||
| 291 | public function packFields(): array |
||
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritdoc} |
||
| 307 | * |
||
| 308 | * Include every composition public data into result. |
||
| 309 | */ |
||
| 310 | public function publicValue(): array |
||
| 311 | { |
||
| 312 | $result = []; |
||
| 313 | |||
| 314 | foreach ($this->getKeys() as $field) { |
||
| 315 | if (!$this->isPublic($field)) { |
||
| 316 | //We might need to use isset in future, for performance, for science |
||
| 317 | continue; |
||
| 318 | } |
||
| 319 | |||
| 320 | $value = $this->getField($field); |
||
| 321 | |||
| 322 | if ($value instanceof PublishableInterface) { |
||
| 323 | $result[$field] = $value->publicValue(); |
||
| 324 | } else { |
||
| 325 | $result[$field] = $value; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | return $result; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Alias for packFields. |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function toArray(): array |
||
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritdoc} |
||
| 344 | * |
||
| 345 | * By default use publicFields to be json serialized. |
||
| 346 | */ |
||
| 347 | public function jsonSerialize() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Destruct data entity. |
||
| 354 | */ |
||
| 355 | public function __destruct() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Reset every field value. |
||
| 362 | */ |
||
| 363 | protected function flushFields() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Indication that field in public and can be presented in published data. |
||
| 370 | * |
||
| 371 | * @param string $field |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | abstract protected function isPublic(string $field): bool; |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Check if field is fillable. |
||
| 379 | * |
||
| 380 | * @param string $field |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | abstract protected function isFillable(string $field): bool; |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get mutator associated with given field. |
||
| 388 | * |
||
| 389 | * @param string $field |
||
| 390 | * @param string $type See MUTATOR_* constants |
||
| 391 | * |
||
| 392 | * @return mixed |
||
| 393 | */ |
||
| 394 | abstract protected function getMutator(string $field, string $type); |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Nullable fields would not require automatic accessor creation. |
||
| 398 | * |
||
| 399 | * @param string $field |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | protected function isNullable(string $field): bool |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Create instance of field accessor. |
||
| 410 | * |
||
| 411 | * @param mixed|string $accessor Might be entity implementation specific. |
||
| 412 | * @param string $name |
||
| 413 | * @param mixed $value |
||
| 414 | * @param array $context Custom accessor context. |
||
| 415 | * |
||
| 416 | * @return AccessorInterface|null |
||
| 417 | * |
||
| 418 | * @throws AccessorExceptionInterface |
||
| 419 | * @throws EntityException |
||
| 420 | */ |
||
| 421 | protected function createAccessor( |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get value thought associated mutator. |
||
| 439 | * |
||
| 440 | * @param string $name |
||
| 441 | * @param bool $filter |
||
| 442 | * @param mixed $value |
||
| 443 | * |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | private function getMutated(string $name, bool $filter, $value) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Set value thought associated mutator. |
||
| 464 | * |
||
| 465 | * @param string $name |
||
| 466 | * @param mixed $value |
||
| 467 | */ |
||
| 468 | private function setMutated(string $name, $value) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Set value in/thought associated accessor. |
||
| 485 | * |
||
| 486 | * @param string $name |
||
| 487 | * @param string|array $accessor Accessor definition (implementation specific). |
||
| 488 | * @param mixed $value |
||
| 489 | */ |
||
| 490 | private function setAccessed($accessor, string $name, $value) |
||
| 509 | } |