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 |
||
15 | class IndexDBFactory { |
||
16 | /** |
||
17 | * Create a new database for index at path. |
||
18 | * |
||
19 | * @param string $path |
||
20 | * @throws \RuntimeException if database already exists. |
||
21 | * @return IndexDB |
||
22 | */ |
||
23 | 2 | View Code Duplication | public function build_index_db($path) { |
|
|||
24 | 2 | if (file_exists($path)) { |
|
25 | 1 | throw new \RuntimeException("File at '$path' already exists, can't build database."); |
|
26 | } |
||
27 | 1 | $connection = DB::sqlite_connection($path); |
|
28 | 1 | $db = new IndexDB($connection); |
|
29 | 1 | $db->init_sqlite_regexp(); |
|
30 | 1 | $db->init_database_schema(); |
|
31 | 1 | return $db; |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * Check if an index database exists. |
||
36 | * |
||
37 | * @param string $path |
||
38 | * @return bool |
||
39 | */ |
||
40 | 4 | public function index_db_exists($path) { |
|
43 | |||
44 | /** |
||
45 | * Load existing index database. |
||
46 | * |
||
47 | * @param string $path |
||
48 | * @throws \RuntimeException if file does not exist |
||
49 | * @return IndexDB |
||
50 | */ |
||
51 | 2 | View Code Duplication | public function load_index_db($path) { |
60 | } |
||
61 |
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.