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:
Complex classes like StatsJsonView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StatsJsonView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | |||
| 14 | /** |
||
| 15 | * JSON response for requesting the stats data. |
||
| 16 | * |
||
| 17 | * @since 1.0 |
||
| 18 | */ |
||
| 19 | class StatsJsonView extends BaseJsonView |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Flag if the response should return the raw data. |
||
| 23 | * |
||
| 24 | * @var boolean |
||
| 25 | * @since 1.0 |
||
| 26 | */ |
||
| 27 | private $authorizedRaw = false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Array holding the valid data sources. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | * @since 1.0 |
||
| 34 | */ |
||
| 35 | private $dataSources = ['php_version', 'db_type', 'db_version', 'cms_version', 'server_os']; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The data source to return. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | * @since 1.0 |
||
| 42 | */ |
||
| 43 | private $source = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Count of the number of items. |
||
| 47 | * |
||
| 48 | * @var integer |
||
| 49 | * @since 1.0 |
||
| 50 | */ |
||
| 51 | private $totalItems = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Instantiate the view. |
||
| 55 | * |
||
| 56 | * @param StatsModel $model The model object. |
||
| 57 | * |
||
| 58 | * @since 1.0 |
||
| 59 | */ |
||
| 60 | public function __construct(StatsModel $model) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set whether the raw data should be returned. |
||
| 67 | * |
||
| 68 | * @param bool $authorizedRaw Flag if the response should return the raw data. |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | * |
||
| 72 | * @since 1.0 |
||
| 73 | */ |
||
| 74 | 1 | public function isAuthorizedRaw(bool $authorizedRaw) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Method to render the view. |
||
| 81 | * |
||
| 82 | * @return string The rendered view. |
||
| 83 | * |
||
| 84 | * @since 1.0 |
||
| 85 | * @throws \InvalidArgumentException |
||
| 86 | */ |
||
| 87 | public function render() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set the data source. |
||
| 161 | * |
||
| 162 | * @param string $source Data source to return. |
||
| 163 | * |
||
| 164 | * @return void |
||
| 165 | * |
||
| 166 | * @since 1.0 |
||
| 167 | */ |
||
| 168 | 1 | public function setSource(string $source) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Process the raw data into the response data format. |
||
| 175 | * |
||
| 176 | * @param array $data The raw data array. |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | * |
||
| 180 | * @since 1.0 |
||
| 181 | */ |
||
| 182 | private function buildResponseData(array $data) : array |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Process the response for a single data source. |
||
| 212 | * |
||
| 213 | * @param \Generator $generator The source items to process. |
||
| 214 | * |
||
| 215 | * @return string The rendered view. |
||
| 216 | * |
||
| 217 | * @since 1.0 |
||
| 218 | */ |
||
| 219 | private function processSingleSource(\Generator $generator) : string |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sanitize the response data into summarized groups. |
||
| 272 | * |
||
| 273 | * @param array $responseData The response data to sanitize. |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | * |
||
| 277 | * @since 1.0 |
||
| 278 | */ |
||
| 279 | private function sanitizeData(array $responseData) : array |
||
| 280 | { |
||
| 281 | foreach ($responseData as $key => $dataGroup) |
||
| 282 | { |
||
| 283 | switch ($key) |
||
| 284 | { |
||
| 285 | case 'php_version': |
||
| 286 | case 'db_version': |
||
| 287 | // We're going to group by minor version branch here and convert to a percentage |
||
| 288 | $counts = []; |
||
| 289 | |||
| 290 | foreach ($dataGroup as $row) |
||
| 291 | { |
||
| 292 | $exploded = explode('.', $row['name']); |
||
| 293 | $version = $exploded[0] . '.' . (isset($exploded[1]) ? $exploded[1] : '0'); |
||
| 294 | |||
| 295 | // If the container does not exist, add it |
||
| 296 | if (!isset($counts[$version])) |
||
| 297 | { |
||
| 298 | $counts[$version] = 0; |
||
| 299 | } |
||
| 300 | |||
| 301 | $counts[$version] += $row['count']; |
||
| 302 | } |
||
| 303 | |||
| 304 | $sanitizedData = []; |
||
| 305 | |||
| 306 | View Code Duplication | foreach ($counts as $version => $count) |
|
| 307 | { |
||
| 308 | $sanitizedData[$version] = round(($count / $this->totalItems) * 100, 2); |
||
| 309 | } |
||
| 310 | |||
| 311 | $responseData[$key] = $sanitizedData; |
||
| 312 | |||
| 313 | break; |
||
| 314 | |||
| 315 | case 'server_os': |
||
| 316 | // We're going to group by operating system here |
||
| 317 | $counts = []; |
||
| 318 | |||
| 319 | foreach ($dataGroup as $row) |
||
| 320 | { |
||
| 321 | $fullOs = explode(' ', $row['name']); |
||
| 322 | $os = $fullOs[0]; |
||
| 323 | |||
| 324 | // If the container does not exist, add it |
||
| 325 | if (!isset($counts[$os])) |
||
| 326 | { |
||
| 327 | $counts[$os] = 0; |
||
| 328 | } |
||
| 329 | |||
| 330 | $counts[$os] += $row['count']; |
||
| 331 | } |
||
| 332 | |||
| 333 | $sanitizedData = []; |
||
| 334 | |||
| 335 | View Code Duplication | foreach ($counts as $os => $count) |
|
| 336 | { |
||
| 337 | $sanitizedData[$os] = round(($count / $this->totalItems) * 100, 2); |
||
| 338 | } |
||
| 339 | |||
| 340 | $responseData[$key] = $sanitizedData; |
||
| 341 | |||
| 342 | break; |
||
| 343 | |||
| 344 | case 'db_type': |
||
| 345 | case 'cms_version': |
||
| 346 | default: |
||
| 347 | // For now, group by the object name and figure out the percentages |
||
| 348 | $sanitizedData = []; |
||
| 349 | |||
| 350 | foreach ($dataGroup as $row) |
||
| 351 | { |
||
| 352 | $sanitizedData[$row['name']] = round(($row['count'] / $this->totalItems) * 100, 2); |
||
| 353 | } |
||
| 354 | |||
| 355 | $responseData[$key] = $sanitizedData; |
||
| 356 | |||
| 357 | break; |
||
| 358 | } |
||
| 364 |
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: