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 RenderBzip2FileStream implements FileStream |
||
21 | { |
||
22 | /** |
||
23 | * @var SitemapRender |
||
24 | */ |
||
25 | private $render; |
||
26 | |||
27 | /** |
||
28 | * @var StreamState |
||
29 | */ |
||
30 | private $state; |
||
31 | |||
32 | /** |
||
33 | * @var resource|null |
||
34 | */ |
||
35 | private $handle; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $filename = ''; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $tmp_filename = ''; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | private $counter = 0; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $end_string = ''; |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | */ |
||
60 | private $used_bytes = 0; |
||
61 | |||
62 | /** |
||
63 | * @param SitemapRender $render |
||
64 | * @param string $filename |
||
65 | */ |
||
66 | 11 | public function __construct(SitemapRender $render, $filename) |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 1 | public function getFilename() |
|
80 | |||
81 | 8 | public function open() |
|
96 | |||
97 | 11 | View Code Duplication | public function close() |
114 | |||
115 | /** |
||
116 | * @param Url $url |
||
117 | */ |
||
118 | 5 | View Code Duplication | public function push(Url $url) |
119 | { |
||
120 | 5 | if (!$this->state->isReady()) { |
|
121 | 2 | throw StreamStateException::notReady(); |
|
122 | } |
||
123 | |||
124 | 3 | if ($this->counter >= self::LINKS_LIMIT) { |
|
125 | 1 | throw LinksOverflowException::withLimit(self::LINKS_LIMIT); |
|
126 | } |
||
127 | |||
128 | 3 | $render_url = $this->render->url($url); |
|
129 | |||
130 | 3 | $expected_bytes = $this->used_bytes + strlen($render_url) + strlen($this->end_string); |
|
131 | 3 | if ($expected_bytes > self::BYTE_LIMIT) { |
|
132 | throw SizeOverflowException::withLimit(self::BYTE_LIMIT); |
||
133 | } |
||
134 | |||
135 | 3 | $this->write($render_url); |
|
136 | 3 | ++$this->counter; |
|
137 | 3 | } |
|
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | 2 | public function count() |
|
146 | |||
147 | /** |
||
148 | * @param string $string |
||
149 | */ |
||
150 | 8 | private function write($string) |
|
155 | } |
||
156 |