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_Api_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_Api_Request_Abstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class Payone_Api_Request_Abstract |
||
| 36 | implements Payone_Api_Request_Interface |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $mid = NULL; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $portalid = NULL; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $key = NULL; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $mode = NULL; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $request = NULL; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $encoding = NULL; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * name of the solution-partner (company) |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $solution_name = NULL; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * version of the solution-partner's app / extension / plugin / etc.. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $solution_version = NULL; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * system-name |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $integrator_name = NULL; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * system-version |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $integrator_version = NULL; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Payone_Protocol_Service_ApplyFilters |
||
| 98 | */ |
||
| 99 | private $applyFilters = NULL; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $data |
||
| 103 | */ |
||
| 104 | public function __construct(array $data = array()) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array $data |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function init(array $data = array()) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function toArray() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function __toString() |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @param $name |
||
| 174 | * @return null|mixed |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function get($name) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $name |
||
| 207 | * @param mixed $value |
||
| 208 | * @return boolean|null |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function set($name, $value) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $encoding |
||
| 242 | */ |
||
| 243 | public function setEncoding($encoding) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getEncoding() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $key |
||
| 258 | */ |
||
| 259 | public function setKey($key) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getKey() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param int $mid |
||
| 274 | */ |
||
| 275 | public function setMid($mid) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | public function getMid() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $mode |
||
| 290 | */ |
||
| 291 | public function setMode($mode) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function getMode() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param int $portalid |
||
| 306 | */ |
||
| 307 | public function setPortalid($portalid) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return int |
||
| 314 | */ |
||
| 315 | public function getPortalid() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $request |
||
| 322 | */ |
||
| 323 | public function setRequest($request) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getRequest() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * set the system-Name |
||
| 338 | * |
||
| 339 | * @param string $integrator_name |
||
| 340 | */ |
||
| 341 | public function setIntegratorName($integrator_name) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function getIntegratorName() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * set the system-version |
||
| 356 | * |
||
| 357 | * @param string $integrator_version |
||
| 358 | */ |
||
| 359 | public function setIntegratorVersion($integrator_version) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getIntegratorVersion() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * set the name of the solution-partner (company) |
||
| 374 | * |
||
| 375 | * @param string $solution_name |
||
| 376 | */ |
||
| 377 | public function setSolutionName($solution_name) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function getSolutionName() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * set the version of the solution-partner's app / extension / plugin / etc.. |
||
| 392 | * |
||
| 393 | * @param string $solution_version |
||
| 394 | */ |
||
| 395 | public function setSolutionVersion($solution_version) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getSolutionVersion() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param Payone_Protocol_Service_ApplyFilters $applyFilters |
||
| 410 | */ |
||
| 411 | public function setApplyFilters(Payone_Protocol_Service_ApplyFilters $applyFilters) |
||
| 415 | |||
| 416 | public function isFrontendApiCall() |
||
| 428 | |||
| 429 | } |
||
| 430 |
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.