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 |
||
5 | final class Rebuild |
||
6 | { |
||
7 | private $directiveArray; |
||
8 | private $reRun = true; |
||
9 | |||
10 | /** |
||
11 | * Constructor |
||
12 | * |
||
13 | * @param string $directives |
||
14 | */ |
||
15 | public function __construct($directives) |
||
26 | |||
27 | /** |
||
28 | * Directive all |
||
29 | * |
||
30 | * @return void |
||
31 | */ |
||
32 | private function all() |
||
39 | |||
40 | /** |
||
41 | * Directive noindex |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | View Code Duplication | private function noindex() |
|
53 | |||
54 | /** |
||
55 | * Directive none |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | View Code Duplication | private function none() |
|
68 | |||
69 | /** |
||
70 | * Directive unavailable_after |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | private function unavailable_after() |
||
85 | |||
86 | /** |
||
87 | * Result |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getResult() |
||
95 | } |
||
96 |
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.