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 |
||
16 | class Plan extends Client |
||
17 | { |
||
18 | public static $addonTypes = [ |
||
19 | 'recurring', |
||
20 | 'one_time', |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * Returns all plans. |
||
25 | * |
||
26 | * @param array $filters associative array of filters |
||
27 | * |
||
28 | * @throws \Exception |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | public function listPlans(array $filters = [], bool $withAddons = true, string $addonType = null): array |
||
54 | |||
55 | /** |
||
56 | * Returns a Plan by its identifier. |
||
57 | * |
||
58 | * @param string $planCode |
||
59 | * |
||
60 | * @throws \Exception |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | View Code Duplication | public function getPlan(string $planCode): array |
|
82 | |||
83 | /** |
||
84 | * get reccurent addons for given plan. |
||
85 | * |
||
86 | * @param array $plans |
||
87 | * @param string|null $addonType |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getAddonsForPlan(array $plans, string $addonType = null): array |
||
115 | |||
116 | /** |
||
117 | * filter given plans with given filters. |
||
118 | * |
||
119 | * @param array $plans |
||
120 | * @param array $filters |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | public function filterPlans(array $plans, array $filters): array |
||
136 | |||
137 | /** |
||
138 | * get price by planCode |
||
139 | * |
||
140 | */ |
||
141 | public function getPriceByPlanCode(string $planCode): float |
||
147 | } |
||
148 |
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.