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 |
||
17 | class BillHwPurchaseCombo extends Combo |
||
18 | { |
||
19 | /** {@inheritdoc} */ |
||
20 | public $type = 'bill/descr'; |
||
21 | |||
22 | /** {@inheritdoc} */ |
||
23 | public $name = 'descr'; |
||
24 | |||
25 | /** {@inheritdoc} */ |
||
26 | public $url = '/finance/bill/index'; |
||
27 | |||
28 | /** {@inheritdoc} */ |
||
29 | public $_return = ['id', 'client', 'sum', 'currency', 'descr', 'label', 'time']; |
||
30 | |||
31 | /** {@inheritdoc} */ |
||
32 | public $_rename = ['text' => 'label']; |
||
33 | |||
34 | /** {@inheritdoc} */ |
||
35 | public $_primaryFilter = 'descr'; |
||
36 | |||
37 | /** {@inheritdoc} */ |
||
38 | View Code Duplication | public function getPluginOptions($options = []) |
|
|
|||
39 | { |
||
40 | return parent::getPluginOptions([ |
||
41 | 'select2Options' => [ |
||
42 | 'templateResult' => new JsExpression("function (data) { |
||
43 | if (data.loading) { |
||
44 | return data.text; |
||
45 | } |
||
46 | var client = '<b>' + data.client + ': ' |
||
47 | color = data.sum < 0 ? 'text-danger' : 'text-success'; |
||
48 | sum = ' <span class=\"' + color +'\">' + data.sum + '</span> ' |
||
49 | currency = ' ' + data.currency.toUpperCase() + '</b><br>' |
||
50 | descr = (data.descr ? data.descr : data.label); |
||
51 | |||
52 | return client + sum + currency + (descr ? descr : '<span class=\"text-muted\">--</span>'); |
||
53 | }"), |
||
54 | 'escapeMarkup' => new JsExpression('function (markup) { |
||
55 | return markup; // Allows HTML |
||
56 | }'), |
||
57 | ], |
||
58 | ]); |
||
59 | } |
||
60 | |||
61 | /** {@inheritdoc} */ |
||
62 | public function getFilter() |
||
69 | } |
||
70 |
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.