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:
| 1 | <?php |
||
| 14 | class Insurance extends Option |
||
| 15 | { |
||
| 16 | |||
| 17 | const INSURANCE_TYPE_BASIC_INSURANCE = 'basicInsurance'; |
||
| 18 | const INSURANCE_TYPE_ADDITIONAL_INSURANCE = 'additionalInsurance'; |
||
| 19 | |||
| 20 | const INSURANCE_AMOUNT_UP_TO_2500_EUROS = 2; |
||
| 21 | const INSURANCE_AMOUNT_UP_TO_5000_EUROS = 3; |
||
| 22 | const INSURANCE_AMOUNT_UP_TO_7500_EUROS = 4; |
||
| 23 | const INSURANCE_AMOUNT_UP_TO_10000_EUROS = 5; |
||
| 24 | const INSURANCE_AMOUNT_UP_TO_12500_EUROS = 6; |
||
| 25 | const INSURANCE_AMOUNT_UP_TO_15000_EUROS = 7; |
||
| 26 | const INSURANCE_AMOUNT_UP_TO_17500_EUROS = 8; |
||
| 27 | const INSURANCE_AMOUNT_UP_TO_20000_EUROS = 9; |
||
| 28 | const INSURANCE_AMOUNT_UP_TO_22500_EUROS = 10; |
||
| 29 | const INSURANCE_AMOUNT_UP_TO_25000_EUROS = 11; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $type; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $value; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | 2 | public static function getPossibleTypeValues() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $type |
||
| 54 | * @throws BpostInvalidValueException |
||
| 55 | */ |
||
| 56 | 2 | public function setType($type) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 1 | public function getType() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $value |
||
| 76 | * @throws BpostInvalidValueException |
||
| 77 | */ |
||
| 78 | 2 | public function setValue($value) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 1 | public function getValue() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 2 | View Code Duplication | public static function getPossibleValueValues() |
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $type |
||
| 116 | * @param string|null $value |
||
| 117 | * |
||
| 118 | * @throws BpostInvalidValueException |
||
| 119 | */ |
||
| 120 | 2 | public function __construct($type, $value = null) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Return the object as an array for usage in the XML |
||
| 130 | * |
||
| 131 | * @param \DomDocument $document |
||
| 132 | * @param string $prefix |
||
| 133 | * @return \DomElement |
||
| 134 | */ |
||
| 135 | 1 | public function toXML(\DOMDocument $document, $prefix = 'common') |
|
| 156 | } |
||
| 157 |
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.