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 Result 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 Result, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Result extends Response { |
||
7 | const VOID = 0x0001; |
||
8 | const ROWS = 0x0002; |
||
9 | const SET_KEYSPACE = 0x0003; |
||
10 | const PREPARED = 0x0004; |
||
11 | const SCHEMA_CHANGE = 0x0005; |
||
12 | |||
13 | const ROWS_FLAG_GLOBAL_TABLES_SPEC = 0x0001; |
||
14 | const ROWS_FLAG_HAS_MORE_PAGES = 0x0002; |
||
15 | const ROWS_FLAG_NO_METADATA = 0x0004; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $_kind; |
||
22 | |||
23 | /** |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $_metadata; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $_rowClass; |
||
34 | |||
35 | /** |
||
36 | * @return \SplFixedArray|string|array|null |
||
37 | */ |
||
38 | public function getData() { |
||
67 | |||
68 | public function getKind(){ |
||
76 | |||
77 | /** |
||
78 | * |
||
79 | * @param array $metadata |
||
80 | * @return self |
||
81 | */ |
||
82 | public function setMetadata(array $metadata) { |
||
87 | |||
88 | /** |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getMetadata(){ |
||
100 | |||
101 | /** |
||
102 | * |
||
103 | * @param string $rowClass |
||
104 | * @return self |
||
105 | */ |
||
106 | public function setRowClass($rowClass) { |
||
111 | |||
112 | /** |
||
113 | * Return metadata |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function _readMetadata() { |
||
156 | |||
157 | /** |
||
158 | * |
||
159 | * @param string $rowClass |
||
160 | * @throws Exception |
||
161 | * @return \SplFixedArray |
||
162 | */ |
||
163 | public function fetchAll($rowClass = null){ |
||
190 | |||
191 | /** |
||
192 | * |
||
193 | * @param int $index |
||
194 | * @throws Exception |
||
195 | * @return \SplFixedArray |
||
196 | */ |
||
197 | public function fetchCol($index = 0){ |
||
222 | |||
223 | /** |
||
224 | * |
||
225 | * @throws Exception |
||
226 | * @return array |
||
227 | */ |
||
228 | public function fetchPairs(){ |
||
257 | |||
258 | /** |
||
259 | * |
||
260 | * @param string $rowClass |
||
261 | * @throws Exception |
||
262 | * @return \ArrayObject |
||
263 | */ |
||
264 | public function fetchRow($rowClass = null){ |
||
288 | |||
289 | /** |
||
290 | * |
||
291 | * @throws Exception |
||
292 | * @return mixed |
||
293 | */ |
||
294 | public function fetchOne(){ |
||
314 | } |
||
315 |
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.