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 | View Code Duplication | class DriverStatistic |
|
|
|||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $info = ''; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $size = 0; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $data = ''; |
||
36 | |||
37 | /** |
||
38 | * @var mixed |
||
39 | */ |
||
40 | protected $rawData; |
||
41 | |||
42 | /** |
||
43 | * @return string|bool Return infos or false if no information available |
||
44 | */ |
||
45 | public function getInfo() |
||
49 | |||
50 | /** |
||
51 | * @return int|bool Return size in octet or false if no information available |
||
52 | */ |
||
53 | public function getSize() |
||
57 | |||
58 | /** |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function getData() |
||
65 | |||
66 | /** |
||
67 | * @param $info |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function setInfo($info) |
||
76 | |||
77 | |||
78 | /** |
||
79 | * @param int $size |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function setSize($size) |
||
88 | |||
89 | /** |
||
90 | * @param mixed $data |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setData($data) |
||
99 | |||
100 | /** |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function getRawData() |
||
107 | |||
108 | /** |
||
109 | * @param mixed $raw |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function setRawData($raw) |
||
118 | |||
119 | /** |
||
120 | * @return array |
||
121 | */ |
||
122 | public function getPublicDesc() |
||
131 | } |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.