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 | class StatsJsonView extends BaseJsonView |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Flag if the response should return the raw data. |
||
| 17 | * |
||
| 18 | * @var boolean |
||
| 19 | * @since 1.0 |
||
| 20 | */ |
||
| 21 | private $authorizedRaw = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Array holding the valid data sources. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | * @since 1.0 |
||
| 28 | */ |
||
| 29 | private $dataSources = ['php_version', 'db_type', 'db_version', 'cms_version', 'server_os']; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The data source to return. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | * @since 1.0 |
||
| 36 | */ |
||
| 37 | private $source = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Count of the number of items. |
||
| 41 | * |
||
| 42 | * @var integer |
||
| 43 | * @since 1.0 |
||
| 44 | */ |
||
| 45 | private $totalItems = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Instantiate the view. |
||
| 49 | * |
||
| 50 | * @param StatsModel $model The model object. |
||
| 51 | * |
||
| 52 | * @since 1.0 |
||
| 53 | */ |
||
| 54 | public function __construct(StatsModel $model) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set whether the raw data should be returned. |
||
| 61 | * |
||
| 62 | * @param bool $authorizedRaw Flag if the response should return the raw data. |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | * |
||
| 66 | * @since 1.0 |
||
| 67 | */ |
||
| 68 | 1 | public function isAuthorizedRaw(bool $authorizedRaw) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Method to render the view. |
||
| 75 | * |
||
| 76 | * @return string The rendered view. |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @throws \InvalidArgumentException |
||
| 80 | */ |
||
| 81 | public function render() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set the data source. |
||
| 155 | * |
||
| 156 | * @param string $source Data source to return. |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | * |
||
| 160 | * @since 1.0 |
||
| 161 | */ |
||
| 162 | 1 | public function setSource(string $source) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Process the raw data into the response data format. |
||
| 169 | * |
||
| 170 | * @param array $data The raw data array. |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | * |
||
| 174 | * @since 1.0 |
||
| 175 | */ |
||
| 176 | private function buildResponseData(array $data) : array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Process the response for a single data source. |
||
| 206 | * |
||
| 207 | * @param \Generator $generator The source items to process. |
||
| 208 | * |
||
| 209 | * @return string The rendered view. |
||
| 210 | * |
||
| 211 | * @since 1.0 |
||
| 212 | */ |
||
| 213 | private function processSingleSource(\Generator $generator) : string |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Sanitize the response data into summarized groups. |
||
| 266 | * |
||
| 267 | * @param array $responseData The response data to sanitize. |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | * |
||
| 271 | * @since 1.0 |
||
| 272 | */ |
||
| 273 | private function sanitizeData(array $responseData) : array |
||
| 357 | } |
||
| 358 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..