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 |
||
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() |
|
130 | |||
131 | /** |
||
132 | * Set the data source. |
||
133 | * |
||
134 | * @param string $source Data source to return. |
||
135 | * |
||
136 | * @return void |
||
137 | * |
||
138 | * @since 1.0 |
||
139 | */ |
||
140 | 1 | public function setSource($source) |
|
144 | |||
145 | /** |
||
146 | * Process the raw data into the response data format. |
||
147 | * |
||
148 | * @param array $data The raw data array. |
||
149 | * |
||
150 | * @return array |
||
151 | * |
||
152 | * @since 1.0 |
||
153 | */ |
||
154 | 2 | private function buildResponseData(array $data) |
|
181 | |||
182 | /** |
||
183 | * Process the response for a single data source. |
||
184 | * |
||
185 | * @param array $items The source items to process. |
||
186 | * |
||
187 | * @return string The rendered view. |
||
188 | * |
||
189 | * @since 1.0 |
||
190 | */ |
||
191 | 2 | private function processSingleSource(array $items) |
|
219 | |||
220 | /** |
||
221 | * Sanitize the response data into summarized groups. |
||
222 | * |
||
223 | * @param array $responseData The response data to sanitize. |
||
224 | * |
||
225 | * @return array |
||
226 | * |
||
227 | * @since 1.0 |
||
228 | */ |
||
229 | 2 | private function sanitizeData(array $responseData) |
|
313 | } |
||
314 |
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.