Complex classes like RecordSchema 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 RecordSchema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class RecordSchema implements SchemaInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var ReflectionEntity |
||
25 | */ |
||
26 | private $reflection; |
||
27 | |||
28 | /** |
||
29 | * @invisible |
||
30 | * @var MutatorsConfig |
||
31 | */ |
||
32 | private $mutatorsConfig; |
||
33 | |||
34 | /** |
||
35 | * @var ColumnRenderer |
||
36 | */ |
||
37 | private $renderer; |
||
38 | |||
39 | /** |
||
40 | * @param ReflectionEntity $reflection |
||
41 | * @param MutatorsConfig $mutators |
||
42 | * @param ColumnRenderer|null $rendered |
||
43 | */ |
||
44 | public function __construct( |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getClass(): string |
||
61 | |||
62 | /** |
||
63 | * @return ReflectionEntity |
||
64 | */ |
||
65 | public function getReflection(): ReflectionEntity |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function getInstantiator(): string |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function getDatabase() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function getTable(): string |
||
106 | |||
107 | /** |
||
108 | * Fields and their types declared in Record model. |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getFields(): array |
||
124 | |||
125 | /** |
||
126 | * Returns set of declared indexes. |
||
127 | * |
||
128 | * Example: |
||
129 | * const INDEXES = [ |
||
130 | * [self::UNIQUE, 'email'], |
||
131 | * [self::INDEX, 'status', 'balance'], |
||
132 | * [self::INDEX, 'public_id'] |
||
133 | * ]; |
||
134 | * |
||
135 | * @do generator |
||
136 | * |
||
137 | * @return \Generator|IndexDefinition[] |
||
138 | * |
||
139 | * @throws DefinitionException |
||
140 | */ |
||
141 | public function getIndexes(): \Generator |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function renderTable(AbstractTable $table): AbstractTable |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getRelations(): array |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function packSchema(SchemaBuilder $builder, AbstractTable $table): array |
||
191 | |||
192 | /** |
||
193 | * Generate set of default values to be used by record. |
||
194 | * |
||
195 | * @param AbstractTable $table |
||
196 | * @param array $overwriteDefaults |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function packDefaults(AbstractTable $table, array $overwriteDefaults = []): array |
||
256 | |||
257 | /** |
||
258 | * Generate set of mutators associated with entity fields using user defined and automatic |
||
259 | * mutators. |
||
260 | * |
||
261 | * @see MutatorsConfig |
||
262 | * |
||
263 | * @param AbstractTable $table |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | protected function buildMutators(AbstractTable $table): array |
||
294 | |||
295 | /** |
||
296 | * Check if field schema/type defines relation. |
||
297 | * |
||
298 | * @param mixed $type |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | protected function isRelation($type): bool |
||
310 | |||
311 | /** |
||
312 | * @param array $definition |
||
313 | * |
||
314 | * @return IndexDefinition |
||
315 | * |
||
316 | * @throws DefinitionException |
||
317 | */ |
||
318 | protected function castIndex(array $definition) |
||
346 | |||
347 | /** |
||
348 | * Default defined values. |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | protected function getDefaults(): array |
||
357 | |||
358 | |||
359 | /** |
||
360 | * Pass value thought accessor to ensure it's default. |
||
361 | * |
||
362 | * @param mixed $default |
||
363 | * @param string $accessor |
||
364 | * |
||
365 | * @return mixed |
||
366 | * |
||
367 | * @throws AccessorExceptionInterface |
||
368 | */ |
||
369 | protected function accessorDefault($default, string $accessor) |
||
384 | } |