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 |
||
20 | class RenderFileStream implements FileStream |
||
21 | { |
||
22 | const LINKS_LIMIT = 50000; |
||
23 | |||
24 | const BYTE_LIMIT = 52428800; // 50 Mb |
||
25 | |||
26 | /** |
||
27 | * @var SitemapRender |
||
28 | */ |
||
29 | private $render; |
||
30 | |||
31 | /** |
||
32 | * @var StreamState |
||
33 | */ |
||
34 | private $state; |
||
35 | |||
36 | /** |
||
37 | * @var resource|null |
||
38 | */ |
||
39 | private $handle; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $filename = ''; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $counter = 0; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $end_string = ''; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $used_bytes = 0; |
||
60 | |||
61 | /** |
||
62 | * @param SitemapRender $render |
||
63 | * @param string $filename |
||
64 | */ |
||
65 | public function __construct(SitemapRender $render, $filename) |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getFilename() |
||
79 | |||
80 | View Code Duplication | public function open() |
|
94 | |||
95 | public function close() |
||
101 | |||
102 | /** |
||
103 | * @param Url $url |
||
104 | */ |
||
105 | View Code Duplication | public function push(Url $url) |
|
125 | |||
126 | /** |
||
127 | * @return int |
||
128 | */ |
||
129 | public function count() |
||
133 | |||
134 | /** |
||
135 | * @param string $string |
||
136 | */ |
||
137 | private function write($string) |
||
142 | } |
||
143 |
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.