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 RunnableSelect 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 RunnableSelect, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class RunnableSelect extends Select implements IteratorAggregate { |
||
18 | /** @var array */ |
||
19 | private $values = array(); |
||
20 | /** @var bool */ |
||
21 | private $preserveTypes = false; |
||
22 | /** @var int */ |
||
23 | private $foundRows = 0; |
||
24 | |||
25 | /** |
||
26 | * @param array $values |
||
27 | * @return $this |
||
28 | */ |
||
29 | public function bindValues(array $values) { |
||
33 | |||
34 | /** |
||
35 | * @param string $key |
||
36 | * @param string|int|bool|float|null $value |
||
37 | * @return $this |
||
38 | */ |
||
39 | public function bindValue($key, $value) { |
||
43 | |||
44 | /** |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function clearValues() { |
||
51 | |||
52 | /** |
||
53 | * @param bool $preserveTypes |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function setPreserveTypes($preserveTypes = true) { |
||
60 | |||
61 | /** |
||
62 | * @param Closure $callback |
||
63 | * @return array[] |
||
64 | */ |
||
65 | View Code Duplication | public function fetchRows(Closure $callback = null) { |
|
91 | |||
92 | /** |
||
93 | * @param Closure $callback |
||
94 | * @return array[]|\Generator |
||
95 | */ |
||
96 | View Code Duplication | public function fetchRowsLazy(Closure $callback = null) { |
|
109 | |||
110 | /** |
||
111 | * @param Closure|null $callback |
||
112 | * @return string[] |
||
113 | * @throws \Exception |
||
114 | */ |
||
115 | View Code Duplication | public function fetchRow(Closure $callback = null) { |
|
135 | |||
136 | /** |
||
137 | * @param string $className |
||
138 | * @param Closure $callback |
||
139 | * @return \array[] |
||
140 | * @throws \Exception |
||
141 | */ |
||
142 | View Code Duplication | public function fetchObjects($className, Closure $callback = null) { |
|
168 | |||
169 | /** |
||
170 | * @param string $className |
||
171 | * @param Closure $callback |
||
172 | * @return array[]|Generator |
||
173 | */ |
||
174 | View Code Duplication | public function fetchObjectsLazy($className, Closure $callback = null) { |
|
187 | |||
188 | /** |
||
189 | * @param string $className |
||
190 | * @param Closure|null $callback |
||
191 | * @return string[] |
||
192 | * @throws \Exception |
||
193 | */ |
||
194 | View Code Duplication | public function fetchObject($className, Closure $callback = null) { |
|
214 | |||
215 | /** |
||
216 | * @param bool $treatValueAsArray |
||
217 | * @return mixed[] |
||
218 | */ |
||
219 | public function fetchKeyValue($treatValueAsArray = false) { |
||
233 | |||
234 | /** |
||
235 | * @param array $fields |
||
236 | * @return array |
||
237 | */ |
||
238 | public function fetchGroups(array $fields) { |
||
254 | |||
255 | /** |
||
256 | * @return string[] |
||
257 | */ |
||
258 | public function fetchArray() { |
||
263 | |||
264 | /** |
||
265 | * @param mixed $default |
||
266 | * @return null|bool|string|int|float |
||
267 | */ |
||
268 | public function fetchValue($default = null) { |
||
277 | |||
278 | /** |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function getFoundRows() { |
||
284 | |||
285 | /** |
||
286 | * @param callback $fn |
||
287 | * @return mixed |
||
288 | * @throws \Exception |
||
289 | */ |
||
290 | private function createTempStatement($fn) { |
||
302 | |||
303 | /** |
||
304 | * @return QueryStatement |
||
305 | */ |
||
306 | private function createStatement() { |
||
316 | |||
317 | /** |
||
318 | * @return Traversable|array[]|\Generator |
||
319 | */ |
||
320 | public function getIterator() { |
||
323 | } |
||
324 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.