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 |
||
| 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 | 2 | public function render() |
|
| 71 | { |
||
| 72 | 2 | $items = $this->model->getItems($this->source); |
|
| 73 | |||
| 74 | // Null out the model now to free some memory |
||
| 75 | 2 | $this->model = null; |
|
| 76 | |||
| 77 | 2 | if ($this->source) |
|
| 78 | 2 | { |
|
| 79 | 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 += count($group); |
|
| 92 | |||
| 93 | 2 | foreach ($group as $item) |
|
| 94 | { |
||
| 95 | 2 | foreach ($this->dataSources as $source) |
|
| 96 | { |
||
| 97 | 2 | if (isset($item[$source]) && !is_null($item[$source])) |
|
| 98 | 2 | { |
|
| 99 | // Special case, if the server is empty then change the key to "unknown" |
||
| 100 | 2 | if ($source === 'server_os' && empty($item[$source])) |
|
| 101 | 2 | { |
|
| 102 | 2 | if (!isset(${$source}['unknown'])) |
|
| 103 | 2 | { |
|
| 104 | 2 | ${$source}['unknown'] = 0; |
|
| 105 | 2 | } |
|
| 106 | |||
| 107 | 2 | ${$source}['unknown']++; |
|
| 108 | 2 | } |
|
| 109 | else |
||
| 110 | { |
||
| 111 | 2 | if (!isset(${$source}[$item[$source]])) |
|
| 112 | 2 | { |
|
| 113 | 2 | ${$source}[$item[$source]] = 0; |
|
| 114 | 2 | } |
|
| 115 | |||
| 116 | 2 | ${$source}[$item[$source]]++; |
|
| 117 | } |
||
| 118 | 2 | } |
|
| 119 | 2 | } |
|
| 120 | 2 | } |
|
| 121 | 2 | } |
|
| 122 | |||
| 123 | 2 | unset($items); |
|
| 124 | |||
| 125 | $data = [ |
||
| 126 | 2 | 'php_version' => $php_version, |
|
| 127 | 2 | 'db_type' => $db_type, |
|
| 128 | 2 | 'db_version' => $db_version, |
|
| 129 | 2 | 'cms_version' => $cms_version, |
|
| 130 | 2 | 'server_os' => $server_os, |
|
| 131 | 2 | ]; |
|
| 132 | |||
| 133 | 2 | $responseData = $this->buildResponseData($data); |
|
| 134 | |||
| 135 | 2 | $responseData['total'] = $this->totalItems; |
|
| 136 | |||
| 137 | 2 | $this->addData('data', $responseData); |
|
| 138 | |||
| 139 | 2 | return parent::render(); |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set the data source. |
||
| 144 | * |
||
| 145 | * @param string $source Data source to return. |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | * |
||
| 149 | * @since 1.0 |
||
| 150 | */ |
||
| 151 | 1 | public function setSource($source) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Process the raw data into the response data format. |
||
| 158 | * |
||
| 159 | * @param array $data The raw data array. |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | * |
||
| 163 | * @since 1.0 |
||
| 164 | */ |
||
| 165 | 2 | private function buildResponseData(array $data) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Process the response for a single data source. |
||
| 195 | * |
||
| 196 | * @param \Generator $generator The source items to process. |
||
| 197 | * |
||
| 198 | * @return string The rendered view. |
||
| 199 | * |
||
| 200 | * @since 1.0 |
||
| 201 | */ |
||
| 202 | private function processSingleSource(\Generator $generator) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Sanitize the response data into summarized groups. |
||
| 253 | * |
||
| 254 | * @param array $responseData The response data to sanitize. |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | * |
||
| 258 | * @since 1.0 |
||
| 259 | */ |
||
| 260 | 1 | private function sanitizeData(array $responseData) |
|
| 344 | } |
||
| 345 |
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 theSoncalls the wrong method in the parent class.