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:
Complex classes like ElggSite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ElggSite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ElggSite extends \ElggEntity { |
||
29 | |||
30 | /** |
||
31 | * Initialize the attributes array. |
||
32 | * This is vital to distinguish between metadata and base attributes. |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | 1 | View Code Duplication | protected function initializeAttributes() { |
43 | |||
44 | /** |
||
45 | * Get default values for attributes stored in a separate table |
||
46 | * |
||
47 | * @return array |
||
48 | * @access private |
||
49 | * |
||
50 | * @see \Elgg\Database\EntityTable::getEntities |
||
51 | */ |
||
52 | 1 | final public static function getExternalAttributes() { |
|
59 | |||
60 | /** |
||
61 | * Create a new \ElggSite. |
||
62 | * |
||
63 | * Plugin developers should only use the constructor to create a new entity. |
||
64 | * To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
||
65 | * |
||
66 | * @param \stdClass $row Database row result. Default is null to create a new site. |
||
67 | * |
||
68 | * @throws IOException If cannot load remaining data from db |
||
69 | * @throws InvalidParameterException If not passed a db result |
||
70 | */ |
||
71 | 1 | View Code Duplication | public function __construct($row = null) { |
109 | |||
110 | /** |
||
111 | * Loads the full \ElggSite when given a guid. |
||
112 | * |
||
113 | * @param mixed $guid GUID of \ElggSite entity or database row object |
||
114 | * |
||
115 | * @return bool |
||
116 | * @throws InvalidClassException |
||
117 | */ |
||
118 | View Code Duplication | protected function load($guid) { |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | protected function create() { |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | View Code Duplication | protected function update() { |
|
187 | |||
188 | /** |
||
189 | * Delete the site. |
||
190 | * |
||
191 | * @note You cannot delete the current site. |
||
192 | * |
||
193 | * @return bool |
||
194 | * @throws SecurityException |
||
195 | */ |
||
196 | public function delete() { |
||
204 | |||
205 | /** |
||
206 | * Disable the site |
||
207 | * |
||
208 | * @note You cannot disable the current site. |
||
209 | * |
||
210 | * @param string $reason Optional reason for disabling |
||
211 | * @param bool $recursive Recursively disable all contained entities? |
||
212 | * |
||
213 | * @return bool |
||
214 | * @throws SecurityException |
||
215 | */ |
||
216 | public function disable($reason = "", $recursive = true) { |
||
225 | |||
226 | /** |
||
227 | * Returns the URL for this site |
||
228 | * |
||
229 | * @return string The URL |
||
230 | */ |
||
231 | public function getURL() { |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function getDisplayName() { |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function setDisplayName($displayName) { |
||
248 | |||
249 | /** |
||
250 | * Gets an array of \ElggUser entities who are members of the site. |
||
251 | * |
||
252 | * @param array $options An associative array for key => value parameters |
||
253 | * accepted by elgg_get_entities(). Common parameters |
||
254 | * include 'limit', and 'offset'. |
||
255 | * Note: this was $limit before version 1.8 |
||
256 | * @param int $offset Offset @deprecated parameter |
||
257 | * |
||
258 | * @return array of \ElggUsers |
||
259 | * @deprecated 1.9 Use \ElggSite::getEntities() |
||
260 | */ |
||
261 | public function getMembers($options = array(), $offset = 0) { |
||
283 | |||
284 | /** |
||
285 | * List the members of this site |
||
286 | * |
||
287 | * @param array $options An associative array for key => value parameters |
||
288 | * accepted by elgg_list_entities(). Common parameters |
||
289 | * include 'full_view', 'limit', and 'offset'. |
||
290 | * |
||
291 | * @return string |
||
292 | * @since 1.8.0 |
||
293 | * @deprecated 1.9 Use elgg_list_entities_from_relationship() |
||
294 | */ |
||
295 | public function listMembers($options = array()) { |
||
309 | |||
310 | /** |
||
311 | * Adds an entity to the site. |
||
312 | * |
||
313 | * This adds a 'member_of_site' relationship between between the entity and |
||
314 | * the site. It does not change the site_guid of the entity. |
||
315 | * |
||
316 | * @param \ElggEntity $entity User, group, or object entity |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | public function addEntity(\ElggEntity $entity) { |
||
326 | |||
327 | /** |
||
328 | * Removes an entity from this site |
||
329 | * |
||
330 | * @param \ElggEntity $entity User, group, or object entity |
||
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function removeEntity($entity) { |
||
340 | |||
341 | /** |
||
342 | * Get an array of entities that belong to the site. |
||
343 | * |
||
344 | * This only returns entities that have been explicitly added to the |
||
345 | * site through addEntity(). |
||
346 | * |
||
347 | * @param array $options Options array for elgg_get_entities_from_relationship() |
||
348 | * Parameters set automatically by this method: |
||
349 | * 'relationship', 'relationship_guid', 'inverse_relationship' |
||
350 | * @return array |
||
351 | */ |
||
352 | View Code Duplication | public function getEntities(array $options = array()) { |
|
362 | |||
363 | /** |
||
364 | * Adds a user to the site. |
||
365 | * |
||
366 | * @param int $user_guid GUID |
||
367 | * |
||
368 | * @return bool |
||
369 | * @deprecated 1.9 Use \ElggSite::addEntity() |
||
370 | */ |
||
371 | public function addUser($user_guid) { |
||
375 | |||
376 | /** |
||
377 | * Removes a user from the site. |
||
378 | * |
||
379 | * @param int $user_guid GUID |
||
380 | * |
||
381 | * @return bool |
||
382 | * @deprecated 1.9 Use \ElggSite::removeEntity() |
||
383 | */ |
||
384 | public function removeUser($user_guid) { |
||
388 | |||
389 | /** |
||
390 | * Returns an array of \ElggObject entities that belong to the site. |
||
391 | * |
||
392 | * @warning This only returns objects that have been explicitly added to the |
||
393 | * site through addObject() |
||
394 | * |
||
395 | * @param string $subtype Entity subtype |
||
396 | * @param int $limit Limit |
||
397 | * @param int $offset Offset |
||
398 | * |
||
399 | * @return array |
||
400 | * @deprecated 1.9 Use \ElggSite:getEntities() |
||
401 | */ |
||
402 | public function getObjects($subtype = "", $limit = 10, $offset = 0) { |
||
406 | |||
407 | /** |
||
408 | * Adds an object to the site. |
||
409 | * |
||
410 | * @param int $object_guid GUID |
||
411 | * |
||
412 | * @return bool |
||
413 | * @deprecated 1.9 Use \ElggSite::addEntity() |
||
414 | */ |
||
415 | public function addObject($object_guid) { |
||
419 | |||
420 | /** |
||
421 | * Remvoes an object from the site. |
||
422 | * |
||
423 | * @param int $object_guid GUID |
||
424 | * |
||
425 | * @return bool |
||
426 | * @deprecated 1.9 Use \ElggSite::removeEntity() |
||
427 | */ |
||
428 | public function removeObject($object_guid) { |
||
432 | |||
433 | /** |
||
434 | * Get the collections associated with a site. |
||
435 | * |
||
436 | * @param string $subtype Subtype |
||
437 | * @param int $limit Limit |
||
438 | * @param int $offset Offset |
||
439 | * |
||
440 | * @return mixed |
||
441 | * @deprecated 1.8 Was never implemented |
||
442 | */ |
||
443 | public function getCollections($subtype = "", $limit = 10, $offset = 0) { |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | View Code Duplication | protected function prepareObject($object) { |
|
458 | |||
459 | /* |
||
460 | * EXPORTABLE INTERFACE |
||
461 | */ |
||
462 | |||
463 | /** |
||
464 | * Return an array of fields which can be exported. |
||
465 | * |
||
466 | * @return array |
||
467 | * @deprecated 1.9 Use toObject() |
||
468 | */ |
||
469 | public function getExportableValues() { |
||
476 | |||
477 | /** |
||
478 | * Get the domain for this site |
||
479 | * |
||
480 | * @return string |
||
481 | * @since 1.9 |
||
482 | */ |
||
483 | public function getDomain() { |
||
487 | |||
488 | /** |
||
489 | * Halts bootup and redirects to the site front page |
||
490 | * if site is in walled garden mode, no user is logged in, |
||
491 | * and the URL is not a public page. |
||
492 | * |
||
493 | * @return void |
||
494 | * @since 1.8.0 |
||
495 | */ |
||
496 | public function checkWalledGarden() { |
||
528 | |||
529 | /** |
||
530 | * Returns if a URL is public for this site when in Walled Garden mode. |
||
531 | * |
||
532 | * Pages are registered to be public by {@elgg_plugin_hook public_pages walled_garden}. |
||
533 | * |
||
534 | * @param string $url Defaults to the current URL. |
||
535 | * |
||
536 | * @return bool |
||
537 | * @since 1.8.0 |
||
538 | */ |
||
539 | public function isPublicPage($url = '') { |
||
590 | } |
||
591 |
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.