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 | * Prefix given string (column name) with table name |
||
70 | * @param string $column |
||
71 | * @return string |
||
72 | */ |
||
73 | public static function prefixColumn(string $column): string |
||
77 | |||
78 | /** |
||
79 | * @return Context |
||
80 | */ |
||
81 | public function getDatabaseContext(): Context |
||
85 | |||
86 | /********************************************************************\ |
||
87 | | Magic methods |
||
88 | \********************************************************************/ |
||
89 | |||
90 | /** |
||
91 | * @param string $name |
||
92 | * @param array $arguments |
||
93 | * @return mixed |
||
94 | * @throws RepositoryException |
||
95 | */ |
||
96 | public function __call(string $name, array $arguments) |
||
111 | |||
112 | /********************************************************************\ |
||
113 | | Wrapper methods |
||
114 | \********************************************************************/ |
||
115 | |||
116 | /** |
||
117 | * Find all records |
||
118 | * @return Selection |
||
119 | */ |
||
120 | public function findAll(): Selection |
||
124 | |||
125 | /** |
||
126 | * Find by conditions |
||
127 | * @param array $by |
||
128 | * @return Selection |
||
129 | */ |
||
130 | public function findBy(array $by): Selection |
||
134 | |||
135 | /** |
||
136 | * Returns all rows as associative array |
||
137 | * @param string|null $key |
||
138 | * @param string|null $value |
||
139 | * @param string|null $order |
||
140 | * @param array $where |
||
141 | * @return array |
||
142 | */ |
||
143 | public function fetchPairs(string $key = null, string $value = null, string $order = null, array $where = []): array |
||
156 | |||
157 | /** |
||
158 | * Insert one record |
||
159 | * @param array|Traversable $data |
||
160 | * @return ActiveRow|null |
||
161 | * @throws Exception |
||
162 | */ |
||
163 | public function insert(array $data): ?ActiveRow |
||
186 | |||
187 | /** |
||
188 | * Update one record |
||
189 | * @param ActiveRow $record |
||
190 | * @param array $data |
||
191 | * @return ActiveRow|null |
||
192 | */ |
||
193 | public function update(ActiveRow $record, array $data): ?ActiveRow |
||
213 | |||
214 | /** |
||
215 | * Delete one record |
||
216 | * @param ActiveRow $record |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function delete(ActiveRow $record): bool |
||
245 | |||
246 | /********************************************************************\ |
||
247 | | Internal methods |
||
248 | \********************************************************************/ |
||
249 | |||
250 | /** |
||
251 | * @return NetteDatabaseSelection |
||
252 | */ |
||
253 | protected function getTable(): NetteDatabaseSelection |
||
257 | |||
258 | /** |
||
259 | * @param Behaviour $behaviour |
||
260 | * @return Repository |
||
261 | */ |
||
262 | protected function registerBehaviour(Behaviour $behaviour): Repository |
||
267 | |||
268 | /** |
||
269 | * Get behaviour by class |
||
270 | * @param string $class |
||
271 | * @return Behaviour|null |
||
272 | */ |
||
273 | protected function getBehaviour($class): ?Behaviour |
||
277 | |||
278 | /** |
||
279 | * Configure repository |
||
280 | */ |
||
281 | protected function configure(): void |
||
285 | |||
286 | /** |
||
287 | * Define table scopes |
||
288 | * @return array |
||
289 | */ |
||
290 | protected function getScopes(): array |
||
295 | |||
296 | /********************************************************************\ |
||
297 | | Builder methods |
||
298 | \********************************************************************/ |
||
299 | |||
300 | /** |
||
301 | * @param NetteDatabaseSelection $selection |
||
302 | * @return Selection |
||
303 | */ |
||
304 | private function prepareSelection(NetteDatabaseSelection $selection): Selection |
||
309 | |||
310 | /** |
||
311 | * @param NetteDatabaseActiveRow $row |
||
312 | * @return ActiveRow |
||
313 | */ |
||
314 | private function prepareRecord(NetteDatabaseActiveRow $row): ActiveRow |
||
319 | |||
320 | /********************************************************************\ |
||
321 | | Helper methods |
||
322 | \********************************************************************/ |
||
323 | |||
324 | /** |
||
325 | * Run new transaction if no transaction is running, do nothing otherwise |
||
326 | * @param callable $callback |
||
327 | * @return mixed |
||
328 | */ |
||
329 | public function transaction(callable $callback) |
||
352 | |||
353 | /** |
||
354 | * @param callable $callback |
||
355 | * @param int $retryTimes |
||
356 | * @return mixed |
||
357 | * @throws DriverException |
||
358 | */ |
||
359 | public function ensure(callable $callback, int $retryTimes = 1) |
||
371 | |||
372 | /** |
||
373 | * Try call callback X times |
||
374 | * @param callable $callback |
||
375 | * @param int $retryTimes |
||
376 | * @return mixed |
||
377 | * @throws DriverException |
||
378 | */ |
||
379 | public function retry(callable $callback, int $retryTimes = 3) |
||
390 | |||
391 | /** |
||
392 | * Paginate callback |
||
393 | * @param Selection $selection |
||
394 | * @param int $limit |
||
395 | * @param callable $callback |
||
396 | */ |
||
397 | public function chunk(Selection $selection, int $limit, callable $callback) |
||
405 | } |
||
406 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: