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 |
||
22 | class ElggObject extends \ElggEntity { |
||
23 | |||
24 | /** |
||
25 | * Initialize the attributes array to include the type, |
||
26 | * title, and description. |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | 15 | View Code Duplication | protected function initializeAttributes() { |
37 | |||
38 | /** |
||
39 | * Get default values for attributes stored in a separate table |
||
40 | * |
||
41 | * @return array |
||
42 | * @access private |
||
43 | * |
||
44 | * @see \Elgg\Database\EntityTable::getEntities |
||
45 | */ |
||
46 | 15 | final public static function getExternalAttributes() { |
|
52 | |||
53 | /** |
||
54 | * Create a new \ElggObject. |
||
55 | * |
||
56 | * Plugin developers should only use the constructor to create a new entity. |
||
57 | * To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
||
58 | * |
||
59 | * If no arguments are passed, it creates a new entity. |
||
60 | * If a database result is passed as a \stdClass instance, it instantiates |
||
61 | * that entity. |
||
62 | * |
||
63 | * @param \stdClass $row Database row result. Default is null to create a new object. |
||
64 | * |
||
65 | * @throws IOException If cannot load remaining data from db |
||
66 | * @throws InvalidParameterException If not passed a db row result |
||
67 | */ |
||
68 | 15 | View Code Duplication | public function __construct($row = null) { |
99 | |||
100 | /** |
||
101 | * Loads the full \ElggObject when given a guid. |
||
102 | * |
||
103 | * @param mixed $guid GUID of an \ElggObject or the \stdClass object from entities table |
||
104 | * |
||
105 | * @return bool |
||
106 | * @throws InvalidClassException |
||
107 | */ |
||
108 | 15 | View Code Duplication | protected function load($guid) { |
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | View Code Duplication | protected function create() { |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | View Code Duplication | protected function update() { |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getDisplayName() { |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function setDisplayName($displayName) { |
||
187 | |||
188 | /** |
||
189 | * Return sites that this object is a member of |
||
190 | * |
||
191 | * Site membership is determined by relationships and not site_guid. |
||
192 | * |
||
193 | * @todo Moved to \ElggEntity so remove this in 2.0 |
||
194 | * |
||
195 | * @param array $options Options array. Used to be $subtype |
||
196 | * @param int $limit The number of results to return (deprecated) |
||
197 | * @param int $offset Any indexing offset (deprecated) |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | View Code Duplication | public function getSites($options = "", $limit = 10, $offset = 0) { |
|
209 | |||
210 | /** |
||
211 | * Add this object to a site |
||
212 | * |
||
213 | * @param \ElggSite $site The site to add this object to. This used to be the |
||
214 | * the site guid (still supported by deprecated) |
||
215 | * @return bool |
||
216 | */ |
||
217 | View Code Duplication | public function addToSite($site) { |
|
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | protected function prepareObject($object) { |
||
236 | |||
237 | /* |
||
238 | * EXPORTABLE INTERFACE |
||
239 | */ |
||
240 | |||
241 | /** |
||
242 | * Return an array of fields which can be exported. |
||
243 | * |
||
244 | * @return array |
||
245 | * @deprecated 1.9 Use toObject() |
||
246 | */ |
||
247 | public function getExportableValues() { |
||
253 | |||
254 | /** |
||
255 | * Can a user comment on this object? |
||
256 | * |
||
257 | * @see \ElggEntity::canComment() |
||
258 | * |
||
259 | * @param int $user_guid User guid (default is logged in user) |
||
260 | * @return bool |
||
261 | * @since 1.8.0 |
||
262 | */ |
||
263 | public function canComment($user_guid = 0) { |
||
288 | } |
||
289 |
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.