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 namespace BinPacking3d\Entity; |
||
10 | class Request implements EntityInterface |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var |
||
15 | */ |
||
16 | private $username; |
||
17 | /** |
||
18 | * @var |
||
19 | */ |
||
20 | private $apiKey; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $bins = array(); |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $items = array(); |
||
31 | |||
32 | /** |
||
33 | * @return array |
||
34 | * @throws \Exception |
||
35 | */ |
||
36 | public function render() |
||
59 | |||
60 | /** |
||
61 | * @return bool |
||
62 | * @throws \Exception |
||
63 | */ |
||
64 | public function validate() |
||
72 | |||
73 | /** |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function getUsername() |
||
80 | |||
81 | /** |
||
82 | * @param mixed $username |
||
83 | * @return Request |
||
84 | */ |
||
85 | public function setUsername($username) |
||
91 | |||
92 | /** |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function getApiKey() |
||
99 | |||
100 | /** |
||
101 | * @param mixed $apiKey |
||
102 | * @return Request |
||
103 | */ |
||
104 | public function setApiKey($apiKey) |
||
110 | |||
111 | /** |
||
112 | * @return \Generator |
||
113 | */ |
||
114 | private function yieldBins() |
||
120 | |||
121 | /** |
||
122 | * @return \Generator |
||
123 | */ |
||
124 | private function yieldItems() |
||
130 | |||
131 | /** |
||
132 | * @param $identifier |
||
133 | * @return mixed |
||
134 | */ |
||
135 | public function getItem($identifier) |
||
139 | |||
140 | /** |
||
141 | * @param $identifier |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function getBin($identifier) |
||
148 | |||
149 | /** |
||
150 | * @param Item $item |
||
151 | * @return $this |
||
152 | */ |
||
153 | View Code Duplication | public function addItem(Item $item) |
|
170 | |||
171 | /** |
||
172 | * @param Bin $bin |
||
173 | * @return $this |
||
174 | */ |
||
175 | View Code Duplication | public function addBin(Bin $bin) |
|
192 | |||
193 | /** |
||
194 | * @return array |
||
195 | */ |
||
196 | public function getBins() |
||
200 | |||
201 | /** |
||
202 | * @param array $bins |
||
203 | * @return Request |
||
204 | */ |
||
205 | public function setBins($bins) |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | public function getItems() |
||
219 | |||
220 | /** |
||
221 | * @param array $items |
||
222 | * @return Request |
||
223 | */ |
||
224 | public function setItems($items) |
||
230 | |||
231 | } |
||
232 |
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.