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 |
||
3 | class FoxyStripeOptionInventoryManager extends DataExtension |
||
4 | { |
||
5 | /** |
||
6 | * @var array |
||
7 | */ |
||
8 | private static $db = [ |
||
9 | 'ControlInventory' => 'Boolean', |
||
10 | 'PurchaseLimit' => 'Int', |
||
11 | ]; |
||
12 | |||
13 | /** |
||
14 | * @param FieldList $fields |
||
15 | */ |
||
16 | View Code Duplication | public function updateCMSFields(FieldList $fields) |
|
35 | |||
36 | /** |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function getHasInventory() |
||
43 | |||
44 | /** |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function getIsOptionAvailable() |
||
54 | |||
55 | /** |
||
56 | * @return int |
||
57 | */ |
||
58 | public function getNumberAvailable() |
||
64 | |||
65 | /** |
||
66 | * @return int |
||
67 | */ |
||
68 | View Code Duplication | public function getNumberPurchased() |
|
78 | |||
79 | /** |
||
80 | * @return DataList |
||
81 | */ |
||
82 | public function getOrders() |
||
89 | |||
90 | /** |
||
91 | * @param $available |
||
92 | */ |
||
93 | public function updateOptionAvailability(&$available) |
||
99 | } |
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.