Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Table implements ArrayAccess |
||
11 | { |
||
12 | private $db; |
||
13 | private $row; |
||
14 | private $collection; |
||
15 | private $cache = []; |
||
16 | |||
17 | public $name; |
||
18 | public $fields = []; |
||
19 | public $queriesModifiers = []; |
||
20 | |||
21 | /** |
||
22 | * Constructor. |
||
23 | * |
||
24 | * @param SimpleCrud $db |
||
25 | * @param string $name |
||
26 | */ |
||
27 | final public function __construct(SimpleCrud $db, $name) |
||
28 | { |
||
29 | $this->db = $db; |
||
30 | $this->name = $name; |
||
31 | |||
32 | $this->setRow(new Row($this)); |
||
33 | $this->setCollection(new RowCollection($this)); |
||
34 | |||
35 | $fieldFactory = $db->getFieldFactory(); |
||
36 | |||
37 | foreach (array_keys($this->getScheme()['fields']) as $name) { |
||
38 | $this->fields[$name] = $fieldFactory->get($this, $name); |
||
39 | } |
||
40 | |||
41 | $this->init(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Debug info. |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function __debugInfo() |
||
50 | { |
||
51 | return [ |
||
52 | 'name' => $this->name, |
||
53 | 'fields' => $this->fields, |
||
54 | ]; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Callback used to init the table. |
||
59 | */ |
||
60 | protected function init() |
||
61 | { |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Store a row in the cache. |
||
66 | * |
||
67 | * @param Row $row |
||
68 | */ |
||
69 | public function cache(Row $row) |
||
73 | |||
74 | /** |
||
75 | * Clear the current cache. |
||
76 | */ |
||
77 | public function clearCache() |
||
81 | |||
82 | /** |
||
83 | * Register a new query modifier |
||
84 | * |
||
85 | * @param string $name |
||
86 | * @param callable $modifier |
||
87 | */ |
||
88 | public function addQueryModifier($name, callable $modifier) |
||
96 | |||
97 | /** |
||
98 | * Magic method to create queries related with this table. |
||
99 | * |
||
100 | * @param string $name |
||
101 | * @param array $arguments |
||
102 | * |
||
103 | * @throws SimpleCrudException |
||
104 | * |
||
105 | * @return Queries\Query|null |
||
106 | */ |
||
107 | public function __call($name, $arguments) |
||
119 | |||
120 | /** |
||
121 | * Check if a row with a specific id exists. |
||
122 | * |
||
123 | * @see ArrayAccess |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function offsetExists($offset) |
||
138 | |||
139 | /** |
||
140 | * Returns a row with a specific id. |
||
141 | * |
||
142 | * @see ArrayAccess |
||
143 | * |
||
144 | * @return Row|null |
||
145 | */ |
||
146 | public function offsetGet($offset) |
||
157 | |||
158 | /** |
||
159 | * Store a row with a specific id. |
||
160 | * |
||
161 | * @see ArrayAccess |
||
162 | */ |
||
163 | public function offsetSet($offset, $value) |
||
198 | |||
199 | /** |
||
200 | * Remove a row with a specific id. |
||
201 | * |
||
202 | * @see ArrayAccess |
||
203 | */ |
||
204 | public function offsetUnset($offset) |
||
213 | |||
214 | /** |
||
215 | * Returns the SimpleCrud instance associated with this table. |
||
216 | * |
||
217 | * @return SimpleCrud |
||
218 | */ |
||
219 | public function getDatabase() |
||
223 | |||
224 | /** |
||
225 | * Returns the table scheme. |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function getScheme() |
||
233 | |||
234 | /** |
||
235 | * Returns an attribute. |
||
236 | * |
||
237 | * @param string $name |
||
238 | * |
||
239 | * @return null|mixed |
||
240 | */ |
||
241 | public function getAttribute($name) |
||
245 | |||
246 | /** |
||
247 | * Defines the Row class used by this table. |
||
248 | * |
||
249 | * @param Row $row |
||
250 | */ |
||
251 | public function setRow(Row $row) |
||
255 | |||
256 | /** |
||
257 | * Defines the RowCollection class used by this table. |
||
258 | * |
||
259 | * @param RowCollection $collection |
||
260 | */ |
||
261 | public function setCollection(RowCollection $collection) |
||
265 | |||
266 | /** |
||
267 | * Creates a new row instance. |
||
268 | * |
||
269 | * @param array $data The values of the row |
||
270 | * |
||
271 | * @return Row |
||
272 | */ |
||
273 | public function create(array $data = []) |
||
287 | |||
288 | /** |
||
289 | * Creates a new rowCollection instance. |
||
290 | * |
||
291 | * @param array $data Rows added to this collection |
||
292 | * |
||
293 | * @return RowCollection |
||
294 | */ |
||
295 | public function createCollection(array $data = []) |
||
305 | |||
306 | /** |
||
307 | * Default data converter/validator from database. |
||
308 | * |
||
309 | * @param array $data The values before insert to database |
||
310 | * @param bool $new True for inserts, false for updates |
||
311 | */ |
||
312 | public function dataToDatabase(array $data, $new) |
||
316 | |||
317 | /** |
||
318 | * Default data converter from database. |
||
319 | * |
||
320 | * @param array $data The database format values |
||
321 | */ |
||
322 | public function dataFromDatabase(array $data) |
||
326 | |||
327 | /** |
||
328 | * Prepares the data from the result of a database selection. |
||
329 | * |
||
330 | * @param array $data |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | public function createFromDatabase(array $data) |
||
355 | |||
356 | /** |
||
357 | * Prepares the data before save into database (used by update and insert). |
||
358 | * |
||
359 | * @param array $data |
||
360 | * @param bool $new |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function prepareDataToDatabase(array $data, $new) |
||
381 | } |
||
382 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: