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 |
||
| 36 | class Toolkit extends \Payone\Core\Helper\Base |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * PAYONE payment helper |
||
| 40 | * |
||
| 41 | * @var \Payone\Core\Helper\Payment |
||
| 42 | */ |
||
| 43 | protected $paymentHelper; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor |
||
| 47 | * |
||
| 48 | * @param \Magento\Framework\App\Helper\Context $context |
||
| 49 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
||
| 50 | * @param \Payone\Core\Helper\Payment $paymentHelper |
||
| 51 | * @param \Payone\Core\Helper\Shop $shopHelper |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get security keys for all payment types for the given store code |
||
| 65 | * |
||
| 66 | * @param string $sStoreCode |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | protected function getAllPayoneSecurityKeysByStoreCode($sStoreCode) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the configured security keys for all available stores |
||
| 83 | * and payment types - since every payment-type can have its own |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getAllPayoneSecurityKeys() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check wheither the given key is configured in the shop and thus valid |
||
| 99 | * |
||
| 100 | * @param string $sKey |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function isKeyValid($sKey) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Replace substitutes in a given text with the given replacements |
||
| 116 | * |
||
| 117 | * @param string $sText |
||
| 118 | * @param string $aSubstitutionArray |
||
| 119 | * @param int|bool $iMaxLength |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function handleSubstituteReplacement($sText, $aSubstitutionArray, $iMaxLength = false) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get substituted invoice appendix text |
||
| 136 | * |
||
| 137 | * @param SalesOrder $oOrder |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function getInvoiceAppendix(SalesOrder $oOrder) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Returns narrative text for authorization request |
||
| 153 | * |
||
| 154 | * @param SalesOrder $oOrder |
||
| 155 | * @param PayoneMethod $oPayment |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function getNarrativeText(SalesOrder $oOrder, PayoneMethod $oPayment) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Format a price to the XX.YY format |
||
| 170 | * |
||
| 171 | * @param double $dPrice price of any sort |
||
| 172 | * @param int $iDecimals number of digits behind the decimal point |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function formatNumber($dPrice, $iDecimals = 2) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Checks if given string is utf8 encoded |
||
| 182 | * |
||
| 183 | * @param string $sString |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isUTF8($sString) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return data from data-object |
||
| 193 | * Needed because of different ways to read from it for different magento versions |
||
| 194 | * |
||
| 195 | * @param DataObject $oData |
||
| 196 | * @param string $sKey |
||
| 197 | * @return string|null |
||
| 198 | */ |
||
| 199 | public function getAdditionalDataEntry(DataObject $oData, $sKey) |
||
| 212 | } |
||
| 213 |
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.