Complex classes like SchemaBuilder 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 SchemaBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 23 | class SchemaBuilder | ||
| 24 | { | ||
| 25 | /** | ||
| 26 | * @var DatabaseManager | ||
| 27 | */ | ||
| 28 | private $manager; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var RelationBuilder | ||
| 32 | */ | ||
| 33 | private $relations; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @var AbstractTable[] | ||
| 37 | */ | ||
| 38 | private $tables = []; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @var SchemaInterface[] | ||
| 42 | */ | ||
| 43 | private $schemas = []; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Class names of sources associated with specific class. | ||
| 47 | * | ||
| 48 | * @var array | ||
| 49 | */ | ||
| 50 | private $sources = []; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @param DatabaseManager $manager | ||
| 54 | * @param RelationBuilder $relations | ||
| 55 | */ | ||
| 56 | public function __construct(DatabaseManager $manager, RelationBuilder $relations) | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Add new model schema into pool. | ||
| 64 | * | ||
| 65 | * @param SchemaInterface $schema | ||
| 66 | * | ||
| 67 | * @return self|$this | ||
| 68 | */ | ||
| 69 | public function addSchema(SchemaInterface $schema): SchemaBuilder | ||
| 75 | |||
| 76 | /** | ||
| 77 | * @param string $class | ||
| 78 | * | ||
| 79 | * @return bool | ||
| 80 | */ | ||
| 81 | public function hasSchema(string $class): bool | ||
| 85 | |||
| 86 | /** | ||
| 87 | * @param string $class | ||
| 88 | * | ||
| 89 | * @return SchemaInterface | ||
| 90 | * | ||
| 91 | * @throws SchemaException | ||
| 92 | */ | ||
| 93 | public function getSchema(string $class): SchemaInterface | ||
| 101 | |||
| 102 | /** | ||
| 103 | * All available document schemas. | ||
| 104 | * | ||
| 105 | * @return SchemaInterface[] | ||
| 106 | */ | ||
| 107 | public function getSchemas(): array | ||
| 111 | |||
| 112 | /** | ||
| 113 | * Associate source class with entity class. Source will be automatically associated with given | ||
| 114 | * class and all classes from the same collection which extends it. | ||
| 115 | * | ||
| 116 | * @param string $class | ||
| 117 | * @param string $source | ||
| 118 | * | ||
| 119 | * @return SchemaBuilder | ||
| 120 | * | ||
| 121 | * @throws SchemaException | ||
| 122 | */ | ||
| 123 | public function addSource(string $class, string $source): SchemaBuilder | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Check if given entity has associated source. | ||
| 136 | * | ||
| 137 | * @param string $class | ||
| 138 | * | ||
| 139 | * @return bool | ||
| 140 | */ | ||
| 141 | public function hasSource(string $class): bool | ||
| 145 | |||
| 146 | /** | ||
| 147 | * Get source associated with specific class, if any. | ||
| 148 | * | ||
| 149 | * @param string $class | ||
| 150 | * | ||
| 151 | * @return string|null | ||
| 152 | */ | ||
| 153 | public function getSource(string $class) | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Get all associated sources. | ||
| 164 | * | ||
| 165 | * @return array | ||
| 166 | */ | ||
| 167 | public function getSources(): array | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Get all created relations. | ||
| 174 | * | ||
| 175 | * @return RelationInterface[] | ||
| 176 | */ | ||
| 177 | public function getRelations(): array | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Process all added schemas and relations in order to created needed tables, indexes and etc. | ||
| 184 | * Attention, this method will return new instance of SchemaBuilder without affecting original | ||
| 185 | * object. You MUST call this method before calling packSchema() method. | ||
| 186 | * | ||
| 187 | * Attention, this methods DOES NOT write anything into database, use pushSchema() to push | ||
| 188 | * changes into database using automatic diff generation. You can also access list of | ||
| 189 | * generated/changed tables via getTables() to create your own migrations. | ||
| 190 | * | ||
| 191 | * @see packSchema() | ||
| 192 | * @see pushSchema() | ||
| 193 | * @see getTables() | ||
| 194 | * | ||
| 195 | * @return SchemaBuilder | ||
| 196 | * | ||
| 197 | * @throws SchemaException | ||
| 198 | */ | ||
| 199 | public function renderSchema(): SchemaBuilder | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Request table schema by name/database combination. Attention, you can only save table by | ||
| 220 | * pushing it back using pushTable() method. | ||
| 221 | * | ||
| 222 | * @see pushTable() | ||
| 223 | * | ||
| 224 | * @param string $table | ||
| 225 | * @param string|null $database | ||
| 226 | * @param bool $resetState When set to true current table state will be reset in order | ||
| 227 | * to allow model to redefine it's schema. | ||
| 228 | * @param bool $unique Set to true (default), to throw an exception when table | ||
| 229 | * already referenced by another model. | ||
| 230 | * | ||
| 231 | * @return AbstractTable Unlinked. | ||
| 232 | * | ||
| 233 | * @throws DoubleReferenceException When two records refers to same table and unique option | ||
| 234 | * set. | ||
| 235 | */ | ||
| 236 | public function requestTable( | ||
| 267 | |||
| 268 | /** | ||
| 269 | * Get all defined tables, make sure to call renderSchema() first. Attention, all given tables | ||
| 270 | * will be returned in detached state. | ||
| 271 | * | ||
| 272 | * @return AbstractTable[] | ||
| 273 | * | ||
| 274 | * @throws SchemaException | ||
| 275 | */ | ||
| 276 | public function getTables(): array | ||
| 292 | |||
| 293 | /** | ||
| 294 | * Indication that tables in database require syncing before being matched with ORM models. | ||
| 295 | * | ||
| 296 | * @return bool | ||
| 297 | */ | ||
| 298 | public function hasChanges(): bool | ||
| 308 | |||
| 309 | /** | ||
| 310 | * Save every change made to generated tables. Method utilizes default DBAL diff mechanism, | ||
| 311 | * use getTables() method in order to generate your own migrations. | ||
| 312 | * | ||
| 313 | * @param LoggerInterface|null $logger | ||
| 314 | * | ||
| 315 | * @throws SchemaException | ||
| 316 | * @throws DBALException | ||
| 317 | * @throws QueryException | ||
| 318 | * @throws DriverException | ||
| 319 | */ | ||
| 320 | public function pushSchema(LoggerInterface $logger = null) | ||
| 325 | |||
| 326 | /** | ||
| 327 | * Pack declared schemas in a normalized form, make sure to call renderSchema() first. | ||
| 328 | * | ||
| 329 | * @return array | ||
| 330 | * | ||
| 331 | * @throws SchemaException | ||
| 332 | */ | ||
| 333 | public function packSchema(): array | ||
| 373 | |||
| 374 | /** | ||
| 375 | * Walk thought schemas and define structure of associated tables. | ||
| 376 | * | ||
| 377 | * @throws SchemaException | ||
| 378 | * @throws DBALException | ||
| 379 | */ | ||
| 380 | protected function renderModels() | ||
| 426 | |||
| 427 | /** | ||
| 428 | * Walk thought all record schemas, fetch and declare needed relations using relation manager. | ||
| 429 | * | ||
| 430 | * @throws SchemaException | ||
| 431 | * @throws DefinitionException | ||
| 432 | */ | ||
| 433 | protected function renderRelations() | ||
| 464 | |||
| 465 | /** | ||
| 466 | * Update table state. | ||
| 467 | * | ||
| 468 | * @param AbstractTable $schema | ||
| 469 | * | ||
| 470 | * @throws SchemaException | ||
| 471 | */ | ||
| 472 | protected function pushTable(AbstractTable $schema) | ||
| 483 | } |