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 |
||
17 | View Code Duplication | class Function_Deprecated extends PersistentListItem implements Invocation_Warner { |
|
|
|||
18 | /** |
||
19 | * Old declaration. |
||
20 | * |
||
21 | * @var object |
||
22 | */ |
||
23 | public $old_declaration; |
||
24 | |||
25 | /** |
||
26 | * New declaration. |
||
27 | * |
||
28 | * @var object |
||
29 | */ |
||
30 | public $new_declaration; |
||
31 | |||
32 | /** |
||
33 | * Function_Moved constructor. |
||
34 | * |
||
35 | * @param object $old_declaration Old declaration. |
||
36 | * @param object $new_declaration New declaration. |
||
37 | */ |
||
38 | public function __construct( $old_declaration, $new_declaration ) { |
||
42 | |||
43 | /** |
||
44 | * Return array of declaration items. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function to_csv_array() { |
||
56 | |||
57 | /** |
||
58 | * Returns type of issue discovered. |
||
59 | * |
||
60 | * @return string 'function_deprecated' |
||
61 | */ |
||
62 | public function type() { |
||
65 | |||
66 | /** |
||
67 | * Returns the display name of the issue. |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function display_name() { |
||
74 | |||
75 | /** |
||
76 | * Find warnings. |
||
77 | * |
||
78 | * @param object $invocation Invocation. |
||
79 | * @param object $warnings Warnings. |
||
80 | */ |
||
81 | public function find_invocation_warnings( $invocation, $warnings ) { |
||
88 | } |
||
89 |
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.