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 |
||
14 | class ElggAnnotation extends \ElggExtender { |
||
15 | |||
16 | /** |
||
17 | * (non-PHPdoc) |
||
18 | * |
||
19 | * @see \ElggData::initializeAttributes() |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | protected function initializeAttributes() { |
||
28 | |||
29 | /** |
||
30 | * Construct a new annotation object |
||
31 | * |
||
32 | * Plugin developers will probably never use the constructor. |
||
33 | * See \ElggEntity for its API for adding annotations. |
||
34 | * |
||
35 | * @param \stdClass $row Database row as \stdClass object |
||
36 | */ |
||
37 | View Code Duplication | public function __construct($row = null) { |
|
57 | |||
58 | /** |
||
59 | * Save this instance |
||
60 | * |
||
61 | * @return int an object id |
||
62 | * |
||
63 | * @throws IOException |
||
64 | */ |
||
65 | View Code Duplication | public function save() { |
|
79 | |||
80 | /** |
||
81 | * Delete the annotation. |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function delete() { |
||
93 | |||
94 | /** |
||
95 | * Disable the annotation. |
||
96 | * |
||
97 | * @return bool |
||
98 | * @since 1.8 |
||
99 | */ |
||
100 | public function disable() { |
||
103 | |||
104 | /** |
||
105 | * Enable the annotation. |
||
106 | * |
||
107 | * @return bool |
||
108 | * @since 1.8 |
||
109 | */ |
||
110 | public function enable() { |
||
113 | |||
114 | /** |
||
115 | * Determines whether or not the user can edit this annotation |
||
116 | * |
||
117 | * @param int $user_guid The GUID of the user (defaults to currently logged in user) |
||
118 | * |
||
119 | * @return bool |
||
120 | * @see elgg_set_ignore_access() |
||
121 | */ |
||
122 | public function canEdit($user_guid = 0) { |
||
152 | |||
153 | // SYSTEM LOG INTERFACE |
||
154 | |||
155 | /** |
||
156 | * For a given ID, return the object associated with it. |
||
157 | * This is used by the river functionality primarily. |
||
158 | * This is useful for checking access permissions etc on objects. |
||
159 | * |
||
160 | * @param int $id An annotation ID. |
||
161 | * |
||
162 | * @return \ElggAnnotation |
||
163 | */ |
||
164 | public function getObjectFromID($id) { |
||
167 | } |
||
168 |
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.