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 |
||
| 26 | class RecordSchema implements SchemaInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var ReflectionEntity |
||
| 30 | */ |
||
| 31 | private $reflection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @invisible |
||
| 35 | * @var MutatorsConfig |
||
| 36 | */ |
||
| 37 | private $mutatorsConfig; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @invisible |
||
| 41 | * @var ColumnRenderer |
||
| 42 | */ |
||
| 43 | private $renderer; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param ReflectionEntity $reflection |
||
| 47 | * @param MutatorsConfig $mutators |
||
| 48 | * @param ColumnRenderer|null $rendered |
||
| 49 | */ |
||
| 50 | public function __construct( |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | public function getClass(): string |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function getRole(): string |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return ReflectionEntity |
||
| 81 | */ |
||
| 82 | public function getReflection(): ReflectionEntity |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function getInstantiator(): string |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function getDatabase() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function getTable(): string |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Fields and their types declared in Record model. |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function getFields(): array |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns set of declared indexes. |
||
| 144 | * |
||
| 145 | * Example: |
||
| 146 | * const INDEXES = [ |
||
| 147 | * [self::UNIQUE, 'email'], |
||
| 148 | * [self::INDEX, 'status', 'balance'], |
||
| 149 | * [self::INDEX, 'public_id'] |
||
| 150 | * ]; |
||
| 151 | * |
||
| 152 | * @do generator |
||
| 153 | * |
||
| 154 | * @return \Generator|IndexDefinition[] |
||
| 155 | * |
||
| 156 | * @throws DefinitionException |
||
| 157 | */ |
||
| 158 | public function getIndexes(): \Generator |
||
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | public function declareTable(AbstractTable $table): AbstractTable |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | */ |
||
| 182 | public function getRelations(): \Generator |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | public function packSchema(SchemaBuilder $builder, AbstractTable $table): array |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Generate set of default values to be used by record. |
||
| 233 | * |
||
| 234 | * @param AbstractTable $table |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function packDefaults(AbstractTable $table): array |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Generate set of mutators associated with entity fields using user defined and automatic |
||
| 262 | * mutators. |
||
| 263 | * |
||
| 264 | * @see MutatorsConfig |
||
| 265 | * |
||
| 266 | * @param AbstractTable $table |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected function buildMutators(AbstractTable $table): array |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Check if field schema/type defines relation. |
||
| 310 | * |
||
| 311 | * @param mixed $type |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function isRelation($type): bool |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param array $definition |
||
| 326 | * |
||
| 327 | * @return IndexDefinition |
||
| 328 | * |
||
| 329 | * @throws DefinitionException |
||
| 330 | */ |
||
| 331 | protected function castIndex(array $definition) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Default defined values. |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | protected function getDefaults(): array |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Process value thought associated mutator if any. |
||
| 389 | * |
||
| 390 | * @param array $mutators |
||
| 391 | * @param string $field |
||
| 392 | * @param mixed $default |
||
| 393 | * |
||
| 394 | * @return mixed |
||
| 395 | */ |
||
| 396 | protected function mutateValue(array $mutators, string $field, $default) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Pass value thought accessor to ensure it's default. |
||
| 424 | * |
||
| 425 | * @param mixed $default |
||
| 426 | * @param string $accessor |
||
| 427 | * |
||
| 428 | * @return mixed |
||
| 429 | * |
||
| 430 | * @throws AccessException |
||
| 431 | */ |
||
| 432 | protected function accessorDefault($default, string $accessor) |
||
| 447 | } |