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 Package 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 Package, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Package extends Model |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string CRC32 temporary uniq id of the package |
||
| 29 | */ |
||
| 30 | private $_id; |
||
| 31 | |||
| 32 | /** @var Tariff */ |
||
| 33 | protected $_tariff; |
||
| 34 | |||
| 35 | /** @var Part[] */ |
||
| 36 | public $parts = []; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | public $calculation; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var |
||
| 43 | */ |
||
| 44 | protected $_resources; |
||
| 45 | |||
| 46 | public function init() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Tariff $tariff |
||
| 59 | */ |
||
| 60 | public function setTariff($tariff) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @throws InvalidConfigException |
||
| 67 | * TODO: implement and get rid of many magic functions bellow |
||
| 68 | */ |
||
| 69 | protected function initResources() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $resource |
||
| 83 | * @return string |
||
| 84 | * TODO: implement and get rid of many magic functions bellow |
||
| 85 | */ |
||
| 86 | protected function buildResourceClass($resource) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return Tariff |
||
| 97 | */ |
||
| 98 | public function getTariff() |
||
| 102 | |||
| 103 | protected function getResourceTitle_cpu() |
||
| 107 | |||
| 108 | protected function getResourceValue_cpu() |
||
| 115 | |||
| 116 | protected function getResourceTitle_ram() |
||
| 120 | |||
| 121 | protected function getResourceValue_ram() |
||
| 127 | |||
| 128 | View Code Duplication | protected function getResourceOveruse_ram() |
|
| 135 | |||
| 136 | protected function getResourceTitle_hdd() |
||
| 140 | |||
| 141 | protected function getResourceValue_hdd() |
||
| 147 | |||
| 148 | View Code Duplication | protected function getResourceOveruse_hdd() |
|
| 156 | |||
| 157 | protected function getResourceTitle_ip() |
||
| 161 | |||
| 162 | protected function getResourceValue_ip() |
||
| 166 | |||
| 167 | protected function getResourceTitle_chassis() |
||
| 171 | |||
| 172 | protected function getResourceValue_chassis() |
||
| 176 | |||
| 177 | View Code Duplication | protected function getResourceOveruse_ip() |
|
| 185 | |||
| 186 | protected function getResourceTitle_support_time() |
||
| 190 | |||
| 191 | protected function getResourceValue_support_time() |
||
| 206 | |||
| 207 | View Code Duplication | protected function getResourceOveruse_traffic() |
|
| 215 | |||
| 216 | protected function getResourceTitle_traffic() |
||
| 220 | |||
| 221 | protected function getResourceValue_traffic() |
||
| 226 | |||
| 227 | protected function getResourceValue_speed() |
||
| 231 | |||
| 232 | protected function getResourceTitle_speed() |
||
| 236 | |||
| 237 | protected function getResourceValue_panel() |
||
| 246 | |||
| 247 | protected function getResourceTitle_panel() |
||
| 251 | |||
| 252 | protected function getResourceValue_purpose() |
||
| 256 | |||
| 257 | protected function getResourceTitle_purpose() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return float |
||
| 264 | */ |
||
| 265 | public function getPrice() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @throws InvalidConfigException |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getDisplayPrice() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getName() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $type |
||
| 291 | * @throws InvalidConfigException |
||
| 292 | * @return mixed |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function getResourceValue($type) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $type |
||
| 307 | * @throws InvalidConfigException |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function getResourceTitle($type) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param $type |
||
| 322 | * @throws InvalidConfigException |
||
| 323 | * @return array|null |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function getOverusePrice($type) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $type |
||
| 337 | * @return Part|null |
||
| 338 | */ |
||
| 339 | public function getPartByType($type) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $type |
||
| 352 | * @return Resource|null |
||
| 353 | */ |
||
| 354 | public function getResourceByType($type) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $type |
||
| 367 | * @return resource|null |
||
| 368 | */ |
||
| 369 | public function getResourceByModelType($type) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function getLocations() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function getId() |
||
| 401 | } |
||
| 402 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: