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 |
||
17 | class StatsJsonView extends JsonView |
||
18 | { |
||
19 | /** |
||
20 | * Flag if the response should return the raw data. |
||
21 | * |
||
22 | * @var boolean |
||
23 | */ |
||
24 | private $authorizedRaw = false; |
||
25 | |||
26 | /** |
||
27 | * Array holding the valid data sources. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $dataSources = ['php_version', 'db_type', 'db_version', 'cms_version', 'server_os', 'cms_php_version', 'db_type_version']; |
||
32 | |||
33 | /** |
||
34 | * Flag if the response should return the recently updated data. |
||
35 | * |
||
36 | * @var boolean |
||
37 | */ |
||
38 | private $recent = false; |
||
39 | |||
40 | /** |
||
41 | * Statistics repository. |
||
42 | * |
||
43 | * @var StatisticsRepository |
||
44 | */ |
||
45 | private $repository; |
||
46 | |||
47 | /** |
||
48 | * The data source to return. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $source = ''; |
||
53 | |||
54 | /** |
||
55 | * Count of the number of items. |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | private $totalItems = 0; |
||
60 | |||
61 | /** |
||
62 | * Instantiate the view. |
||
63 | * |
||
64 | * @param StatisticsRepository $repository Statistics repository. |
||
65 | */ |
||
66 | 8 | public function __construct(StatisticsRepository $repository) |
|
70 | |||
71 | /** |
||
72 | * Set whether the raw data should be returned. |
||
73 | * |
||
74 | * @param bool $authorizedRaw Flag if the response should return the raw data. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 4 | public function isAuthorizedRaw(bool $authorizedRaw): void |
|
82 | |||
83 | /** |
||
84 | * Set whether the recently updated data should be returned. |
||
85 | * |
||
86 | * @param bool $recent Flag if the response should return the recently updated data. |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | 1 | public function isRecent(bool $recent): void |
|
94 | |||
95 | /** |
||
96 | * Method to render the view. |
||
97 | * |
||
98 | * @return string The rendered view. |
||
99 | */ |
||
100 | 8 | public function render() |
|
204 | |||
205 | /** |
||
206 | * Set the data source. |
||
207 | * |
||
208 | * @param string $source Data source to return. |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | 5 | public function setSource(string $source): void |
|
216 | |||
217 | /** |
||
218 | * Process the raw data into the response data format. |
||
219 | * |
||
220 | * @param array $data The raw data array. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | 8 | private function buildResponseData(array $data): array |
|
251 | |||
252 | /** |
||
253 | * Process the response for a single data source. |
||
254 | * |
||
255 | * @param array $items The source items to process. |
||
256 | * |
||
257 | * @return string The rendered view. |
||
258 | */ |
||
259 | 3 | private function processSingleSource(array $items): string |
|
306 | |||
307 | /** |
||
308 | * Sanitize the response data into summarized groups. |
||
309 | * |
||
310 | * @param array $responseData The response data to sanitize. |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | 6 | private function sanitizeData(array $responseData): array |
|
400 | } |
||
401 |
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.