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 StatsJsonView 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 StatsJsonView, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class StatsJsonView extends BaseJsonView |
||
15 | { |
||
16 | /** |
||
17 | * Flag if the response should return the raw data. |
||
18 | * |
||
19 | * @var boolean |
||
20 | * @since 1.0 |
||
21 | */ |
||
22 | private $authorizedRaw = false; |
||
23 | |||
24 | /** |
||
25 | * Array holding the valid data sources. |
||
26 | * |
||
27 | * @var array |
||
28 | * @since 1.0 |
||
29 | */ |
||
30 | private $dataSources = ['php_version', 'db_type', 'db_version', 'cms_version', 'server_os']; |
||
31 | |||
32 | /** |
||
33 | * The data source to return. |
||
34 | * |
||
35 | * @var string |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | private $source; |
||
39 | |||
40 | /** |
||
41 | * Count of the number of items. |
||
42 | * |
||
43 | * @var integer |
||
44 | * @since 1.0 |
||
45 | */ |
||
46 | private $totalItems = 0; |
||
47 | |||
48 | /** |
||
49 | * Set whether the raw data should be returned. |
||
50 | * |
||
51 | * @param boolean $authorizedRaw Flag if the response should return the raw data. |
||
52 | * |
||
53 | * @return void |
||
54 | * |
||
55 | * @since 1.0 |
||
56 | */ |
||
57 | 1 | public function isAuthorizedRaw($authorizedRaw) |
|
61 | |||
62 | /** |
||
63 | * Method to render the view. |
||
64 | * |
||
65 | * @return string The rendered view. |
||
66 | * |
||
67 | * @since 1.0 |
||
68 | * @throws \InvalidArgumentException |
||
69 | */ |
||
70 | 4 | public function render() |
|
138 | |||
139 | /** |
||
140 | * Set the data source. |
||
141 | * |
||
142 | * @param string $source Data source to return. |
||
143 | * |
||
144 | * @return void |
||
145 | * |
||
146 | * @since 1.0 |
||
147 | */ |
||
148 | 1 | public function setSource($source) |
|
152 | |||
153 | /** |
||
154 | * Process the raw data into the response data format. |
||
155 | * |
||
156 | * @param array $data The raw data array. |
||
157 | * |
||
158 | * @return array |
||
159 | * |
||
160 | * @since 1.0 |
||
161 | */ |
||
162 | 4 | private function buildResponseData(array $data) |
|
187 | |||
188 | /** |
||
189 | * Process the response for a single data source. |
||
190 | * |
||
191 | * @param array $items The source items to process. |
||
192 | * |
||
193 | * @return string The rendered view. |
||
194 | * |
||
195 | * @since 1.0 |
||
196 | */ |
||
197 | 2 | private function processSingleSource(array $items) |
|
242 | |||
243 | /** |
||
244 | * Sanitize the response data into summarized groups. |
||
245 | * |
||
246 | * @param array $responseData The response data to sanitize. |
||
247 | * |
||
248 | * @return array |
||
249 | * |
||
250 | * @since 1.0 |
||
251 | */ |
||
252 | 3 | private function sanitizeData(array $responseData) |
|
336 | } |
||
337 |