Complex classes like BaseRepository 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 BaseRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 44 | abstract class BaseRepository |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | abstract protected function getTableNameForReading(): string; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | abstract protected function getTableNameForWriting(): string; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | abstract protected function getClassName(): string; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | abstract protected function getPrimaryKeyName(): string; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Connection |
||
| 68 | */ |
||
| 69 | private $connection; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var DatabaseSchemaInterface |
||
| 73 | */ |
||
| 74 | private $databaseSchema; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param Closure $closure |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | * |
||
| 81 | * @throws RepositoryException |
||
| 82 | */ |
||
| 83 | 21 | public function inTransaction(Closure $closure): void |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @return Connection |
||
| 105 | */ |
||
| 106 | 61 | protected function getConnection(): Connection |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param Connection $connection |
||
| 113 | * |
||
| 114 | * @return self |
||
| 115 | */ |
||
| 116 | 71 | protected function setConnection(Connection $connection): self |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $columns |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | * |
||
| 128 | * @throws RepositoryException |
||
| 129 | */ |
||
| 130 | 4 | protected function indexResources(array $columns = ['*']): array |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param iterable $values |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | * |
||
| 155 | * @throws RepositoryException |
||
| 156 | */ |
||
| 157 | 30 | protected function createResource(iterable $values): void |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | 22 | protected function getLastInsertId(): int |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param string|int $identifier |
||
| 189 | * @param array $columns |
||
| 190 | * |
||
| 191 | * @return mixed |
||
| 192 | * |
||
| 193 | * @throws RepositoryException |
||
| 194 | */ |
||
| 195 | 26 | protected function readResource($identifier, array $columns = ['*']) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string|int $identifier |
||
| 202 | * @param string $column |
||
| 203 | * @param array $columns |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | * |
||
| 207 | * @throws RepositoryException |
||
| 208 | */ |
||
| 209 | 26 | protected function readResourceByColumn($identifier, string $column, array $columns = ['*']) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param string|int $identifier |
||
| 232 | * @param array $values |
||
| 233 | * |
||
| 234 | * @return int |
||
| 235 | * |
||
| 236 | * @throws RepositoryException |
||
| 237 | */ |
||
| 238 | 7 | protected function updateResource($identifier, array $values): int |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param string|int $identifier |
||
| 262 | * |
||
| 263 | * @return int |
||
| 264 | * |
||
| 265 | * @throws RepositoryException |
||
| 266 | */ |
||
| 267 | 7 | protected function deleteResource($identifier): int |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string|int $primaryKey |
||
| 288 | * @param iterable $foreignKeys |
||
| 289 | * @param string $intTableName |
||
| 290 | * @param string $intPrimaryKeyName |
||
| 291 | * @param string $intForeignKeyName |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | * |
||
| 295 | * @throws RepositoryException |
||
| 296 | */ |
||
| 297 | 20 | protected function createBelongsToManyRelationship( |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param string|int $identifier |
||
| 335 | * @param string $intTableName |
||
| 336 | * @param string $intPrimaryKeyName |
||
| 337 | * @param string $intForeignKeyName |
||
| 338 | * |
||
| 339 | * @return string[] |
||
| 340 | * |
||
| 341 | * @throws RepositoryException |
||
| 342 | */ |
||
| 343 | 22 | protected function readBelongsToManyRelationshipIdentifiers( |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $intTableName |
||
| 371 | * @param string $intPrimaryKeyName |
||
| 372 | * @param string|int $identifier |
||
| 373 | * |
||
| 374 | * @return int |
||
| 375 | * |
||
| 376 | * @throws RepositoryException |
||
| 377 | */ |
||
| 378 | 5 | protected function deleteBelongsToManyRelationshipIdentifiers( |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param string|int $identifier |
||
| 403 | * @param string $hasManyTableName |
||
| 404 | * @param string $hasManyColumn |
||
| 405 | * @param string $hasManyFkName |
||
| 406 | * |
||
| 407 | * @return string[] |
||
| 408 | * |
||
| 409 | * @throws RepositoryException |
||
| 410 | */ |
||
| 411 | 16 | protected function readHasManyRelationshipColumn( |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param DateTimeInterface $dateTime |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | * |
||
| 442 | * @throws RepositoryException |
||
| 443 | */ |
||
| 444 | 43 | protected function getDateTimeForDb(DateTimeInterface $dateTime): string |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @return DatabaseSchemaInterface |
||
| 457 | */ |
||
| 458 | 63 | protected function getDatabaseSchema(): DatabaseSchemaInterface |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @param DatabaseSchemaInterface $databaseSchema |
||
| 465 | * |
||
| 466 | * @return self |
||
| 467 | */ |
||
| 468 | 71 | protected function setDatabaseSchema(DatabaseSchemaInterface $databaseSchema): self |
|
| 474 | |||
| 475 | /** |
||
| 476 | * @param QueryBuilder $query |
||
| 477 | * @param mixed $value |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | * |
||
| 481 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 482 | * |
||
| 483 | * @throws RepositoryException |
||
| 484 | */ |
||
| 485 | 55 | protected function createTypedParameter(QueryBuilder $query, $value): string |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Helps to ignore exception handling for cases when they do not arise (e.g. having current date and time). |
||
| 505 | * |
||
| 506 | * @param Closure $closure |
||
| 507 | * @param mixed $defaultValue |
||
| 508 | * |
||
| 509 | * @return mixed|null |
||
| 510 | */ |
||
| 511 | 43 | protected function ignoreException(Closure $closure, $defaultValue = null) |
|
| 520 | } |
||
| 521 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.