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 |
||
| 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 | public function render() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set the data source. |
||
| 143 | * |
||
| 144 | * @param string $source Data source to return. |
||
| 145 | * |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | 1 | public function setSource(string $source) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Process the raw data into the response data format. |
||
| 155 | * |
||
| 156 | * @param array $data The raw data array. |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | private function buildResponseData(array $data) : array |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Process the response for a single data source. |
||
| 190 | * |
||
| 191 | * @param \Generator $items The source items to process. |
||
| 192 | * |
||
| 193 | * @return string The rendered view. |
||
| 194 | */ |
||
| 195 | private function processSingleSource(\Generator $items) : string |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sanitize the response data into summarized groups. |
||
| 245 | * |
||
| 246 | * @param array $responseData The response data to sanitize. |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | private function sanitizeData(array $responseData) : array |
||
| 334 | } |
||
| 335 |
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: