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 |
||
8 | final class Manager |
||
9 | { |
||
10 | /** |
||
11 | * @var Connector |
||
12 | */ |
||
13 | private $connector; |
||
14 | |||
15 | /** |
||
16 | * @var Builder |
||
17 | */ |
||
18 | private $builder; |
||
19 | |||
20 | /** |
||
21 | * @param Connector $connector |
||
22 | * @param Builder $builder |
||
23 | */ |
||
24 | public function __construct(Connector $connector, Builder $builder) |
||
29 | |||
30 | /** |
||
31 | * @param string $method |
||
32 | * @param array $parameters |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function __call($method, array $parameters = []) |
||
45 | |||
46 | /** |
||
47 | * @return PromiseInterface |
||
48 | */ |
||
49 | public function first() |
||
56 | |||
57 | /** |
||
58 | * @return PromiseInterface |
||
59 | */ |
||
60 | public function get() |
||
68 | |||
69 | /** |
||
70 | * @param array $build |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | private function interpolate(array $build) |
||
82 | |||
83 | /** |
||
84 | * @param array $data |
||
85 | * |
||
86 | * @return PromiseInterface |
||
87 | */ |
||
88 | View Code Duplication | public function insert(array $data) |
|
96 | |||
97 | /** |
||
98 | * @param array $data |
||
99 | * |
||
100 | * @return PromiseInterface |
||
101 | */ |
||
102 | View Code Duplication | public function update(array $data) |
|
110 | |||
111 | /** |
||
112 | * @return PromiseInterface |
||
113 | */ |
||
114 | public function delete() |
||
122 | } |
||
123 |
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.