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:
1 | <?php |
||
17 | class StatsJsonView extends BaseJsonView |
||
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']; |
||
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() |
|
163 | |||
164 | /** |
||
165 | * Set the data source. |
||
166 | * |
||
167 | * @param string $source Data source to return. |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 5 | public function setSource(string $source): void |
|
175 | |||
176 | /** |
||
177 | * Process the raw data into the response data format. |
||
178 | * |
||
179 | * @param array $data The raw data array. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | 8 | private function buildResponseData(array $data): array |
|
210 | |||
211 | /** |
||
212 | * Process the response for a single data source. |
||
213 | * |
||
214 | * @param array $items The source items to process. |
||
215 | * |
||
216 | * @return string The rendered view. |
||
217 | */ |
||
218 | 3 | private function processSingleSource(array $items): string |
|
248 | |||
249 | /** |
||
250 | * Sanitize the response data into summarized groups. |
||
251 | * |
||
252 | * @param array $responseData The response data to sanitize. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | 6 | private function sanitizeData(array $responseData): array |
|
340 | } |
||
341 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.