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 RenderIndexFileStream implements FileStream |
||
20 | { |
||
21 | /** |
||
22 | * @var SitemapIndexRender |
||
23 | */ |
||
24 | private $render; |
||
25 | |||
26 | /** |
||
27 | * @var FileStream |
||
28 | */ |
||
29 | private $substream; |
||
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 $host = ''; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $filename = ''; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $tmp_filename = ''; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $index = 0; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $counter = 0; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $empty_index = true; |
||
70 | |||
71 | /** |
||
72 | * @param SitemapIndexRender $render |
||
73 | * @param FileStream $substream |
||
74 | * @param string $host |
||
75 | * @param string $filename |
||
76 | */ |
||
77 | 12 | public function __construct(SitemapIndexRender $render, FileStream $substream, $host, $filename) |
|
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | public function getFilename() |
|
93 | |||
94 | 9 | View Code Duplication | public function open() |
116 | |||
117 | 12 | public function close() |
|
145 | |||
146 | /** |
||
147 | * @param Url $url |
||
148 | */ |
||
149 | 7 | public function push(Url $url) |
|
167 | |||
168 | 5 | private function addSubStreamFileToIndex() |
|
187 | |||
188 | /** |
||
189 | * @param string $path |
||
190 | * @param int $index |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | 9 | private function getIndexPartFilename($path, $index) |
|
204 | |||
205 | /** |
||
206 | * @return int |
||
207 | */ |
||
208 | 4 | public function count() |
|
212 | |||
213 | /** |
||
214 | * Move parts of the sitemap from the temporary directory to the target. |
||
215 | */ |
||
216 | 8 | private function moveParts() |
|
228 | |||
229 | /** |
||
230 | * Remove old parts of the sitemap from the target directory. |
||
231 | */ |
||
232 | 8 | private function removeOldParts() |
|
245 | } |
||
246 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.