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 | /** |
||
15 | * JSON response for requesting the stats data. |
||
16 | */ |
||
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 | * The data source to return. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $source = ''; |
||
39 | |||
40 | /** |
||
41 | * Count of the number of items. |
||
42 | * |
||
43 | * @var integer |
||
44 | */ |
||
45 | private $totalItems = 0; |
||
46 | |||
47 | /** |
||
48 | * Instantiate the view. |
||
49 | * |
||
50 | * @param StatsModel $model The model object. |
||
51 | */ |
||
52 | public function __construct(StatsModel $model) |
||
56 | |||
57 | /** |
||
58 | * Set whether the raw data should be returned. |
||
59 | * |
||
60 | * @param bool $authorizedRaw Flag if the response should return the raw data. |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | 1 | public function isAuthorizedRaw(bool $authorizedRaw) |
|
68 | |||
69 | /** |
||
70 | * Method to render the view. |
||
71 | * |
||
72 | * @return string The rendered view. |
||
73 | */ |
||
74 | 4 | public function render() |
|
133 | |||
134 | /** |
||
135 | * Set the data source. |
||
136 | * |
||
137 | * @param string $source Data source to return. |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 1 | public function setSource(string $source) |
|
145 | |||
146 | /** |
||
147 | * Process the raw data into the response data format. |
||
148 | * |
||
149 | * @param array $data The raw data array. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 4 | private function buildResponseData(array $data) : array |
|
180 | |||
181 | /** |
||
182 | * Process the response for a single data source. |
||
183 | * |
||
184 | * @param array $items The source items to process. |
||
185 | * |
||
186 | * @return string The rendered view. |
||
187 | */ |
||
188 | 2 | private function processSingleSource(array $items) : string |
|
218 | |||
219 | /** |
||
220 | * Sanitize the response data into summarized groups. |
||
221 | * |
||
222 | * @param array $responseData The response data to sanitize. |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | 3 | private function sanitizeData(array $responseData) : array |
|
310 | } |
||
311 |
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: