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 |
||
| 34 | class Country extends \Payone\Core\Helper\Base |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * List of all countries where the state parameter has to be submitted |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | static protected $aStateNeeded = [ |
||
| 42 | 'US', |
||
| 43 | 'CA', |
||
| 44 | 'CN', |
||
| 45 | 'JP', |
||
| 46 | 'MX', |
||
| 47 | 'BR', |
||
| 48 | 'AR', |
||
| 49 | 'ID', |
||
| 50 | 'TH', |
||
| 51 | 'IN', |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Country object |
||
| 56 | * |
||
| 57 | * @var \Magento\Directory\Model\Country |
||
| 58 | */ |
||
| 59 | protected $country; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param \Magento\Framework\App\Helper\Context $context |
||
| 65 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
||
| 66 | * @param \Payone\Core\Helper\Shop $shopHelper |
||
| 67 | * @param \Magento\Directory\Model\Country $country |
||
| 68 | */ |
||
| 69 | public function __construct( |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get country name by its ISO2 abbreviation |
||
| 81 | * |
||
| 82 | * @param string $sCountryCode |
||
| 83 | * @return string|bool |
||
| 84 | */ |
||
| 85 | public function getCountryNameByIso2($sCountryCode) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get all activated debit SEPA countries |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function getDebitSepaCountries() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Return if state parameter has to be added for the given country |
||
| 121 | * |
||
| 122 | * @param string $sIsoToCountry |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public static function isStateNeeded($sIsoToCountry) |
||
| 132 | } |
||
| 133 |
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.