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() |
|
71 | { |
||
72 | 4 | $items = $this->model->getItems($this->source); |
|
73 | |||
74 | // Null out the model now to free some memory |
||
75 | 4 | $this->model = null; |
|
76 | |||
77 | 4 | if ($this->source) |
|
78 | 4 | { |
|
79 | 2 | return $this->processSingleSource($items); |
|
|
|||
80 | } |
||
81 | |||
82 | 2 | $php_version = []; |
|
83 | 2 | $db_type = []; |
|
84 | 2 | $db_version = []; |
|
85 | 2 | $cms_version = []; |
|
86 | 2 | $server_os = []; |
|
87 | |||
88 | // If we have the entire database, we have to loop within each group to put it all together |
||
89 | 2 | foreach ($items as $group) |
|
90 | { |
||
91 | 2 | $this->totalItems = 0; |
|
92 | 2 | foreach ($group as $item) |
|
93 | { |
||
94 | 2 | foreach ($this->dataSources as $source) |
|
95 | { |
||
96 | 2 | if (isset($item[$source]) && !is_null($item[$source])) |
|
97 | 2 | { |
|
98 | // Special case, if the server is empty then change the key to "unknown" |
||
99 | 2 | if ($source === 'server_os' && empty(trim($item[$source]))) |
|
100 | 2 | { |
|
101 | 2 | $item[$source] = 'unknown'; |
|
102 | 2 | } |
|
103 | |||
104 | 2 | ${$source}[$item[$source]] = $item['count']; |
|
105 | |||
106 | 2 | $this->totalItems += $item['count']; |
|
107 | 2 | } |
|
108 | 2 | } |
|
109 | 2 | } |
|
110 | 2 | } |
|
111 | |||
112 | 2 | unset($items); |
|
113 | |||
114 | $data = [ |
||
115 | 2 | 'php_version' => $php_version, |
|
116 | 2 | 'db_type' => $db_type, |
|
117 | 2 | 'db_version' => $db_version, |
|
118 | 2 | 'cms_version' => $cms_version, |
|
119 | 2 | 'server_os' => $server_os, |
|
120 | 2 | ]; |
|
121 | |||
122 | 2 | $responseData = $this->buildResponseData($data); |
|
123 | |||
124 | 2 | $responseData['total'] = $this->totalItems; |
|
125 | |||
126 | 2 | $this->addData('data', $responseData); |
|
127 | |||
128 | 2 | return parent::render(); |
|
129 | } |
||
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 | 4 | 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) |
|
221 | |||
222 | /** |
||
223 | * Sanitize the response data into summarized groups. |
||
224 | * |
||
225 | * @param array $responseData The response data to sanitize. |
||
226 | * |
||
227 | * @return array |
||
228 | * |
||
229 | * @since 1.0 |
||
230 | */ |
||
231 | 3 | private function sanitizeData(array $responseData) |
|
315 | } |
||
316 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.