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 |
||
| 18 | abstract class Repository |
||
| 19 | { |
||
| 20 | /** @var Context */ |
||
| 21 | protected $databaseContext; |
||
| 22 | |||
| 23 | /** @var Structure|null */ |
||
| 24 | protected $structure; |
||
| 25 | |||
| 26 | /** @var string Soft delete field, if empty soft delete is disabled */ |
||
| 27 | protected $softDelete = ''; |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | private $behaviours = []; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected static $tableName = 'unknown'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Context $databaseContext |
||
| 37 | * @param Structure $structure |
||
| 38 | */ |
||
| 39 | 10 | public function __construct(Context $databaseContext, Structure $structure) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 10 | public static function getTableName(): string |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @return NetteDatabaseSelection |
||
| 61 | */ |
||
| 62 | 10 | public function getTable(): NetteDatabaseSelection |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return Context |
||
| 69 | */ |
||
| 70 | 8 | public function getDatabaseContext(): Context |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @param Behaviour $behaviour |
||
| 77 | * @return Repository |
||
| 78 | */ |
||
| 79 | 10 | public function registerBehaviour(Behaviour $behaviour): Repository |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Get behaviour by class |
||
| 87 | * @param string $class |
||
| 88 | * @return Behaviour|null |
||
| 89 | */ |
||
| 90 | public function getBehaviour($class): ?Behaviour |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Configure repository |
||
| 97 | */ |
||
| 98 | public function configure(): void |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Define table scopes |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | protected function getScopes(): array |
||
| 112 | |||
| 113 | /********************************************************************\ |
||
| 114 | | Magic methods |
||
| 115 | \********************************************************************/ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $name |
||
| 119 | * @param array $arguments |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 2 | public function __call(string $name, array $arguments) |
|
| 137 | |||
| 138 | /********************************************************************\ |
||
| 139 | | Wrapper methods |
||
| 140 | \********************************************************************/ |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Find all records |
||
| 144 | * @return Selection |
||
| 145 | */ |
||
| 146 | 8 | public function findAll(): Selection |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Find by conditions |
||
| 153 | * @param array $by |
||
| 154 | * @return Selection |
||
| 155 | */ |
||
| 156 | public function findBy(array $by): Selection |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns all rows as associative array |
||
| 163 | * @param string|null $key |
||
| 164 | * @param string|null $value |
||
| 165 | * @param string|null $order |
||
| 166 | * @param array $where |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function fetchPairs(string $key = null, string $value = null, string $order = null, array $where = []): array |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Insert one record |
||
| 185 | * @param array|Traversable $data |
||
| 186 | * @return ActiveRow|null |
||
| 187 | * @throws Exception |
||
| 188 | */ |
||
| 189 | 2 | public function insert(array $data): ?ActiveRow |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Update one record |
||
| 215 | * @param ActiveRow $record |
||
| 216 | * @param array $data |
||
| 217 | * @return ActiveRow|null |
||
| 218 | */ |
||
| 219 | 2 | public function update(ActiveRow $record, array $data): ?ActiveRow |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Delete one record |
||
| 242 | * @param ActiveRow $record |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function delete(ActiveRow $record): bool |
||
| 271 | |||
| 272 | /********************************************************************\ |
||
| 273 | | Builder methods |
||
| 274 | \********************************************************************/ |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param NetteDatabaseSelection $selection |
||
| 278 | * @return Selection |
||
| 279 | */ |
||
| 280 | 8 | private function prepareSelection(NetteDatabaseSelection $selection): Selection |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param NetteDatabaseActiveRow $row |
||
| 288 | * @return ActiveRow |
||
| 289 | */ |
||
| 290 | 2 | private function prepareRecord(NetteDatabaseActiveRow $row): ActiveRow |
|
| 295 | |||
| 296 | /********************************************************************\ |
||
| 297 | | Helper methods |
||
| 298 | \********************************************************************/ |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Run new transaction if no transaction is running, do nothing otherwise |
||
| 302 | * @param callable $callback |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | 8 | public function transaction(callable $callback) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param callable $callback |
||
| 331 | * @param int $retryTimes |
||
| 332 | * @return mixed |
||
| 333 | * @throws DriverException |
||
| 334 | */ |
||
| 335 | public function ensure(callable $callback, int $retryTimes = 1) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Try call callback X times |
||
| 350 | * @param callable $callback |
||
| 351 | * @param int $retryTimes |
||
| 352 | * @return mixed |
||
| 353 | * @throws DriverException |
||
| 354 | */ |
||
| 355 | public function retry(callable $callback, int $retryTimes = 3) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Paginate callback |
||
| 369 | * @param Selection $selection |
||
| 370 | * @param int $limit |
||
| 371 | * @param callable $callback |
||
| 372 | */ |
||
| 373 | public function chunk(Selection $selection, int $limit, callable $callback) |
||
| 381 | } |
||
| 382 |
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: