Complex classes like DocumentCompositor 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 DocumentCompositor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DocumentCompositor implements CompositableInterface, \Countable, \IteratorAggregate |
||
| 24 | { |
||
| 25 | use SolidableTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Lazy conversion from array to CompositableInterface (ie DocumentEntity). |
||
| 29 | * |
||
| 30 | * @var CompositableInterface[] |
||
| 31 | */ |
||
| 32 | private $entities = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set of atomic operation applied to whole composition set. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $atomics = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $class; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @invisible |
||
| 48 | * @var ODMInterface |
||
| 49 | */ |
||
| 50 | protected $odm; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $class |
||
| 54 | * @param array|CompositableInterface[] $data |
||
| 55 | * @param ODMInterface $odm |
||
| 56 | */ |
||
| 57 | public function __construct(string $class, array $data, ODMInterface $odm) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get primary composition class. |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getClass(): string |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Push new entity to end of set. |
||
| 78 | * |
||
| 79 | * Be aware that any added entity will be cloned in order to detach it from passed object: |
||
| 80 | * $user->addresses->push($address); |
||
| 81 | * $address->city = 'Minsk'; //this will have no effect of $user->addresses |
||
| 82 | * |
||
| 83 | * @param CompositableInterface $entity |
||
| 84 | * |
||
| 85 | * @return DocumentCompositor |
||
| 86 | * |
||
| 87 | * @throws CompositorException When entity is invalid type. |
||
| 88 | */ |
||
| 89 | public function push(CompositableInterface $entity): DocumentCompositor |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add entity to set, only one instance of document must be presented. |
||
| 104 | * |
||
| 105 | * Be aware that any added entity will be cloned in order to detach it from passed object: |
||
| 106 | * $user->addresses->add($address); |
||
| 107 | * $address->city = 'Minsk'; //this will have no effect of $user->addresses |
||
| 108 | * |
||
| 109 | * @param CompositableInterface $entity |
||
| 110 | * |
||
| 111 | * @return DocumentCompositor |
||
| 112 | * |
||
| 113 | * @throws CompositorException When entity is invalid type. |
||
| 114 | */ |
||
| 115 | public function add(CompositableInterface $entity): DocumentCompositor |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Pull mathced entities from composition. |
||
| 133 | * |
||
| 134 | * $user->addresses->pull($address); |
||
| 135 | * |
||
| 136 | * @param CompositableInterface $entity |
||
| 137 | * |
||
| 138 | * @return DocumentCompositor |
||
| 139 | */ |
||
| 140 | public function pull(CompositableInterface $entity): DocumentCompositor |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Check if composition contains desired document or document matching query. |
||
| 156 | * |
||
| 157 | * Example: |
||
| 158 | * $user->cards->has(['active' => true]); |
||
| 159 | * $user->cards->has(new Card(...)); |
||
| 160 | * |
||
| 161 | * @param CompositableInterface|array $query |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function has($query): bool |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Find document in composition based on given entity or matching query. |
||
| 172 | * |
||
| 173 | * $user->cards->findOne(['active' => true]); |
||
| 174 | * $user->cards->findOne(new Card(...)); |
||
| 175 | * |
||
| 176 | * @param CompositableInterface|array $query |
||
| 177 | * |
||
| 178 | * @return CompositableInterface|null |
||
| 179 | */ |
||
| 180 | public function findOne($query) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Find all entities matching given query (query can be provided in a form of |
||
| 192 | * CompositableInterface). |
||
| 193 | * |
||
| 194 | * $user->cards->find(['active' => true]); |
||
| 195 | * $user->cards->find(new Card(...)); //Attention, this will likely to return only on match |
||
| 196 | * |
||
| 197 | * @param CompositableInterface|array $query |
||
| 198 | * @param bool $preserveKeys Set to true to keep original offsets. |
||
| 199 | * |
||
| 200 | * @return CompositableInterface[] |
||
| 201 | */ |
||
| 202 | public function find($query, bool $preserveKeys = false): array |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | * |
||
| 227 | * Be aware that any added entity will be cloned in order to detach it from passed object: |
||
| 228 | * $user->addresses->mountValue([$address]); |
||
| 229 | * $address->city = 'Minsk'; //this will have no effect of $user->addresses |
||
| 230 | */ |
||
| 231 | public function setValue($data) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function packValue(): array |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | * |
||
| 257 | * @param bool $changedEntities Reference, will be set to true if any of entities changed |
||
| 258 | * internally. |
||
| 259 | */ |
||
| 260 | public function hasChanges(bool &$changedEntities = null): bool |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function flushChanges() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function buildAtomics(string $container = ''): array |
||
| 333 | |||
| 334 | /** |
||
| 335 | * {@inheritdoc} |
||
| 336 | */ |
||
| 337 | public function jsonSerialize() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return int |
||
| 344 | */ |
||
| 345 | public function count(): int |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return \ArrayIterator |
||
| 352 | */ |
||
| 353 | public function getIterator(): \ArrayIterator |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Cloning will be called when object will be embedded into another document. |
||
| 360 | */ |
||
| 361 | public function __clone() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function __debugInfo() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Assert that given entity supported by composition. |
||
| 383 | * |
||
| 384 | * @param CompositableInterface $entity |
||
| 385 | * |
||
| 386 | * @throws CompositorException |
||
| 387 | */ |
||
| 388 | protected function assertSupported(CompositableInterface $entity) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Instantiate every entity in composition. |
||
| 401 | * |
||
| 402 | * @param array $data |
||
| 403 | * @param bool $filter |
||
| 404 | * |
||
| 405 | * @return CompositableInterface[] |
||
| 406 | * |
||
| 407 | * @throws CompositorException |
||
| 408 | */ |
||
| 409 | private function createEntities(array $data, bool $filter = true): array |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Pack multiple entities into array form. |
||
| 428 | * |
||
| 429 | * @param CompositableInterface[] $entities |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | private function packValues(array $entities): array |
||
| 442 | } |