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 |
||
13 | class Swedish implements I18nInterface |
||
14 | { |
||
15 | public static $translations = array( |
||
16 | 'INVOICE' => 'FAKTURA', |
||
17 | 'Page ' => 'Sida ', |
||
18 | ' of ' => ' av ', |
||
19 | 'Invoice date' => 'Fakturadatum', |
||
20 | 'Invoice number' => 'Fakturanummer', |
||
21 | 'Reference' => 'OCR-nummer', |
||
22 | 'Payment term' => 'Betalningsvillkor', |
||
23 | ' days' => ' dagar', |
||
24 | 'Expiry date' => 'Förfallodag', |
||
25 | 'VAT rate' => 'Momssats', |
||
26 | 'Amount' => 'Antal', |
||
27 | 'Unit cost' => 'Á pris', |
||
28 | 'Total' => 'Summa', |
||
29 | 'Basis' => 'Underlag', |
||
30 | 'Total ex. VAT' => 'Summa ex. moms', |
||
31 | 'Total VAT' => 'Total moms', |
||
32 | 'Deduction' => 'Erlagd förskottsbetalning', |
||
33 | 'Tax registered' => 'Godkänd för F-skatt.', |
||
34 | 'To pay' => 'Att betala', |
||
35 | 'Address' => 'Svarsadress', |
||
36 | 'Identification' => 'Organisationsnr', |
||
37 | 'VAT number' => 'Momsreg.nr.', |
||
38 | 'Phone' => 'Telefon', |
||
39 | 'E-mail' => 'E-post', |
||
40 | 'Continues on next page..' => 'Sammanställningen fortsätter på nästa sida..' |
||
41 | ); |
||
42 | |||
43 | private $currencyFormatter; |
||
44 | |||
45 | View Code Duplication | public function __construct(\NumberFormatter $currencyFormatter = null) |
|
56 | |||
57 | public function translate($string) |
||
65 | |||
66 | public function formateDate(\DateTimeInterface $datetime) |
||
70 | |||
71 | public function formatCurrency(Amount $amount) |
||
75 | |||
76 | public function formatCurrencySymbol(Amount $amount, $currency) |
||
80 | |||
81 | public function formatPercentage($amount) |
||
85 | |||
86 | public function formatUnits($units) |
||
90 | } |
||
91 |
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.