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 |
||
| 22 | abstract class Repository |
||
| 23 | { |
||
| 24 | /** @var Context */ |
||
| 25 | protected $databaseContext; |
||
| 26 | |||
| 27 | /** @var Structure|null */ |
||
| 28 | protected $structure; |
||
| 29 | |||
| 30 | /** @var string Soft delete field, if empty soft delete is disabled */ |
||
| 31 | protected $softDelete = ''; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $behaviours = []; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | protected static $tableName = 'unknown'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param Context $databaseContext |
||
| 41 | */ |
||
| 42 | public function __construct(Context $databaseContext) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param Structure $structure |
||
| 51 | */ |
||
| 52 | public function setStructure(Structure $structure): void |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public static function getTableName(): string |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return Context |
||
| 70 | */ |
||
| 71 | public function getDatabaseContext(): Context |
||
| 75 | |||
| 76 | /********************************************************************\ |
||
| 77 | | Magic methods |
||
| 78 | \********************************************************************/ |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $name |
||
| 82 | * @param array $arguments |
||
| 83 | * @return mixed |
||
| 84 | * @throws RepositoryException |
||
| 85 | */ |
||
| 86 | public function __call(string $name, array $arguments) |
||
| 101 | |||
| 102 | /********************************************************************\ |
||
| 103 | | Wrapper methods |
||
| 104 | \********************************************************************/ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Find all records |
||
| 108 | * @return Selection |
||
| 109 | */ |
||
| 110 | public function findAll(): Selection |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Find by conditions |
||
| 117 | * @param array $by |
||
| 118 | * @return Selection |
||
| 119 | */ |
||
| 120 | public function findBy(array $by): Selection |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns all rows as associative array |
||
| 127 | * @param string|null $key |
||
| 128 | * @param string|null $value |
||
| 129 | * @param string|null $order |
||
| 130 | * @param array $where |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public function fetchPairs(string $key = null, string $value = null, string $order = null, array $where = []): array |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Insert one record |
||
| 149 | * @param array|Traversable $data |
||
| 150 | * @return ActiveRow|null |
||
| 151 | * @throws Exception |
||
| 152 | */ |
||
| 153 | public function insert(array $data): ?ActiveRow |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Update one record |
||
| 179 | * @param ActiveRow $record |
||
| 180 | * @param array $data |
||
| 181 | * @return ActiveRow|null |
||
| 182 | */ |
||
| 183 | public function update(ActiveRow $record, array $data): ?ActiveRow |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Delete one record |
||
| 206 | * @param ActiveRow $record |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function delete(ActiveRow $record): bool |
||
| 235 | |||
| 236 | /********************************************************************\ |
||
| 237 | | Internal methods |
||
| 238 | \********************************************************************/ |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return NetteDatabaseSelection |
||
| 242 | */ |
||
| 243 | protected function getTable(): NetteDatabaseSelection |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param Behaviour $behaviour |
||
| 250 | * @return Repository |
||
| 251 | */ |
||
| 252 | protected function registerBehaviour(Behaviour $behaviour): Repository |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get behaviour by class |
||
| 260 | * @param string $class |
||
| 261 | * @return Behaviour|null |
||
| 262 | */ |
||
| 263 | protected function getBehaviour($class): ?Behaviour |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Configure repository |
||
| 270 | */ |
||
| 271 | protected function configure(): void |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Define table scopes |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | protected function getScopes(): array |
||
| 285 | |||
| 286 | /********************************************************************\ |
||
| 287 | | Builder methods |
||
| 288 | \********************************************************************/ |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param NetteDatabaseSelection $selection |
||
| 292 | * @return Selection |
||
| 293 | */ |
||
| 294 | private function prepareSelection(NetteDatabaseSelection $selection): Selection |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param NetteDatabaseActiveRow $row |
||
| 302 | * @return ActiveRow |
||
| 303 | */ |
||
| 304 | private function prepareRecord(NetteDatabaseActiveRow $row): ActiveRow |
||
| 309 | |||
| 310 | /********************************************************************\ |
||
| 311 | | Helper methods |
||
| 312 | \********************************************************************/ |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Run new transaction if no transaction is running, do nothing otherwise |
||
| 316 | * @param callable $callback |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | public function transaction(callable $callback) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param callable $callback |
||
| 345 | * @param int $retryTimes |
||
| 346 | * @return mixed |
||
| 347 | * @throws DriverException |
||
| 348 | */ |
||
| 349 | public function ensure(callable $callback, int $retryTimes = 1) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Try call callback X times |
||
| 364 | * @param callable $callback |
||
| 365 | * @param int $retryTimes |
||
| 366 | * @return mixed |
||
| 367 | * @throws DriverException |
||
| 368 | */ |
||
| 369 | public function retry(callable $callback, int $retryTimes = 3) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Paginate callback |
||
| 383 | * @param Selection $selection |
||
| 384 | * @param int $limit |
||
| 385 | * @param callable $callback |
||
| 386 | */ |
||
| 387 | public function chunk(Selection $selection, int $limit, callable $callback) |
||
| 395 | } |
||
| 396 |
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: