Complex classes like Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Repository |
||
| 21 | { |
||
| 22 | /** @var Context */ |
||
| 23 | protected $databaseContext; |
||
| 24 | |||
| 25 | /** @var Structure|null */ |
||
| 26 | protected $structure; |
||
| 27 | |||
| 28 | /** @var string Soft delete field, if empty soft delete is disabled */ |
||
| 29 | protected $softDelete = ''; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | private $behaviours = []; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected static $tableName = 'unknown'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param Context $databaseContext |
||
| 39 | */ |
||
| 40 | 64 | public function __construct(Context $databaseContext) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param Structure $structure |
||
| 49 | */ |
||
| 50 | 64 | public function setStructure(Structure $structure) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | 64 | public static function getTableName(): string |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @return Context |
||
| 68 | */ |
||
| 69 | 8 | public function getDatabaseContext(): Context |
|
| 73 | |||
| 74 | /********************************************************************\ |
||
| 75 | | Magic methods |
||
| 76 | \********************************************************************/ |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $name |
||
| 80 | * @param array $arguments |
||
| 81 | * @return mixed |
||
| 82 | * @throws RepositoryException |
||
| 83 | */ |
||
| 84 | 6 | public function __call(string $name, array $arguments) |
|
| 99 | |||
| 100 | /********************************************************************\ |
||
| 101 | | Wrapper methods |
||
| 102 | \********************************************************************/ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Find all records |
||
| 106 | * @return Selection |
||
| 107 | */ |
||
| 108 | 40 | public function findAll(): Selection |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Find by conditions |
||
| 115 | * @param array $by |
||
| 116 | * @return Selection |
||
| 117 | */ |
||
| 118 | 16 | public function findBy(array $by): Selection |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Returns all rows as associative array |
||
| 125 | * @param string|null $key |
||
| 126 | * @param string|null $value |
||
| 127 | * @param string|null $order |
||
| 128 | * @param array $where |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | public function fetchPairs(string $key = null, string $value = null, string $order = null, array $where = []): array |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Insert one record |
||
| 147 | * @param array|Traversable $data |
||
| 148 | * @return ActiveRow|null |
||
| 149 | * @throws Exception |
||
| 150 | */ |
||
| 151 | 2 | public function insert(array $data): ?ActiveRow |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Update one record |
||
| 177 | * @param ActiveRow $record |
||
| 178 | * @param array $data |
||
| 179 | * @return ActiveRow|null |
||
| 180 | */ |
||
| 181 | 2 | public function update(ActiveRow $record, array $data): ?ActiveRow |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Delete one record |
||
| 204 | * @param ActiveRow $record |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function delete(ActiveRow $record): bool |
||
| 233 | |||
| 234 | /********************************************************************\ |
||
| 235 | | Internal methods |
||
| 236 | \********************************************************************/ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return NetteDatabaseSelection |
||
| 240 | */ |
||
| 241 | 58 | protected function getTable(): NetteDatabaseSelection |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param Behaviour $behaviour |
||
| 248 | * @return Repository |
||
| 249 | */ |
||
| 250 | 64 | protected function registerBehaviour(Behaviour $behaviour): Repository |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Get behaviour by class |
||
| 258 | * @param string $class |
||
| 259 | * @return Behaviour|null |
||
| 260 | */ |
||
| 261 | protected function getBehaviour($class): ?Behaviour |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Configure repository |
||
| 268 | */ |
||
| 269 | 64 | protected function configure(): void |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Define table scopes |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | 64 | protected function getScopes(): array |
|
| 283 | |||
| 284 | /********************************************************************\ |
||
| 285 | | Builder methods |
||
| 286 | \********************************************************************/ |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param NetteDatabaseSelection $selection |
||
| 290 | * @return Selection |
||
| 291 | */ |
||
| 292 | 56 | private function prepareSelection(NetteDatabaseSelection $selection): Selection |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param NetteDatabaseActiveRow $row |
||
| 300 | * @return ActiveRow |
||
| 301 | */ |
||
| 302 | 2 | private function prepareRecord(NetteDatabaseActiveRow $row): ActiveRow |
|
| 307 | |||
| 308 | /********************************************************************\ |
||
| 309 | | Helper methods |
||
| 310 | \********************************************************************/ |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Run new transaction if no transaction is running, do nothing otherwise |
||
| 314 | * @param callable $callback |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | 8 | public function transaction(callable $callback) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param callable $callback |
||
| 343 | * @param int $retryTimes |
||
| 344 | * @return mixed |
||
| 345 | * @throws DriverException |
||
| 346 | */ |
||
| 347 | public function ensure(callable $callback, int $retryTimes = 1) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Try call callback X times |
||
| 362 | * @param callable $callback |
||
| 363 | * @param int $retryTimes |
||
| 364 | * @return mixed |
||
| 365 | * @throws DriverException |
||
| 366 | */ |
||
| 367 | public function retry(callable $callback, int $retryTimes = 3) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Paginate callback |
||
| 381 | * @param Selection $selection |
||
| 382 | * @param int $limit |
||
| 383 | * @param callable $callback |
||
| 384 | */ |
||
| 385 | public function chunk(Selection $selection, int $limit, callable $callback) |
||
| 393 | } |
||
| 394 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: