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 |
||
24 | CompositableInterface, |
||
25 | PublishableInterface, |
||
26 | \Countable, |
||
27 | \IteratorAggregate |
||
28 | { |
||
29 | use SolidableTrait; |
||
30 | |||
31 | /** |
||
32 | * Lazy conversion from array to CompositableInterface (ie DocumentEntity). |
||
33 | * |
||
34 | * @var CompositableInterface[] |
||
35 | */ |
||
36 | private $entities = []; |
||
37 | |||
38 | /** |
||
39 | * Set of atomic operation applied to whole composition set. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $atomics = []; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $class; |
||
49 | |||
50 | /** |
||
51 | * @invisible |
||
52 | * @var ODMInterface |
||
53 | */ |
||
54 | protected $odm; |
||
55 | |||
56 | /** |
||
57 | * @param string $class |
||
58 | * @param array|CompositableInterface[] $data |
||
59 | * @param ODMInterface $odm |
||
60 | */ |
||
61 | public function __construct(string $class, array $data, ODMInterface $odm) |
||
69 | |||
70 | /** |
||
71 | * Get primary composition class. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getClass(): string |
||
79 | |||
80 | /** |
||
81 | * Push new entity to end of set. |
||
82 | * |
||
83 | * Be aware that any added entity will be cloned in order to detach it from passed object: |
||
84 | * $user->addresses->push($address); |
||
85 | * $address->city = 'Minsk'; //this will have no effect of $user->addresses |
||
86 | * |
||
87 | * @param CompositableInterface $entity |
||
88 | * |
||
89 | * @return DocumentCompositor |
||
90 | * |
||
91 | * @throws CompositorException When entity is invalid type. |
||
92 | */ |
||
93 | public function push(CompositableInterface $entity): DocumentCompositor |
||
105 | |||
106 | /** |
||
107 | * Add entity to set, only one instance of document must be presented. |
||
108 | * |
||
109 | * Be aware that any added entity will be cloned in order to detach it from passed object: |
||
110 | * $user->addresses->add($address); |
||
111 | * $address->city = 'Minsk'; //this will have no effect of $user->addresses |
||
112 | * |
||
113 | * @param CompositableInterface $entity |
||
114 | * |
||
115 | * @return DocumentCompositor |
||
116 | * |
||
117 | * @throws CompositorException When entity is invalid type. |
||
118 | */ |
||
119 | public function add(CompositableInterface $entity): DocumentCompositor |
||
134 | |||
135 | /** |
||
136 | * Pull mathced entities from composition. |
||
137 | * |
||
138 | * $user->addresses->pull($address); |
||
139 | * |
||
140 | * @param CompositableInterface $entity |
||
141 | * |
||
142 | * @return DocumentCompositor |
||
143 | */ |
||
144 | public function pull(CompositableInterface $entity): DocumentCompositor |
||
157 | |||
158 | /** |
||
159 | * Check if composition contains desired document or document matching query. |
||
160 | * |
||
161 | * Example: |
||
162 | * $user->cards->has(['active' => true]); |
||
163 | * $user->cards->has(new Card(...)); |
||
164 | * |
||
165 | * @param CompositableInterface|array $query |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function has($query): bool |
||
173 | |||
174 | /** |
||
175 | * Find document in composition based on given entity or matching query. |
||
176 | * |
||
177 | * $user->cards->findOne(['active' => true]); |
||
178 | * $user->cards->findOne(new Card(...)); |
||
179 | * |
||
180 | * @param CompositableInterface|array $query |
||
181 | * |
||
182 | * @return CompositableInterface|null |
||
183 | */ |
||
184 | public function findOne($query) |
||
193 | |||
194 | /** |
||
195 | * Find all entities matching given query (query can be provided in a form of |
||
196 | * CompositableInterface). |
||
197 | * |
||
198 | * $user->cards->find(['active' => true]); |
||
199 | * $user->cards->find(new Card(...)); //Attention, this will likely to return only on match |
||
200 | * |
||
201 | * @param CompositableInterface|array $query |
||
202 | * @param bool $preserveKeys Set to true to keep original offsets. |
||
203 | * |
||
204 | * @return CompositableInterface[] |
||
205 | */ |
||
206 | public function find($query, bool $preserveKeys = false): array |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | * |
||
231 | * Be aware that any added entity will be cloned in order to detach it from passed object: |
||
232 | * $user->addresses->mountValue([$address]); |
||
233 | * $address->city = 'Minsk'; //this will have no effect of $user->addresses |
||
234 | */ |
||
235 | public function stateValue($data) |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | public function packValue(): array |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | * |
||
261 | * @param bool $changedEntities Reference, will be set to true if any of entities changed |
||
262 | * internally. |
||
263 | */ |
||
264 | public function hasChanges(bool &$changedEntities = null): bool |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function flushChanges() |
||
281 | { |
||
282 | $this->atomics = []; |
||
283 | |||
284 | foreach ($this->entities as $entity) { |
||
285 | $entity->flushChanges(); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | public function buildAtomics(string $container = ''): array |
||
337 | |||
338 | /** |
||
339 | * Packs only public values of all nested documents. |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | public function publicValue(): array |
||
354 | |||
355 | /** |
||
356 | * {@inheritdoc} |
||
357 | */ |
||
358 | public function jsonSerialize() |
||
362 | |||
363 | /** |
||
364 | * @return int |
||
365 | */ |
||
366 | public function count(): int |
||
370 | |||
371 | /** |
||
372 | * @return \ArrayIterator |
||
373 | */ |
||
374 | public function getIterator(): \ArrayIterator |
||
378 | |||
379 | /** |
||
380 | * Cloning will be called when object will be embedded into another document. |
||
381 | */ |
||
382 | public function __clone() |
||
390 | |||
391 | /** |
||
392 | * @return array |
||
393 | */ |
||
394 | public function __debugInfo() |
||
401 | |||
402 | /** |
||
403 | * Assert that given entity supported by composition. |
||
404 | * |
||
405 | * @param CompositableInterface $entity |
||
406 | * |
||
407 | * @throws CompositorException |
||
408 | */ |
||
409 | protected function assertSupported(CompositableInterface $entity) |
||
419 | |||
420 | /** |
||
421 | * Instantiate every entity in composition. |
||
422 | * |
||
423 | * @param array $data |
||
424 | * @param bool $filter |
||
425 | * |
||
426 | * @return CompositableInterface[] |
||
427 | * |
||
428 | * @throws CompositorException |
||
429 | */ |
||
430 | private function createEntities(array $data, bool $filter = true): array |
||
446 | |||
447 | /** |
||
448 | * Pack multiple entities into array form. |
||
449 | * |
||
450 | * @param CompositableInterface[] $entities |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | private function packValues(array $entities): array |
||
463 | } |