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 |
||
19 | class OutputStream implements Stream |
||
20 | { |
||
21 | const LINKS_LIMIT = 50000; |
||
22 | |||
23 | const BYTE_LIMIT = 52428800; // 50 Mb |
||
24 | |||
25 | /** |
||
26 | * @var SitemapRender |
||
27 | */ |
||
28 | private $render; |
||
29 | |||
30 | /** |
||
31 | * @var StreamState |
||
32 | */ |
||
33 | private $state; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $counter = 0; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $used_bytes = 0; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $end_string = ''; |
||
49 | |||
50 | /** |
||
51 | * @param SitemapRender $render |
||
52 | */ |
||
53 | public function __construct(SitemapRender $render) |
||
58 | |||
59 | public function open() |
||
66 | |||
67 | public function close() |
||
72 | |||
73 | /** |
||
74 | * @param Url $url |
||
75 | */ |
||
76 | View Code Duplication | public function push(Url $url) |
|
98 | |||
99 | /** |
||
100 | * @return int |
||
101 | */ |
||
102 | public function count() |
||
106 | |||
107 | /** |
||
108 | * @param string $string |
||
109 | */ |
||
110 | private function send($string) |
||
116 | } |
||
117 |
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.