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 |
||
19 | class IndexDB extends Graph implements Insert, Index { |
||
20 | /** |
||
21 | * @var array<string,Node> |
||
22 | */ |
||
23 | protected $namespaces = []; |
||
24 | |||
25 | /** |
||
26 | * @var array<string,Node> |
||
27 | */ |
||
28 | protected $globals = []; |
||
29 | |||
30 | /** |
||
31 | * @var array<string,Node> |
||
32 | */ |
||
33 | protected $language_constructs = []; |
||
34 | |||
35 | /** |
||
36 | * @var array<string,Node> |
||
37 | */ |
||
38 | protected $method_references = []; |
||
39 | |||
40 | /** |
||
41 | * @var array<string,Node> |
||
42 | */ |
||
43 | protected $function_references = []; |
||
44 | |||
45 | /** |
||
46 | * @inheritdocs |
||
47 | */ |
||
48 | 61 | public function _file($path, $source) { |
|
62 | |||
63 | /** |
||
64 | * @inheritdocs |
||
65 | */ |
||
66 | 10 | public function _namespace($name) { |
|
67 | 10 | assert('is_string($name)'); |
|
68 | |||
69 | 10 | if (array_key_exists($name, $this->namespaces)) { |
|
70 | 1 | return $this->namespaces[$name]; |
|
71 | } |
||
72 | |||
73 | 10 | $namespace = $this->create_node |
|
74 | 10 | ( "namespace" |
|
75 | 10 | , [ "name" => $name |
|
76 | 10 | ] |
|
77 | 10 | ); |
|
78 | |||
79 | 10 | $this->namespaces[$name] = $namespace; |
|
80 | 10 | return $namespace; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @inheritdocs |
||
85 | */ |
||
86 | 46 | View Code Duplication | public function _class($name, $file, $start_line, $end_line, $namespace = null) { |
101 | |||
102 | /** |
||
103 | * @inheritdocs |
||
104 | */ |
||
105 | 6 | View Code Duplication | public function _interface($name, $file, $start_line, $end_line, $namespace = null) { |
120 | |||
121 | /** |
||
122 | * @inheritdocs |
||
123 | */ |
||
124 | 6 | View Code Duplication | public function _trait($name, $file, $start_line, $end_line, $namespace = null) { |
139 | |||
140 | /** |
||
141 | * @inheritdocs |
||
142 | */ |
||
143 | 39 | public function _method($name, $class, $file, $start_line, $end_line) { |
|
161 | |||
162 | /** |
||
163 | * @inheritdocs |
||
164 | */ |
||
165 | 9 | View Code Duplication | public function _function($name, $file, $start_line, $end_line, $namespace = null) { |
181 | |||
182 | /** |
||
183 | * @inheritdocs |
||
184 | */ |
||
185 | 12 | View Code Duplication | public function _global($name) { |
197 | |||
198 | /** |
||
199 | * @inheritdocs |
||
200 | */ |
||
201 | 8 | View Code Duplication | public function _language_construct($name) { |
213 | |||
214 | /** |
||
215 | * @inheritdocs |
||
216 | */ |
||
217 | 6 | View Code Duplication | public function _method_reference($name, $file, $line, $column) { |
218 | 6 | assert('is_string($name)'); |
|
219 | 6 | assert('$file->type() == "file"'); |
|
220 | 6 | assert('is_int($line)'); |
|
221 | |||
222 | 6 | $key = $name."_".$file->property("path")."_".$line."_".$column; |
|
223 | |||
224 | 6 | if (array_key_exists($key, $this->method_references)) { |
|
225 | 1 | return $this->method_references[$key]; |
|
226 | } |
||
227 | |||
228 | 6 | $method = $this->create_node("method reference", ["name" => $name]); |
|
229 | 6 | $this->add_relation |
|
230 | 6 | ( $method |
|
231 | 6 | , "referenced at" |
|
232 | 6 | , ["line" => $line, "column" => $column] |
|
233 | 6 | , $file |
|
234 | 6 | ); |
|
235 | |||
236 | 6 | $this->method_references[$key] = $method; |
|
237 | 6 | return $method; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * @inheritdocs |
||
242 | */ |
||
243 | 10 | View Code Duplication | public function _function_reference($name, $file, $line, $column) { |
244 | 10 | assert('is_string($name)'); |
|
245 | 10 | assert('$file->type() == "file"'); |
|
246 | 10 | assert('is_int($line)'); |
|
247 | |||
248 | 10 | $key = $name."_".$file->property("path")."_".$line."_".$column; |
|
249 | |||
250 | 10 | if (array_key_exists($key, $this->function_references)) { |
|
251 | 1 | return $this->function_references[$key]; |
|
252 | } |
||
253 | |||
254 | 10 | $function = $this->create_node("function reference", ["name" => $name]); |
|
255 | 10 | $this->add_relation |
|
256 | 10 | ( $function |
|
257 | 10 | , "referenced at" |
|
258 | 10 | , ["line" => $line, "column" => $column] |
|
259 | 10 | , $file |
|
260 | 10 | ); |
|
261 | |||
262 | 10 | $this->function_references[$key] = $function; |
|
263 | 10 | return $function; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * @inheritdocs |
||
268 | */ |
||
269 | 17 | public function _relation($left_entity, $relation, $right_entity, $file, $line) { |
|
286 | |||
287 | // Helper |
||
288 | |||
289 | 57 | protected function add_definition(Node $n, Node $file, $start_line, $end_line, Node $namespace = null) { |
|
305 | } |
||
306 |
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.