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( |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get all defined tables, make sure to call renderSchema() first. Attention, all given tables |
||
| 269 | * will be returned in detached state. |
||
| 270 | * |
||
| 271 | * @return AbstractTable[] |
||
| 272 | * |
||
| 273 | * @throws SchemaException |
||
| 274 | */ |
||
| 275 | public function getTables(): array |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Indication that tables in database require syncing before being matched with ORM models. |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function hasChanges(): bool |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Save every change made to generated tables. Method utilizes default DBAL diff mechanism, |
||
| 310 | * use getTables() method in order to generate your own migrations. |
||
| 311 | * |
||
| 312 | * @param LoggerInterface|null $logger |
||
| 313 | * |
||
| 314 | * @throws SchemaException |
||
| 315 | * @throws DBALException |
||
| 316 | * @throws QueryException |
||
| 317 | * @throws DriverException |
||
| 318 | */ |
||
| 319 | public function pushSchema(LoggerInterface $logger = null) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Pack declared schemas in a normalized form, make sure to call renderSchema() first. |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | * |
||
| 330 | * @throws SchemaException |
||
| 331 | */ |
||
| 332 | public function packSchema(): array |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Walk thought schemas and define structure of associated tables. |
||
| 375 | * |
||
| 376 | * @throws SchemaException |
||
| 377 | * @throws DBALException |
||
| 378 | */ |
||
| 379 | protected function renderModels() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Walk thought all record schemas, fetch and declare needed relations using relation manager. |
||
| 428 | * |
||
| 429 | * @throws SchemaException |
||
| 430 | * @throws DefinitionException |
||
| 431 | */ |
||
| 432 | protected function renderRelations() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Update table state. |
||
| 466 | * |
||
| 467 | * @param AbstractTable $schema |
||
| 468 | * |
||
| 469 | * @throws SchemaException |
||
| 470 | */ |
||
| 471 | protected function pushTable(AbstractTable $schema) |
||
| 482 | } |