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 ElggMetadata 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 metadata object |
||
31 | * |
||
32 | * Plugin developers will probably never need to use this API. See \ElggEntity |
||
33 | * for its API for setting and getting metadata. |
||
34 | * |
||
35 | * @param \stdClass $row Database row as \stdClass object |
||
36 | */ |
||
37 | View Code Duplication | public function __construct($row = null) { |
|
57 | |||
58 | /** |
||
59 | * Determines whether or not the user can edit this piece of metadata |
||
60 | * |
||
61 | * @param int $user_guid The GUID of the user (defaults to currently logged in user) |
||
62 | * |
||
63 | * @return bool |
||
64 | * @see elgg_set_ignore_access() |
||
65 | */ |
||
66 | public function canEdit($user_guid = 0) { |
||
72 | |||
73 | /** |
||
74 | * Save metadata object |
||
75 | * |
||
76 | * @return int|bool the metadata object id or true if updated |
||
77 | * |
||
78 | * @throws IOException |
||
79 | */ |
||
80 | View Code Duplication | public function save() { |
|
94 | |||
95 | /** |
||
96 | * Delete the metadata |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | View Code Duplication | public function delete() { |
|
107 | |||
108 | /** |
||
109 | * Disable the metadata |
||
110 | * |
||
111 | * @return bool |
||
112 | * @since 1.8 |
||
113 | */ |
||
114 | View Code Duplication | public function disable() { |
|
121 | |||
122 | /** |
||
123 | * Enable the metadata |
||
124 | * |
||
125 | * @return bool |
||
126 | * @since 1.8 |
||
127 | */ |
||
128 | View Code Duplication | public function enable() { |
|
135 | |||
136 | // SYSTEM LOG INTERFACE //////////////////////////////////////////////////////////// |
||
137 | |||
138 | /** |
||
139 | * For a given ID, return the object associated with it. |
||
140 | * This is used by the river functionality primarily. |
||
141 | * This is useful for checking access permissions etc on objects. |
||
142 | * |
||
143 | * @param int $id Metadata ID |
||
144 | * |
||
145 | * @return \ElggMetadata |
||
146 | */ |
||
147 | public function getObjectFromID($id) { |
||
150 | } |
||
151 |
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.