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 RenderGzipFileStream 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 $compression_level = 9; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $counter = 0; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $end_string = ''; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | // private $used_bytes = 0; |
||
65 | |||
66 | /** |
||
67 | * @param SitemapRender $render |
||
68 | * @param string $filename |
||
69 | * @param int $compression_level |
||
70 | */ |
||
71 | public function __construct(SitemapRender $render, $filename, $compression_level = 9) |
||
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getFilename() |
||
90 | |||
91 | View Code Duplication | public function open() |
|
106 | |||
107 | public function close() |
||
113 | |||
114 | /** |
||
115 | * @param Url $url |
||
116 | */ |
||
117 | View Code Duplication | public function push(Url $url) |
|
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | public function count() |
||
146 | |||
147 | /** |
||
148 | * @param string $string |
||
149 | */ |
||
150 | private function write($string) |
||
155 | } |
||
156 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.