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 |
||
22 | class DocumentSchema implements SchemaInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var ReflectionEntity |
||
26 | */ |
||
27 | private $reflection; |
||
28 | |||
29 | /** |
||
30 | * @invisible |
||
31 | * |
||
32 | * @var MutatorsConfig |
||
33 | */ |
||
34 | private $mutators; |
||
35 | |||
36 | /** |
||
37 | * @param ReflectionEntity $reflection |
||
38 | * @param MutatorsConfig $config |
||
39 | */ |
||
40 | public function __construct(ReflectionEntity $reflection, MutatorsConfig $config) |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getClass(): string |
||
53 | |||
54 | /** |
||
55 | * @return ReflectionEntity |
||
56 | */ |
||
57 | public function getReflection(): ReflectionEntity |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getInstantiator(): string |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function isEmbedded(): bool |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function getDatabase() |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function getCollection(): string |
||
119 | |||
120 | /** |
||
121 | * Get every embedded entity field (excluding declarations of aggregations). |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getFields(): array |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function getIndexes(): array |
||
167 | |||
168 | /** |
||
169 | * @return AggregationDefinition[] |
||
170 | */ |
||
171 | public function getAggregations(): array |
||
188 | |||
189 | /** |
||
190 | * Find all composition definitions, attention method require builder instance in order to |
||
191 | * properly check that embedded class exists. |
||
192 | * |
||
193 | * @param SchemaBuilder $builder |
||
194 | * |
||
195 | * @return CompositionDefinition[] |
||
196 | */ |
||
197 | public function getCompositions(SchemaBuilder $builder): array |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function resolvePrimary(SchemaBuilder $builder): string |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function packSchema(SchemaBuilder $builder): array |
||
249 | |||
250 | /** |
||
251 | * Define instantiator specific options (usually needed to resolve class inheritance). Might |
||
252 | * return null if associated instantiator is unknown to DocumentSchema. |
||
253 | * |
||
254 | * @param SchemaBuilder $builder |
||
255 | * |
||
256 | * @return mixed |
||
257 | */ |
||
258 | protected function instantiationOptions(SchemaBuilder $builder) |
||
270 | |||
271 | /** |
||
272 | * Entity default values. |
||
273 | * |
||
274 | * @param SchemaBuilder $builder |
||
275 | * @param array $overwriteDefaults Set of default values to replace user defined values. |
||
276 | * |
||
277 | * @return array |
||
278 | * |
||
279 | * @throws SchemaException |
||
280 | */ |
||
281 | protected function packDefaults(SchemaBuilder $builder, array $overwriteDefaults = []): array |
||
338 | |||
339 | /** |
||
340 | * Generate set of mutators associated with entity fields using user defined and automatic |
||
341 | * mutators. |
||
342 | * |
||
343 | * @see MutatorsConfig |
||
344 | * @return array |
||
345 | */ |
||
346 | protected function packMutators(): array |
||
380 | |||
381 | /** |
||
382 | * Pack compositions into simple array definition. |
||
383 | * |
||
384 | * @param SchemaBuilder $builder |
||
385 | * |
||
386 | * @return array |
||
387 | * |
||
388 | * @throws SchemaException |
||
389 | */ |
||
390 | public function packCompositions(SchemaBuilder $builder): array |
||
399 | |||
400 | /** |
||
401 | * Pack aggregations into simple array definition. |
||
402 | * |
||
403 | * @param SchemaBuilder $builder |
||
404 | * |
||
405 | * @return array |
||
406 | * |
||
407 | * @throws SchemaException |
||
408 | */ |
||
409 | protected function packAggregations(SchemaBuilder $builder): array |
||
430 | |||
431 | /** |
||
432 | * Check if field schema/type defines aggregation. |
||
433 | * |
||
434 | * @param mixed $type |
||
435 | * |
||
436 | * @return bool |
||
437 | */ |
||
438 | protected function isAggregation($type): bool |
||
448 | |||
449 | /** |
||
450 | * Pass value thought accessor to ensure it's default. |
||
451 | * |
||
452 | * @param mixed $default |
||
453 | * @param string $accessor |
||
454 | * |
||
455 | * @return mixed |
||
456 | * |
||
457 | * @throws AccessorException |
||
458 | */ |
||
459 | protected function accessorDefault($default, string $accessor) |
||
474 | |||
475 | /** |
||
476 | * Ensure default value for composite field, |
||
477 | * |
||
478 | * @param mixed $default |
||
479 | * @param CompositionDefinition $composition |
||
480 | * @param SchemaBuilder $builder |
||
481 | * |
||
482 | * @return array |
||
483 | * |
||
484 | * @throws SchemaException |
||
485 | */ |
||
486 | protected function compositionDefault( |
||
525 | } |