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_Mapper_Currency 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_Mapper_Currency, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Payone_Api_Mapper_Currency extends Payone_Api_Mapper_Abstract |
||
| 34 | implements Payone_Api_Mapper_Currency_Interface |
||
| 35 | { |
||
| 36 | const PATH_CURRENCY_PROPERTIES_DEFAULT = 'currency.properties'; |
||
| 37 | |||
| 38 | /** Property names from properties file */ |
||
| 39 | const PROPERTY_NAME_ID = 'id'; |
||
| 40 | const PROPERTY_NAME_SIGN = 'sign'; |
||
| 41 | const PROPERTY_NAME_CODE = 'code'; |
||
| 42 | const PROPERTY_NAME_SUBDIV = 'subdiv'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $pathToProperties = ''; |
||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $mappingByCode = null; |
||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $mappingById = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param $amount |
||
| 59 | * @param $code |
||
| 60 | * @return string |
||
| 61 | * |
||
| 62 | */ |
||
| 63 | public function mapAmountToSub($amount, $code) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param $subAmount |
||
| 71 | * @param string $code |
||
| 72 | * @return string |
||
| 73 | * @throws Payone_Api_Exception_MappingNotFound |
||
| 74 | */ |
||
| 75 | public function mapAmountToMain($subAmount, $code) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $code |
||
| 83 | * @return string |
||
| 84 | * @throws Payone_Api_Exception_MappingNotFound |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function getIdByCode($code) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param $id |
||
| 99 | * @return string |
||
| 100 | * @throws Payone_Api_Exception_MappingNotFound |
||
| 101 | */ |
||
| 102 | public function getCodeById($id) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $code |
||
| 115 | * @return string |
||
| 116 | * @throws Payone_Api_Exception_MappingNotFound |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function getCurrencySymbolByCode($code) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param $code |
||
| 131 | * @return int |
||
| 132 | * @throws Payone_Api_Exception_MappingNotFound |
||
| 133 | */ |
||
| 134 | protected function initSubdivByCode($code) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | protected function initCurrenciesRaw() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | protected function initMappingByCode() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param array $mappingByCode |
||
| 228 | */ |
||
| 229 | public function setMappingByCode(array $mappingByCode) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $code |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getMappingByCode($code = '') |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param $code |
||
| 253 | * @param $innerArrayKey ('id' | 'sign' | 'subdiv') |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function hasMappingByCode($code, $innerArrayKey) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return array |
||
| 276 | * |
||
| 277 | * mappingById is derived from mappingByCode |
||
| 278 | */ |
||
| 279 | public function getMappingById() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | protected function initMappingById() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $pathToProperties |
||
| 311 | */ |
||
| 312 | public function setPathToProperties($pathToProperties) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function getPathToProperties() |
||
| 324 | } |
||
| 325 |
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.