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 |
||
| 19 | abstract class Repository |
||
| 20 | { |
||
| 21 | /** @var Context */ |
||
| 22 | protected $databaseContext; |
||
| 23 | |||
| 24 | /** @var Structure|null */ |
||
| 25 | protected $structure; |
||
| 26 | |||
| 27 | /** @var string Soft delete field, if empty soft delete is disabled */ |
||
| 28 | protected $softDelete = ''; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | private $behaviours = []; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | protected static $tableName = 'unknown'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Context $databaseContext |
||
| 38 | */ |
||
| 39 | public function __construct(Context $databaseContext) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param Structure $structure |
||
| 48 | */ |
||
| 49 | public function setStructure(Structure $structure) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public static function getTableName(): string |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return NetteDatabaseSelection |
||
| 64 | */ |
||
| 65 | public function getTable(): NetteDatabaseSelection |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return Context |
||
| 72 | */ |
||
| 73 | public function getDatabaseContext(): Context |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param Behaviour $behaviour |
||
| 80 | * @return Repository |
||
| 81 | */ |
||
| 82 | public function registerBehaviour(Behaviour $behaviour): Repository |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get behaviour by class |
||
| 90 | * @param string $class |
||
| 91 | * @return Behaviour|null |
||
| 92 | */ |
||
| 93 | public function getBehaviour($class): ?Behaviour |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Configure repository |
||
| 100 | */ |
||
| 101 | protected function configure(): void |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Define table scopes |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function getScopes(): array |
||
| 115 | |||
| 116 | /********************************************************************\ |
||
| 117 | | Magic methods |
||
| 118 | \********************************************************************/ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $name |
||
| 122 | * @param array $arguments |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public function __call(string $name, array $arguments) |
||
| 140 | |||
| 141 | /********************************************************************\ |
||
| 142 | | Wrapper methods |
||
| 143 | \********************************************************************/ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Find all records |
||
| 147 | * @return Selection |
||
| 148 | */ |
||
| 149 | public function findAll(): Selection |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Find by conditions |
||
| 156 | * @param array $by |
||
| 157 | * @return Selection |
||
| 158 | */ |
||
| 159 | public function findBy(array $by): Selection |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns all rows as associative array |
||
| 166 | * @param string|null $key |
||
| 167 | * @param string|null $value |
||
| 168 | * @param string|null $order |
||
| 169 | * @param array $where |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function fetchPairs(string $key = null, string $value = null, string $order = null, array $where = []): array |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Insert one record |
||
| 188 | * @param array|Traversable $data |
||
| 189 | * @return ActiveRow|null |
||
| 190 | * @throws Exception |
||
| 191 | */ |
||
| 192 | public function insert(array $data): ?ActiveRow |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Update one record |
||
| 218 | * @param ActiveRow $record |
||
| 219 | * @param array $data |
||
| 220 | * @return ActiveRow|null |
||
| 221 | */ |
||
| 222 | public function update(ActiveRow $record, array $data): ?ActiveRow |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Delete one record |
||
| 245 | * @param ActiveRow $record |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function delete(ActiveRow $record): bool |
||
| 274 | |||
| 275 | /********************************************************************\ |
||
| 276 | | Builder methods |
||
| 277 | \********************************************************************/ |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param NetteDatabaseSelection $selection |
||
| 281 | * @return Selection |
||
| 282 | */ |
||
| 283 | private function prepareSelection(NetteDatabaseSelection $selection): Selection |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param NetteDatabaseActiveRow $row |
||
| 291 | * @return ActiveRow |
||
| 292 | */ |
||
| 293 | private function prepareRecord(NetteDatabaseActiveRow $row): ActiveRow |
||
| 298 | |||
| 299 | /********************************************************************\ |
||
| 300 | | Helper methods |
||
| 301 | \********************************************************************/ |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Run new transaction if no transaction is running, do nothing otherwise |
||
| 305 | * @param callable $callback |
||
| 306 | * @return mixed |
||
| 307 | */ |
||
| 308 | public function transaction(callable $callback) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param callable $callback |
||
| 334 | * @param int $retryTimes |
||
| 335 | * @return mixed |
||
| 336 | * @throws DriverException |
||
| 337 | */ |
||
| 338 | public function ensure(callable $callback, int $retryTimes = 1) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Try call callback X times |
||
| 353 | * @param callable $callback |
||
| 354 | * @param int $retryTimes |
||
| 355 | * @return mixed |
||
| 356 | * @throws DriverException |
||
| 357 | */ |
||
| 358 | public function retry(callable $callback, int $retryTimes = 3) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Paginate callback |
||
| 372 | * @param Selection $selection |
||
| 373 | * @param int $limit |
||
| 374 | * @param callable $callback |
||
| 375 | */ |
||
| 376 | public function chunk(Selection $selection, int $limit, callable $callback) |
||
| 384 | } |
||
| 385 |
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: