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:
Complex classes like DB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class DB |
||
7 | { |
||
8 | //Database host |
||
9 | public $host = "localhost"; |
||
10 | //Database username |
||
11 | public $username = "root"; |
||
12 | //Database password |
||
13 | public $password = ""; |
||
14 | //Database name |
||
15 | public $db = 'myblog'; |
||
16 | // Hold the class instance. |
||
17 | private static $instance; |
||
18 | |||
19 | /** |
||
20 | * DB constructor. |
||
21 | */ |
||
22 | protected function __construct() |
||
33 | |||
34 | /** |
||
35 | * Create new Instance of DB Class |
||
36 | * @return PDO |
||
37 | */ |
||
38 | public static function getInstance() |
||
46 | |||
47 | /** |
||
48 | * @param $tbl_name |
||
49 | * @param $params |
||
50 | * @return \PDOStatement |
||
51 | */ |
||
52 | |||
53 | private static function select($tbl_name, $params) |
||
77 | |||
78 | /** |
||
79 | * @param $tbl_name |
||
80 | * @param $params |
||
81 | * @return mixed |
||
82 | */ |
||
83 | View Code Duplication | public static function fetch($tbl_name, $params) |
|
94 | |||
95 | /** |
||
96 | * @param $tbl_name |
||
97 | * @param $params |
||
98 | * @return mixed |
||
99 | */ |
||
100 | View Code Duplication | public static function fetchAll($tbl_name, $params) |
|
112 | |||
113 | /** |
||
114 | * @param $tbl_name |
||
115 | * @param $params |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public static function getAll($tbl_name, $params) |
||
124 | |||
125 | /** |
||
126 | * @param $tbl_name |
||
127 | * @param $id |
||
128 | * @return mixed |
||
129 | */ |
||
130 | public static function getByID($tbl_name, $id) |
||
143 | |||
144 | /** |
||
145 | * @param $tbl_name |
||
146 | * @param $params |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public static function first($tbl_name, $params) |
||
155 | |||
156 | /** |
||
157 | * @param array $array |
||
158 | * @return string |
||
159 | */ |
||
160 | private static function get_bind($array = array()) |
||
170 | |||
171 | /** |
||
172 | * @param array $array |
||
173 | * @return string |
||
174 | */ |
||
175 | View Code Duplication | private static function get_bind_exe($array = array()) |
|
185 | |||
186 | /** |
||
187 | * @param array $array |
||
188 | * @return string |
||
189 | */ |
||
190 | private static function get_values($array = array()) |
||
195 | |||
196 | /** |
||
197 | * @param array $array |
||
198 | * @return string |
||
199 | */ |
||
200 | View Code Duplication | private static function get_keys($array = array()) |
|
210 | |||
211 | |||
212 | /** |
||
213 | * @param $tbl_name |
||
214 | * @param array $params |
||
215 | * @return boolean|null |
||
216 | */ |
||
217 | public static function Create($tbl_name, $params = array()) |
||
236 | |||
237 | /** |
||
238 | * @param $tbl_name |
||
239 | * @param $condition |
||
240 | */ |
||
241 | public static function Delete($tbl_name, $condition) |
||
256 | |||
257 | /** |
||
258 | * @param $array |
||
259 | * @return string |
||
260 | */ |
||
261 | public static function update_array($array) |
||
271 | |||
272 | /** |
||
273 | * @param $tbl_name |
||
274 | * @param $array |
||
275 | * @param $where |
||
276 | * @return boolean|null |
||
277 | */ |
||
278 | public static function Update($tbl_name, $array, $where) |
||
302 | |||
303 | /** |
||
304 | * @param \PDOStatement $stmt |
||
305 | * @param $bind |
||
306 | */ |
||
307 | private static function bindParam($stmt, $bind) |
||
313 | |||
314 | /** |
||
315 | * @param $statment |
||
316 | * @return bool|mixed |
||
317 | */ |
||
318 | public static function Query($statment) |
||
336 | |||
337 | protected function __clone() |
||
340 | |||
341 | protected function __wakeup() |
||
344 | } |
||
345 |
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.