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 Payone_ClientApi_Request_Abstract 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 Payone_ClientApi_Request_Abstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class Payone_ClientApi_Request_Abstract implements Payone_ClientApi_Request_Interface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | protected $mid = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $portalid = 0; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $key = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $mode = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $request = ''; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $responsetype; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $encoding = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * name of the solution-partner (company) |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $solution_name = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * version of the solution-partner's app / extension / plugin / etc.. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $solution_version = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * system-name |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $integrator_name = ''; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * system-version |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $integrator_version = ''; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * ISO 639 |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $language = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $hash = ''; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $data |
||
| 111 | */ |
||
| 112 | public function __construct(array $data = array()) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $data |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function init(array $data = array()) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function toArray() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function __toString() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param $name |
||
| 169 | * @return null|mixed |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function get($name) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $name |
||
| 202 | * @param mixed $value |
||
| 203 | * @return boolean|null |
||
| 204 | */ |
||
| 205 | public function set($name, $value) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param string $encoding |
||
| 217 | */ |
||
| 218 | public function setEncoding($encoding) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getEncoding() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $key |
||
| 233 | */ |
||
| 234 | public function setKey($key) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function getKey() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param int $mid |
||
| 249 | */ |
||
| 250 | public function setMid($mid) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | public function getMid() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $mode |
||
| 265 | */ |
||
| 266 | public function setMode($mode) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getMode() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param int $portalid |
||
| 281 | */ |
||
| 282 | public function setPortalid($portalid) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return int |
||
| 289 | */ |
||
| 290 | public function getPortalid() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $request |
||
| 297 | */ |
||
| 298 | public function setRequest($request) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getRequest() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $responseType |
||
| 313 | */ |
||
| 314 | public function setResponsetype($responseType) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getResponseType() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * set the system-Name |
||
| 329 | * |
||
| 330 | * @param string $integrator_name |
||
| 331 | */ |
||
| 332 | public function setIntegratorName($integrator_name) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getIntegratorName() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * set the system-version |
||
| 347 | * |
||
| 348 | * @param string $integrator_version |
||
| 349 | */ |
||
| 350 | public function setIntegratorVersion($integrator_version) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getIntegratorVersion() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * set the name of the solution-partner (company) |
||
| 365 | * |
||
| 366 | * @param string $solution_name |
||
| 367 | */ |
||
| 368 | public function setSolutionName($solution_name) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getSolutionName() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * set the version of the solution-partner's app / extension / plugin / etc.. |
||
| 383 | * |
||
| 384 | * @param string $solution_version |
||
| 385 | */ |
||
| 386 | public function setSolutionVersion($solution_version) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getSolutionVersion() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $hash |
||
| 401 | */ |
||
| 402 | public function setHash($hash) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public function getHash() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $language |
||
| 417 | */ |
||
| 418 | public function setLanguage($language) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getLanguage() |
||
| 430 | |||
| 431 | } |
||
| 432 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.