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 |
||
17 | class LanguageTextFrameReader extends StreamContainer |
||
18 | { |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $encoding; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $language; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $text; |
||
33 | |||
34 | /** |
||
35 | * Read encoding. |
||
36 | * |
||
37 | * @return int |
||
38 | */ |
||
39 | protected function readEncoding() |
||
45 | |||
46 | /** |
||
47 | * Get encoding. |
||
48 | * |
||
49 | * @return int |
||
50 | */ |
||
51 | public function getEncoding() |
||
59 | |||
60 | /** |
||
61 | * Read language. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function readLanguage() |
||
76 | |||
77 | /** |
||
78 | * Get language. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getLanguage() |
||
90 | |||
91 | /** |
||
92 | * Read text. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | View Code Duplication | protected function readText() |
|
108 | |||
109 | /** |
||
110 | * Get text. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getText() |
||
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.