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