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 namespace Limoncello\Passport\Repositories; |
||
36 | abstract class BaseRepository |
||
37 | { |
||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | abstract protected function getTableNameForReading(): string; |
||
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | abstract protected function getTableNameForWriting(): string; |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | abstract protected function getClassName(): string; |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | abstract protected function getPrimaryKeyName(): string; |
||
57 | |||
58 | /** |
||
59 | * @var Connection |
||
60 | */ |
||
61 | private $connection; |
||
62 | |||
63 | /** |
||
64 | * @var DatabaseSchemaInterface |
||
65 | */ |
||
66 | private $databaseSchema; |
||
67 | |||
68 | /** |
||
69 | * @param Closure $closure |
||
70 | * |
||
71 | * @return void |
||
72 | * |
||
73 | * @throws RepositoryException |
||
74 | */ |
||
75 | 21 | public function inTransaction(Closure $closure): void |
|
94 | |||
95 | /** |
||
96 | * @return Connection |
||
97 | */ |
||
98 | 61 | protected function getConnection(): Connection |
|
102 | |||
103 | /** |
||
104 | * @param Connection $connection |
||
105 | * |
||
106 | * @return self |
||
107 | */ |
||
108 | 71 | protected function setConnection(Connection $connection): self |
|
114 | |||
115 | /** |
||
116 | * @param array $columns |
||
117 | * |
||
118 | * @return array |
||
119 | * |
||
120 | * @throws RepositoryException |
||
121 | */ |
||
122 | 4 | protected function indexResources(array $columns = ['*']): array |
|
141 | |||
142 | /** |
||
143 | * @param iterable $values |
||
144 | * |
||
145 | * @return void |
||
146 | * |
||
147 | * @throws RepositoryException |
||
148 | */ |
||
149 | 30 | protected function createResource(iterable $values): void |
|
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | 22 | protected function getLastInsertId(): string |
|
176 | |||
177 | /** |
||
178 | * @param string|int $identifier |
||
179 | * @param array $columns |
||
180 | * |
||
181 | * @return mixed |
||
182 | * |
||
183 | * @throws RepositoryException |
||
184 | */ |
||
185 | 26 | protected function readResource($identifier, array $columns = ['*']) |
|
189 | |||
190 | /** |
||
191 | * @param string|int $identifier |
||
192 | * @param string $column |
||
193 | * @param array $columns |
||
194 | * |
||
195 | * @return mixed |
||
196 | * |
||
197 | * @throws RepositoryException |
||
198 | */ |
||
199 | 26 | protected function readResourceByColumn($identifier, string $column, array $columns = ['*']) |
|
219 | |||
220 | /** |
||
221 | * @param string|int $identifier |
||
222 | * @param array $values |
||
223 | * |
||
224 | * @return int |
||
225 | * |
||
226 | * @throws RepositoryException |
||
227 | */ |
||
228 | 7 | protected function updateResource($identifier, array $values): int |
|
249 | |||
250 | /** |
||
251 | * @param string|int $identifier |
||
252 | * |
||
253 | * @return int |
||
254 | * |
||
255 | * @throws RepositoryException |
||
256 | */ |
||
257 | 7 | protected function deleteResource($identifier): int |
|
275 | |||
276 | /** |
||
277 | * @param string|int $primaryKey |
||
278 | * @param iterable $foreignKeys |
||
279 | * @param string $intTableName |
||
280 | * @param string $intPrimaryKeyName |
||
281 | * @param string $intForeignKeyName |
||
282 | * |
||
283 | * @return void |
||
284 | * |
||
285 | * @throws RepositoryException |
||
286 | */ |
||
287 | 20 | protected function createBelongsToManyRelationship( |
|
322 | |||
323 | /** |
||
324 | * @param string|int $identifier |
||
325 | * @param string $intTableName |
||
326 | * @param string $intPrimaryKeyName |
||
327 | * @param string $intForeignKeyName |
||
328 | * |
||
329 | * @return string[] |
||
330 | * |
||
331 | * @throws RepositoryException |
||
332 | */ |
||
333 | 22 | protected function readBelongsToManyRelationshipIdentifiers( |
|
358 | |||
359 | /** |
||
360 | * @param string $intTableName |
||
361 | * @param string $intPrimaryKeyName |
||
362 | * @param string|int $identifier |
||
363 | * |
||
364 | * @return int |
||
365 | * |
||
366 | * @throws RepositoryException |
||
367 | */ |
||
368 | 5 | protected function deleteBelongsToManyRelationshipIdentifiers( |
|
390 | |||
391 | /** |
||
392 | * @param string|int $identifier |
||
393 | * @param string $hasManyTableName |
||
394 | * @param string $hasManyColumn |
||
395 | * @param string $hasManyFkName |
||
396 | * |
||
397 | * @return string[] |
||
398 | * |
||
399 | * @throws RepositoryException |
||
400 | */ |
||
401 | 16 | protected function readHasManyRelationshipColumn( |
|
426 | |||
427 | /** |
||
428 | * @param DateTimeInterface $dateTime |
||
429 | * |
||
430 | * @return string |
||
431 | * |
||
432 | * @throws RepositoryException |
||
433 | */ |
||
434 | 43 | protected function getDateTimeForDb(DateTimeInterface $dateTime): string |
|
444 | |||
445 | /** |
||
446 | * @return DatabaseSchemaInterface |
||
447 | */ |
||
448 | 63 | protected function getDatabaseSchema(): DatabaseSchemaInterface |
|
452 | |||
453 | /** |
||
454 | * @param DatabaseSchemaInterface $databaseSchema |
||
455 | * |
||
456 | * @return self |
||
457 | */ |
||
458 | 71 | protected function setDatabaseSchema(DatabaseSchemaInterface $databaseSchema): self |
|
464 | |||
465 | /** |
||
466 | * @param QueryBuilder $query |
||
467 | * @param mixed $value |
||
468 | * |
||
469 | * @return string |
||
470 | * |
||
471 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
472 | * |
||
473 | * @throws RepositoryException |
||
474 | */ |
||
475 | 55 | protected function createTypedParameter(QueryBuilder $query, $value): string |
|
492 | |||
493 | /** |
||
494 | * Helps to ignore exception handling for cases when they do not arise (e.g. having current date and time). |
||
495 | * |
||
496 | * @param Closure $closure |
||
497 | * @param mixed $defaultValue |
||
498 | * |
||
499 | * @return mixed|null |
||
500 | */ |
||
501 | 43 | protected function ignoreException(Closure $closure, $defaultValue = null) |
|
510 | } |
||
511 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.