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 |
||
23 | class RecordSchema implements SchemaInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var ReflectionEntity |
||
27 | */ |
||
28 | private $reflection; |
||
29 | |||
30 | /** |
||
31 | * @invisible |
||
32 | * @var MutatorsConfig |
||
33 | */ |
||
34 | private $mutatorsConfig; |
||
35 | |||
36 | /** |
||
37 | * @invisible |
||
38 | * @var ColumnRenderer |
||
39 | */ |
||
40 | private $renderer; |
||
41 | |||
42 | /** |
||
43 | * @param ReflectionEntity $reflection |
||
44 | * @param MutatorsConfig $mutators |
||
45 | * @param ColumnRenderer|null $rendered |
||
46 | */ |
||
47 | public function __construct( |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function getClass(): string |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function getRole(): string |
||
75 | |||
76 | /** |
||
77 | * @return ReflectionEntity |
||
78 | */ |
||
79 | public function getReflection(): ReflectionEntity |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function getInstantiator(): string |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function getDatabase() |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function getTable(): string |
||
120 | |||
121 | /** |
||
122 | * Fields and their types declared in Record model. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getFields(): array |
||
138 | |||
139 | /** |
||
140 | * Returns set of declared indexes. |
||
141 | * |
||
142 | * Example: |
||
143 | * const INDEXES = [ |
||
144 | * [self::UNIQUE, 'email'], |
||
145 | * [self::INDEX, 'status', 'balance'], |
||
146 | * [self::INDEX, 'public_id'] |
||
147 | * ]; |
||
148 | * |
||
149 | * @do generator |
||
150 | * |
||
151 | * @return \Generator|IndexDefinition[] |
||
152 | * |
||
153 | * @throws DefinitionException |
||
154 | */ |
||
155 | public function getIndexes(): \Generator |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function declareTable(AbstractTable $table): AbstractTable |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | public function getRelations(): \Generator |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function packSchema(SchemaBuilder $builder, AbstractTable $table): array |
||
228 | |||
229 | /** |
||
230 | * Generate set of default values to be used by record. |
||
231 | * |
||
232 | * @param AbstractTable $table |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | protected function packDefaults(AbstractTable $table): array |
||
257 | |||
258 | /** |
||
259 | * Generate set of mutators associated with entity fields using user defined and automatic |
||
260 | * mutators. |
||
261 | * |
||
262 | * @see MutatorsConfig |
||
263 | * |
||
264 | * @param AbstractTable $table |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | protected function buildMutators(AbstractTable $table): array |
||
295 | |||
296 | /** |
||
297 | * Check if field schema/type defines relation. |
||
298 | * |
||
299 | * @param mixed $type |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | protected function isRelation($type): bool |
||
311 | |||
312 | /** |
||
313 | * @param array $definition |
||
314 | * |
||
315 | * @return IndexDefinition |
||
316 | * |
||
317 | * @throws DefinitionException |
||
318 | */ |
||
319 | protected function castIndex(array $definition) |
||
347 | |||
348 | /** |
||
349 | * Default defined values. |
||
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | protected function getDefaults(): array |
||
358 | |||
359 | /** |
||
360 | * Process value thought associated mutator if any. |
||
361 | * |
||
362 | * @param array $mutators |
||
363 | * @param string $field |
||
364 | * @param mixed $default |
||
365 | * |
||
366 | * @return mixed |
||
367 | */ |
||
368 | protected function mutateValue(array $mutators, string $field, $default) |
||
393 | |||
394 | /** |
||
395 | * Pass value thought accessor to ensure it's default. |
||
396 | * |
||
397 | * @param mixed $default |
||
398 | * @param string $accessor |
||
399 | * |
||
400 | * @return mixed |
||
401 | * |
||
402 | * @throws AccessorExceptionInterface |
||
403 | */ |
||
404 | protected function accessorDefault($default, string $accessor) |
||
419 | } |