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 |
||
| 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 |
||
| 363 | } |
||
| 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: