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 |
||
12 | class Tester |
||
13 | { |
||
14 | /** |
||
15 | * The Session instance. |
||
16 | * |
||
17 | * @var SessionInterface |
||
18 | */ |
||
19 | protected $session; |
||
20 | |||
21 | /** |
||
22 | * Constructor. |
||
23 | * |
||
24 | * @param SessionInterface $session |
||
25 | */ |
||
26 | public function __construct(SessionInterface $session) |
||
30 | |||
31 | /** |
||
32 | * Track clicked links and form submissions. |
||
33 | * |
||
34 | * @param Request $request |
||
35 | * @return void |
||
36 | */ |
||
37 | public function track(Request $request) |
||
73 | |||
74 | /** |
||
75 | * Get or compare the current experiment for this session. |
||
76 | * |
||
77 | * @param string $target |
||
78 | * @return bool|string |
||
79 | */ |
||
80 | public function experiment($target = null) |
||
97 | |||
98 | /** |
||
99 | * Increment the pageviews for the current experiment. |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | View Code Duplication | public function pageview() |
|
119 | |||
120 | /** |
||
121 | * Increment the engagement for the current experiment. |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | View Code Duplication | public function interact() |
|
139 | |||
140 | /** |
||
141 | * Mark a goal as completed for the current experiment. |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function complete($name) |
||
158 | |||
159 | /** |
||
160 | * Set the current experiment for this session manually. |
||
161 | * |
||
162 | * @param string $experiment |
||
163 | */ |
||
164 | public function setExperiment($experiment) |
||
173 | |||
174 | /** |
||
175 | * Get all experiments. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getExperiments() |
||
183 | |||
184 | /** |
||
185 | * Get all goals. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getGoals() |
||
193 | |||
194 | /** |
||
195 | * Get the session instance. |
||
196 | * |
||
197 | * @return SessionInterface |
||
198 | */ |
||
199 | public function getSession() |
||
203 | |||
204 | /** |
||
205 | * Set the session instance. |
||
206 | * |
||
207 | * @param $session SessionInterface |
||
208 | */ |
||
209 | public function setSession(SessionInterface $session) |
||
213 | |||
214 | /** |
||
215 | * If an experiment has initialized get his string. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function currentExperiment() |
||
232 | |||
233 | /** |
||
234 | * Prepare an experiment for this session. |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function nextExperiment($experiment = null) |
||
257 | |||
258 | /** |
||
259 | * Add experiments to the database. |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | protected function checkExperiments() |
||
273 | |||
274 | /** |
||
275 | * Check if there are active experiments. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public function hasExperiments() |
||
285 | } |
||
286 |
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.