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 |
||
8 | class Queries { |
||
9 | /** |
||
10 | * @var ResultDB |
||
11 | */ |
||
12 | protected $result_db; |
||
13 | |||
14 | public function __construct(ResultDB $result_db) { |
||
17 | |||
18 | /** |
||
19 | * Get the id of the current run. |
||
20 | * |
||
21 | * @return int |
||
22 | */ |
||
23 | public function current_run() { |
||
37 | |||
38 | /** |
||
39 | * Get the id of the previous run. |
||
40 | * |
||
41 | * @return int |
||
42 | */ |
||
43 | View Code Duplication | public function previous_run() { |
|
59 | |||
60 | /** |
||
61 | * Get the id of previous run with another commit id as current run. |
||
62 | * |
||
63 | * @return int |
||
64 | */ |
||
65 | public function previous_run_with_different_commit() { |
||
83 | |||
84 | /** |
||
85 | * Get the id of the last run for a certain commit. |
||
86 | * |
||
87 | * @param string $commit_hash |
||
88 | * @return int|null |
||
89 | */ |
||
90 | View Code Duplication | public function last_run_for($commit_hash) { |
|
106 | |||
107 | /** |
||
108 | * Get information about a run. |
||
109 | * |
||
110 | * @param int $run |
||
111 | * @return array<string,string> with keys 'commit_hash' |
||
112 | */ |
||
113 | View Code Duplication | public function run_info($run) { |
|
127 | |||
128 | /** |
||
129 | * Get the amount of violations in a run. |
||
130 | * |
||
131 | * @param int $run |
||
132 | * @param int|null $rule |
||
133 | * @return int |
||
134 | */ |
||
135 | public function count_violations_in($run, $rule = null) { |
||
159 | |||
160 | /** |
||
161 | * Get the amount of violations that were added in a run. |
||
162 | * |
||
163 | * @param int $run_former |
||
164 | * @param int $run_latter |
||
165 | * @param int|null $rule |
||
166 | * @return int |
||
167 | */ |
||
168 | View Code Duplication | public function count_added_violations($run_former, $run_latter, $rule = null) { |
|
197 | |||
198 | /** |
||
199 | * Get the amount of violations that were resolved in a run. |
||
200 | * |
||
201 | * @param int $run_former |
||
202 | * @param int $run_latter |
||
203 | * @param int|null $rule |
||
204 | * @return int |
||
205 | */ |
||
206 | View Code Duplication | public function count_resolved_violations($run_former, $run_latter, $rule = null) { |
|
235 | |||
236 | /** |
||
237 | * Get the rules that were analyzed in a run. |
||
238 | * |
||
239 | * @param int $run |
||
240 | * @return int[] |
||
241 | */ |
||
242 | View Code Duplication | public function analyzed_rules($run) { |
|
255 | |||
256 | /** |
||
257 | * Get information about a rule. |
||
258 | * |
||
259 | * @param int $rule |
||
260 | * @return array<string,string> with keys 'rule', 'explanation' |
||
261 | */ |
||
262 | View Code Duplication | public function rule_info($rule) { |
|
276 | |||
277 | /** |
||
278 | * Get the violations of a rule. |
||
279 | * |
||
280 | * @param int $rule |
||
281 | * @param int $run |
||
282 | * @return array<string,string|int>[] with keys 'file', 'line_no', 'introduced_in' |
||
283 | */ |
||
284 | public function violations_of($rule, $run) { |
||
300 | } |
||
301 |
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.