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 |
||
| 26 | abstract class ElggExtender extends \ElggData { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * (non-PHPdoc) |
||
| 30 | * |
||
| 31 | * @see \ElggData::initializeAttributes() |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | protected function initializeAttributes() { |
||
| 36 | parent::initializeAttributes(); |
||
| 37 | |||
| 38 | $this->attributes['type'] = null; |
||
| 39 | $this->attributes['id'] = null; |
||
| 40 | $this->attributes['entity_guid'] = null; |
||
| 41 | $this->attributes['owner_guid'] = null; |
||
| 42 | $this->attributes['access_id'] = ACCESS_PRIVATE; |
||
| 43 | $this->attributes['enabled'] = 'yes'; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set an attribute |
||
| 48 | * |
||
| 49 | * @param string $name Name |
||
| 50 | * @param mixed $value Value |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 2 | public function __set($name, $value) { |
|
| 54 | 2 | $this->attributes[$name] = $value; |
|
| 55 | 2 | if ($name == 'value') { |
|
| 56 | 1 | $this->attributes['value_type'] = detect_extender_valuetype($value); |
|
| 57 | 1 | } |
|
| 58 | 2 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Set the value of the extender |
||
| 62 | * |
||
| 63 | * @param mixed $value The value being set |
||
| 64 | * @param string $value_type The type of the : 'integer' or 'text' |
||
| 65 | * @return void |
||
| 66 | * @since 1.9 |
||
| 67 | */ |
||
| 68 | 1 | public function setValue($value, $value_type = '') { |
|
| 69 | 1 | $this->attributes['value'] = $value; |
|
| 70 | 1 | $this->attributes['value_type'] = detect_extender_valuetype($value, $value_type); |
|
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Set an attribute |
||
| 75 | * |
||
| 76 | * @param string $name Name |
||
| 77 | * @param mixed $value Value |
||
| 78 | * @param string $value_type Value type |
||
| 79 | * |
||
| 80 | * @return boolean |
||
| 81 | * @deprecated 1.9 |
||
| 82 | */ |
||
| 83 | protected function set($name, $value, $value_type = '') { |
||
| 84 | elgg_deprecated_notice("Use -> instead of set()", 1.9); |
||
| 85 | if ($name == 'value') { |
||
| 86 | $this->setValue($value, $value_type); |
||
| 87 | } else { |
||
| 88 | $this->__set($name, $value); |
||
| 89 | } |
||
| 90 | |||
| 91 | return true; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets an attribute |
||
| 96 | * |
||
| 97 | * @param string $name Name |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | 4 | public function __get($name) { |
|
| 101 | 4 | if (array_key_exists($name, $this->attributes)) { |
|
| 102 | 3 | if ($name == 'value') { |
|
| 103 | 2 | switch ($this->attributes['value_type']) { |
|
| 104 | 2 | case 'integer' : |
|
| 105 | 2 | return (int)$this->attributes['value']; |
|
| 106 | break; |
||
|
|
|||
| 107 | 1 | case 'text' : |
|
| 108 | 1 | return $this->attributes['value']; |
|
| 109 | break; |
||
| 110 | default : |
||
| 111 | $msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type."; |
||
| 112 | throw new \UnexpectedValueException($msg); |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | 3 | return $this->attributes[$name]; |
|
| 118 | } |
||
| 119 | |||
| 120 | 1 | return null; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns an attribute |
||
| 125 | * |
||
| 126 | * @param string $name Name |
||
| 127 | * @return mixed |
||
| 128 | * @deprecated 1.9 |
||
| 129 | */ |
||
| 130 | protected function get($name) { |
||
| 131 | elgg_deprecated_notice("Use -> instead of get()", 1.9); |
||
| 132 | return $this->__get($name); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the GUID of the extender's owner entity. |
||
| 137 | * |
||
| 138 | * @return int The owner GUID |
||
| 139 | */ |
||
| 140 | public function getOwnerGUID() { |
||
| 141 | return $this->owner_guid; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the guid of the entity's owner. |
||
| 146 | * |
||
| 147 | * @return int The owner GUID |
||
| 148 | * @deprecated 1.8 Use getOwnerGUID |
||
| 149 | */ |
||
| 150 | public function getOwner() { |
||
| 151 | elgg_deprecated_notice("\ElggExtender::getOwner deprecated for \ElggExtender::getOwnerGUID", 1.8); |
||
| 152 | return $this->getOwnerGUID(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the entity that owns this extender |
||
| 157 | * |
||
| 158 | * @return \ElggEntity |
||
| 159 | */ |
||
| 160 | public function getOwnerEntity() { |
||
| 161 | return get_entity($this->owner_guid); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get the entity this describes. |
||
| 166 | * |
||
| 167 | * @return \ElggEntity The entity |
||
| 168 | */ |
||
| 169 | public function getEntity() { |
||
| 170 | return get_entity($this->entity_guid); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns if a user can edit this entity extender. |
||
| 175 | * |
||
| 176 | * @param int $user_guid The GUID of the user doing the editing |
||
| 177 | * (defaults to currently logged in user) |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | * @see elgg_set_ignore_access() |
||
| 181 | */ |
||
| 182 | abstract public function canEdit($user_guid = 0); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function toObject() { |
||
| 188 | $object = new \stdClass(); |
||
| 189 | $object->id = $this->id; |
||
| 190 | $object->entity_guid = $this->entity_guid; |
||
| 191 | $object->owner_guid = $this->owner_guid; |
||
| 192 | $object->name = $this->name; |
||
| 193 | $object->value = $this->value; |
||
| 199 | |||
| 200 | /* |
||
| 201 | * EXPORTABLE INTERFACE |
||
| 202 | */ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return an array of fields which can be exported. |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | * @deprecated 1.9 Use toObject() |
||
| 209 | */ |
||
| 210 | public function getExportableValues() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Export this object |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | * @deprecated 1.9 Use toObject() |
||
| 228 | */ |
||
| 229 | public function export() { |
||
| 239 | |||
| 240 | /* |
||
| 241 | * SYSTEM LOG INTERFACE |
||
| 242 | */ |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Return an identification for the object for storage in the system log. |
||
| 246 | * This id must be an integer. |
||
| 247 | * |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | public function getSystemLogID() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Return a type of extension. |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getType() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return a subtype. For metadata & annotations this is the 'name' and |
||
| 265 | * for relationship this is the relationship type. |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getSubtype() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get a url for this extender. |
||
| 275 | * |
||
| 276 | * Plugins can register for the 'extender:url', <type> plugin hook to |
||
| 277 | * customize the url for an annotation or metadata. |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getURL() { |
||
| 315 | |||
| 316 | } |
||
| 317 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.