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 | * The data source to return. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $source = ''; |
||
46 | |||
47 | /** |
||
48 | * Count of the number of items. |
||
49 | * |
||
50 | * @var integer |
||
51 | */ |
||
52 | private $totalItems = 0; |
||
53 | |||
54 | /** |
||
55 | * Instantiate the view. |
||
56 | * |
||
57 | * @param StatsModel $model The model object. |
||
58 | */ |
||
59 | public function __construct(StatsModel $model) |
||
63 | |||
64 | /** |
||
65 | * Set whether the raw data should be returned. |
||
66 | * |
||
67 | * @param bool $authorizedRaw Flag if the response should return the raw data. |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | 1 | public function isAuthorizedRaw(bool $authorizedRaw) |
|
75 | |||
76 | /** |
||
77 | * Set whether the recently updated data should be returned. |
||
78 | * |
||
79 | * @param bool $recent Flag if the response should return the recently updated data. |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | public function isRecent(bool $recent) |
||
87 | |||
88 | /** |
||
89 | * Method to render the view. |
||
90 | * |
||
91 | * @return string The rendered view. |
||
92 | */ |
||
93 | 4 | public function render() |
|
159 | |||
160 | /** |
||
161 | * Set the data source. |
||
162 | * |
||
163 | * @param string $source Data source to return. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | 1 | public function setSource(string $source) |
|
171 | |||
172 | /** |
||
173 | * Process the raw data into the response data format. |
||
174 | * |
||
175 | * @param array $data The raw data array. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 4 | private function buildResponseData(array $data) : array |
|
206 | |||
207 | /** |
||
208 | * Process the response for a single data source. |
||
209 | * |
||
210 | * @param array $items The source items to process. |
||
211 | * |
||
212 | * @return string The rendered view. |
||
213 | */ |
||
214 | 2 | private function processSingleSource(array $items) : string |
|
244 | |||
245 | /** |
||
246 | * Sanitize the response data into summarized groups. |
||
247 | * |
||
248 | * @param array $responseData The response data to sanitize. |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | 3 | private function sanitizeData(array $responseData) : array |
|
336 | } |
||
337 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: