Complex classes like DocumentSchema 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 DocumentSchema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class DocumentSchema implements SchemaInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var ReflectionEntity |
||
27 | */ |
||
28 | private $reflection; |
||
29 | |||
30 | /** |
||
31 | * @invisible |
||
32 | * |
||
33 | * @var MutatorsConfig |
||
34 | */ |
||
35 | private $mutatorsConfig; |
||
36 | |||
37 | /** |
||
38 | * @param ReflectionEntity $reflection |
||
39 | * @param MutatorsConfig $mutators |
||
40 | */ |
||
41 | public function __construct(ReflectionEntity $reflection, MutatorsConfig $mutators) |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | public function getClass(): string |
||
54 | |||
55 | /** |
||
56 | * @return ReflectionEntity |
||
57 | */ |
||
58 | public function getReflection(): ReflectionEntity |
||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | public function getInstantiator(): string |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function isEmbedded(): bool |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function getDatabase() |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getCollection(): string |
||
126 | |||
127 | /** |
||
128 | * Get every embedded entity field (excluding declarations of aggregations). |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function getFields(): array |
||
144 | |||
145 | /** |
||
146 | * Default defined values. |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function getDefaults(): array |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function getIndexes(): array |
||
184 | |||
185 | /** |
||
186 | * @return AggregationDefinition[] |
||
187 | */ |
||
188 | public function getAggregations(): array |
||
205 | |||
206 | /** |
||
207 | * Find all composition definitions, attention method require builder instance in order to |
||
208 | * properly check that embedded class exists. |
||
209 | * |
||
210 | * @param SchemaBuilder $builder |
||
211 | * |
||
212 | * @return CompositionDefinition[] |
||
213 | */ |
||
214 | public function getCompositions(SchemaBuilder $builder): array |
||
229 | |||
230 | /** |
||
231 | * Generate set of mutators associated with entity fields using user defined and automatic |
||
232 | * mutators. |
||
233 | * |
||
234 | * @see MutatorsConfig |
||
235 | * @return array |
||
236 | */ |
||
237 | public function getMutators(): array |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function resolvePrimary(SchemaBuilder $builder): string |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function packSchema(SchemaBuilder $builder): array |
||
308 | |||
309 | /** |
||
310 | * Define instantiator specific options (usually needed to resolve class inheritance). Might |
||
311 | * return null if associated instantiator is unknown to DocumentSchema. |
||
312 | * |
||
313 | * @param SchemaBuilder $builder |
||
314 | * |
||
315 | * @return mixed |
||
316 | */ |
||
317 | protected function instantiationOptions(SchemaBuilder $builder) |
||
329 | |||
330 | /** |
||
331 | * Entity default values. |
||
332 | * |
||
333 | * @param SchemaBuilder $builder |
||
334 | * @param array $overwriteDefaults Set of default values to replace user defined values. |
||
335 | * |
||
336 | * @return array |
||
337 | * |
||
338 | * @throws SchemaException |
||
339 | */ |
||
340 | protected function packDefaults(SchemaBuilder $builder, array $overwriteDefaults = []): array |
||
373 | |||
374 | /** |
||
375 | * Pack compositions into simple array definition. |
||
376 | * |
||
377 | * @param SchemaBuilder $builder |
||
378 | * |
||
379 | * @return array |
||
380 | * |
||
381 | * @throws SchemaException |
||
382 | */ |
||
383 | public function packCompositions(SchemaBuilder $builder): array |
||
392 | |||
393 | /** |
||
394 | * Pack aggregations into simple array definition. |
||
395 | * |
||
396 | * @param SchemaBuilder $builder |
||
397 | * |
||
398 | * @return array |
||
399 | * |
||
400 | * @throws SchemaException |
||
401 | */ |
||
402 | protected function packAggregations(SchemaBuilder $builder): array |
||
423 | |||
424 | /** |
||
425 | * Check if field schema/type defines aggregation. |
||
426 | * |
||
427 | * @param mixed $type |
||
428 | * |
||
429 | * @return bool |
||
430 | */ |
||
431 | protected function isAggregation($type): bool |
||
441 | |||
442 | /** |
||
443 | * Ensure default value using associated mutators or pass thought composition. |
||
444 | * |
||
445 | * @param SchemaBuilder $builder |
||
446 | * @param array $compositions |
||
447 | * @param array $userDefined User defined set of default values. |
||
448 | * @param array $mutators |
||
449 | * @param string $field |
||
450 | * @param mixed $default |
||
451 | * |
||
452 | * @return mixed |
||
453 | */ |
||
454 | protected function mutateValue( |
||
494 | |||
495 | /** |
||
496 | * Pass value thought accessor to ensure it's default. |
||
497 | * |
||
498 | * @param mixed $default |
||
499 | * @param string $accessor |
||
500 | * |
||
501 | * @return mixed |
||
502 | * |
||
503 | * @throws AccessorExceptionInterface |
||
504 | */ |
||
505 | protected function accessorDefault($default, string $accessor) |
||
520 | |||
521 | /** |
||
522 | * Ensure default value for composite field, |
||
523 | * |
||
524 | * @param mixed $default |
||
525 | * @param CompositionDefinition $composition |
||
526 | * @param SchemaBuilder $builder |
||
527 | * |
||
528 | * @return array |
||
529 | * |
||
530 | * @throws SchemaException |
||
531 | */ |
||
532 | protected function compositionDefault( |
||
571 | } |