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 Pivot 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 Pivot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class Pivot |
||
5 | { |
||
6 | /** |
||
7 | * Table name, View name or SELECT instruction to extract the values |
||
8 | * @var string |
||
9 | */ |
||
10 | private $source; |
||
11 | |||
12 | /** |
||
13 | * Collection of all fields used in the source |
||
14 | * @var Fields|Field[] |
||
15 | */ |
||
16 | private $fields; |
||
17 | |||
18 | /** |
||
19 | * Collection of rules |
||
20 | * @var Filters|Filter[] |
||
21 | */ |
||
22 | private $filters; |
||
23 | |||
24 | /** |
||
25 | * Collection of columns |
||
26 | * @var array |
||
27 | */ |
||
28 | private $columns; |
||
29 | |||
30 | /** |
||
31 | * Collection of rows |
||
32 | * @var array |
||
33 | */ |
||
34 | private $rows; |
||
35 | |||
36 | /** |
||
37 | * Collection of aggregates |
||
38 | * @var Aggregates|Aggregate[] |
||
39 | */ |
||
40 | private $aggregates; |
||
41 | |||
42 | /** |
||
43 | * Information of extended information |
||
44 | * @var array $info |
||
45 | */ |
||
46 | private $info; |
||
47 | |||
48 | 18 | public function __construct($source = '') |
|
59 | |||
60 | 5 | public function __clone() |
|
73 | |||
74 | /** |
||
75 | * @param string $source |
||
76 | * |
||
77 | * @throws PivotException If the source is not a string |
||
78 | */ |
||
79 | 18 | public function setSource($source) |
|
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | 13 | public function getSource() |
|
94 | |||
95 | /** |
||
96 | * Return a current value of information |
||
97 | * If keyword not found return FALSE |
||
98 | * If no keyword provided then it returns the complete array of values |
||
99 | * |
||
100 | * @param string $keyword |
||
101 | * @return mixed |
||
102 | */ |
||
103 | 3 | public function getInfo($keyword = '') |
|
113 | |||
114 | /** |
||
115 | * @param string $keyword |
||
116 | * @param mixed $value |
||
117 | * @throws PivotException |
||
118 | */ |
||
119 | 5 | public function setInfo($keyword, $value) |
|
128 | |||
129 | 11 | public function clearSelectors() |
|
136 | |||
137 | 11 | public function clearSelectorRows() |
|
141 | |||
142 | 11 | public function clearFilters() |
|
146 | |||
147 | 11 | public function clearAggregates() |
|
151 | |||
152 | 11 | public function clearSelectorColumns() |
|
156 | |||
157 | 18 | public function clearInfo() |
|
166 | |||
167 | 11 | public function reset() |
|
173 | |||
174 | 13 | public function addSourceField($fieldname, $caption, $type) |
|
178 | |||
179 | 6 | public function addFilter($fieldname, $operator, $arguments) |
|
186 | |||
187 | 8 | View Code Duplication | public function addColumn($fieldname) |
196 | |||
197 | 11 | View Code Duplication | public function addRow($fieldname) |
206 | |||
207 | /** |
||
208 | * @param string $fieldname |
||
209 | * @param string $asname |
||
210 | * @param string $caption |
||
211 | * @param string $agregatorfunction |
||
212 | * @param int $decimals |
||
213 | * @param string $order |
||
214 | */ |
||
215 | 12 | public function addAggregate( |
|
229 | |||
230 | 4 | public function getSourceFields() |
|
234 | |||
235 | 2 | public function getCurrentFilters() |
|
243 | |||
244 | 11 | View Code Duplication | public function getCurrentColumns() |
256 | |||
257 | 12 | View Code Duplication | public function getCurrentRows() |
269 | |||
270 | 9 | public function hasColumns() |
|
274 | |||
275 | public function hasRows() |
||
279 | |||
280 | 11 | public function getCurrentAggregates() |
|
284 | |||
285 | /** |
||
286 | * Return an field object from the field list |
||
287 | * @param string $fieldname |
||
288 | * @return Field |
||
289 | * @throws PivotException when field name does not exists |
||
290 | */ |
||
291 | 10 | public function getFieldElement($fieldname) |
|
298 | |||
299 | public function getFieldsCollection() |
||
303 | |||
304 | 10 | public function getAggregatesCollection() |
|
308 | |||
309 | 9 | public function getFiltersCollection() |
|
313 | } |
||
314 |
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.