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 Row 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 Row, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Row extends AbstractRow |
||
11 | { |
||
12 | private $values = []; |
||
13 | private $relations = []; |
||
14 | private $changed = false; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | public function __construct(Table $table) |
||
20 | { |
||
21 | parent::__construct($table); |
||
22 | |||
23 | foreach ($table->getScheme()['fields'] as $name => $field) { |
||
24 | $this->values[$name] = null; |
||
25 | } |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Debug info. |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | public function __debugInfo() |
||
40 | |||
41 | /** |
||
42 | * Return the current cache. |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | public function getCache() |
||
50 | |||
51 | /** |
||
52 | * Set a new cache. |
||
53 | * |
||
54 | * @param array $relations |
||
55 | */ |
||
56 | public function setCache(array $relations) |
||
60 | |||
61 | /** |
||
62 | * Magic method to return properties or load them automatically. |
||
63 | * |
||
64 | * @param string $name |
||
65 | */ |
||
66 | public function __get($name) |
||
92 | |||
93 | /** |
||
94 | * Magic method to store properties. |
||
95 | * |
||
96 | * @param string $name |
||
97 | * @param mixed $value |
||
98 | */ |
||
99 | public function __set($name, $value) |
||
131 | |||
132 | /** |
||
133 | * Magic method to check if a property is defined or not. |
||
134 | * |
||
135 | * @param string $name Property name |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function __isset($name) |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function toArray(array $bannedEntities = []) |
||
166 | |||
167 | public function edit(array $values) |
||
175 | |||
176 | /** |
||
177 | * Saves this row in the database. |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function save() |
||
182 | { |
||
183 | if ($this->changed) { |
||
184 | if (empty($this->id)) { |
||
185 | $this->id = $this->table->insert() |
||
|
|||
186 | ->data($this->values) |
||
187 | ->run(); |
||
188 | } else { |
||
189 | $this->table->update() |
||
190 | ->data($this->values) |
||
191 | ->byId($this->id) |
||
192 | ->limit(1) |
||
193 | ->run(); |
||
194 | } |
||
195 | |||
196 | $this->table->cache($this); |
||
197 | } |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Relate this row with other row and save the relation. |
||
204 | * |
||
205 | * @param Row $row |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function relate(Row $row) |
||
271 | |||
272 | /** |
||
273 | * Unrelate this row with other row and save it. |
||
274 | * |
||
275 | * @param Row $row |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function unrelate(Row $row) |
||
332 | |||
333 | /** |
||
334 | * Unrelate this row with all rows of a specific table. |
||
335 | * |
||
336 | * @param Table $relationTable |
||
337 | * |
||
338 | * @return $this |
||
339 | */ |
||
340 | public function unrelateAll(Table $relationTable) |
||
383 | } |
||
384 |
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: