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 IndexDB 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 IndexDB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class IndexDB extends DB implements Insert { |
||
20 | protected $nodes_per_insert = 200; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $tables = |
||
26 | [ "files" => ["_file", |
||
27 | ["id", "path", "source"]] |
||
28 | , "namespaces" => ["_namespace", |
||
29 | ["id", "name"]] |
||
30 | , "classes" => ["_class", |
||
31 | ["id", "name", "file_id", "start_line", "end_line", "namespace_id"]] |
||
32 | , "interfaces" => ["_interface", |
||
33 | ["id", "name", "file_id", "start_line", "end_line", "namespace_id"]] |
||
34 | , "traits" => ["_trait", |
||
35 | ["id", "name", "file_id", "start_line", "end_line", "namespace_id"]] |
||
36 | , "methods" => ["_method", |
||
37 | ["id", "name", "class_id", "file_id", "start_line", "end_line"]] |
||
38 | , "functions" => ["_function", |
||
39 | ["id", "name", "file_id", "start_line", "end_line", "namespace_id"]] |
||
40 | , "globals" => ["_global", |
||
41 | ["id", "name"]] |
||
42 | , "language_constructs" => ["_language_construct", |
||
43 | ["id", "name"]] |
||
44 | , "method_references" => ["_method_reference", |
||
45 | ["id", "name", "file_id", "line", "column"]] |
||
46 | , "function_references" => ["_function_reference", |
||
47 | ["id", "name", "file_id", "line", "column"]] |
||
48 | , "relations" => ["_relation", |
||
49 | ["left_id", "relation", "right_id", "file_id", "line"]] |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * Lists of fields that contain an id. |
||
54 | * |
||
55 | * @var array<string,int> |
||
56 | */ |
||
57 | protected $id_fields = |
||
58 | [ "file_id" => 0 |
||
59 | , "namespace_id" => 0 |
||
60 | , "class_id" => 0 |
||
61 | , "left_id" => 0 |
||
62 | , "right_id" => 0 |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * List of all fields that contain an integer. |
||
67 | * |
||
68 | * @var array<string,0> |
||
69 | */ |
||
70 | protected $int_fields = |
||
71 | [ "start_line" => 0 |
||
72 | , "end_line" => 0 |
||
73 | , "line" => 0 |
||
74 | , "column" => 0 |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * @var array[] |
||
79 | */ |
||
80 | protected $caches = []; |
||
81 | |||
82 | /** |
||
83 | * @var integer |
||
84 | */ |
||
85 | protected $id_counter = 0; |
||
86 | |||
87 | 2 | public function __construct($connection) { |
|
93 | |||
94 | /** |
||
95 | * @inheritdocs |
||
96 | */ |
||
97 | 1 | public function _file($path, $source) { |
|
101 | |||
102 | /** |
||
103 | * @inheritdocs |
||
104 | */ |
||
105 | 1 | public function _namespace($name) { |
|
109 | |||
110 | /** |
||
111 | * @inheritdocs |
||
112 | */ |
||
113 | 1 | public function _class($name, $file, $start_line, $end_line, $namespace = null) { |
|
117 | |||
118 | /** |
||
119 | * @inheritdocs |
||
120 | */ |
||
121 | public function _interface($name, $file, $start_line, $end_line, $namespace = null) { |
||
125 | |||
126 | /** |
||
127 | * @inheritdocs |
||
128 | */ |
||
129 | public function _trait($name, $file, $start_line, $end_line, $namespace = null) { |
||
133 | |||
134 | /** |
||
135 | * @inheritdocs |
||
136 | */ |
||
137 | 1 | public function _method($name, $class, $file, $start_line, $end_line) { |
|
141 | |||
142 | /** |
||
143 | * @inheritdocs |
||
144 | */ |
||
145 | 1 | public function _function($name, $file, $start_line, $end_line, $namespace = null) { |
|
149 | |||
150 | /** |
||
151 | * @inheritdocs |
||
152 | */ |
||
153 | 1 | public function _global($name) { |
|
157 | |||
158 | /** |
||
159 | * @inheritdocs |
||
160 | */ |
||
161 | 1 | public function _language_construct($name) { |
|
165 | |||
166 | /** |
||
167 | * @inheritdocs |
||
168 | */ |
||
169 | 1 | public function _method_reference($name, $file, $line, $column) { |
|
173 | |||
174 | /** |
||
175 | * @inheritdocs |
||
176 | */ |
||
177 | 1 | public function _function_reference($name, $file, $line, $column) { |
|
181 | |||
182 | /** |
||
183 | * @inheritdocs |
||
184 | */ |
||
185 | 1 | public function _relation($left_entity, $relation, $right_entity, $file, $line) { |
|
189 | |||
190 | 1 | protected function insert_cache($table) { |
|
206 | |||
207 | 1 | protected function append_and_maybe_flush($table, $values) { |
|
228 | |||
229 | 1 | protected function esc_maybe_null($val) { |
|
236 | |||
237 | /** |
||
238 | * Write everything that currently is in cache to the database. |
||
239 | * |
||
240 | * @return null |
||
241 | */ |
||
242 | 1 | public function write_cached_inserts() { |
|
247 | |||
248 | /** |
||
249 | * Read the index from the database. |
||
250 | * |
||
251 | * @return Graph\IndexDB $index |
||
252 | */ |
||
253 | 1 | public function to_graph_index() { |
|
265 | |||
266 | protected function build_graph_index_db() { |
||
269 | |||
270 | // INIT DATABASE |
||
271 | |||
272 | 1 | public function init_table($name, Schema\Schema $schema, Schema\Table $file_table = null, Schema\Table $namespace_table = null) { |
|
341 | |||
342 | 1 | public function init_database_schema() { |
|
362 | } |
||
363 |
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.