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 |
||
| 60 | class ProfileProfileHandler extends XoopsPersistableObjectHandler |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var bool|ProfileFieldHandler |
||
| 64 | */ |
||
| 65 | private $_fHandler; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Array of {@link ProfileField} objects |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $_fields = array(); |
||
| 73 | |||
| 74 | public function __construct(Connection $db) |
||
| 75 | { |
||
| 76 | parent::__construct($db, 'profile_profile', 'ProfileProfile', 'profile_id'); |
||
| 77 | $xoops = Xoops::getInstance(); |
||
| 78 | $this->_fHandler = \Xoops::getModuleHelper('profile')->getHandler('field'); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * create a new {@link ProfileProfile} |
||
| 83 | * |
||
| 84 | * @param bool $isNew Flag the new objects as "new"? |
||
| 85 | * |
||
| 86 | * @return ProfileProfile {@link ProfileProfile} |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function create($isNew = true) |
|
| 89 | { |
||
| 90 | /* @var $obj ProfileProfile */ |
||
| 91 | $obj = new $this->className($this->loadFields()); |
||
| 92 | $obj->handler = $this; |
||
| 93 | if ($isNew === true) { |
||
| 94 | $obj->setNew(); |
||
| 95 | } |
||
| 96 | return $obj; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get a {@link ProfileProfile} |
||
| 101 | * |
||
| 102 | * @param $uid |
||
| 103 | * @param bool $createOnFailure create a new {@link ProfileProfile} if none is fetched |
||
| 104 | * |
||
| 105 | * @return null|ProfileProfile|XoopsObject |
||
| 106 | */ |
||
| 107 | public function getProfile($uid, $createOnFailure = true) |
||
| 108 | { |
||
| 109 | $obj = parent::get($uid); |
||
| 110 | if (!is_object($obj) && $createOnFailure) { |
||
| 111 | $obj = $this->create(); |
||
| 112 | } |
||
| 113 | return $obj; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Create new {@link ProfileField} object |
||
| 118 | * |
||
| 119 | * @param bool $isNew |
||
| 120 | * |
||
| 121 | * @return ProfileField |
||
| 122 | */ |
||
| 123 | public function createField($isNew = true) |
||
| 124 | { |
||
| 125 | $return = $this->_fHandler->create($isNew); |
||
| 126 | return $return; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Load field information |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function loadFields() |
||
| 135 | { |
||
| 136 | if (count($this->_fields) == 0) { |
||
| 137 | $this->_fields = $this->_fHandler->loadFields(); |
||
| 138 | } |
||
| 139 | return $this->_fields; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Fetch fields |
||
| 144 | * |
||
| 145 | * @param CriteriaElement $criteria {@link CriteriaElement} object |
||
| 146 | * @param bool $id_as_key return array with field IDs as key? |
||
| 147 | * @param bool $as_object return array of objects? |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | **/ |
||
| 151 | public function getFields(CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Insert a field in the database |
||
| 158 | * |
||
| 159 | * @param ProfileField $field |
||
| 160 | * @param bool $force |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function insertField(ProfileField $field, $force = false) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Delete a field from the database |
||
| 171 | * |
||
| 172 | * @param ProfileField $field |
||
| 173 | * @param bool $force |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function deleteField(ProfileField $field, $force = false) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Save a new field in the database |
||
| 184 | * |
||
| 185 | * @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
||
| 186 | * @param int $weight |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function saveField($vars, $weight = 0) |
||
| 191 | { |
||
| 192 | $field = $this->createField(); |
||
| 193 | $field->setVar('field_name', $vars['name']); |
||
| 194 | $field->setVar('field_valuetype', $vars['valuetype']); |
||
| 195 | $field->setVar('field_type', $vars['type']); |
||
| 196 | $field->setVar('field_weight', $weight); |
||
| 197 | if (isset($vars['title'])) { |
||
| 198 | $field->setVar('field_title', $vars['title']); |
||
| 199 | } |
||
| 200 | if (isset($vars['description'])) { |
||
| 201 | $field->setVar('field_description', $vars['description']); |
||
| 202 | } |
||
| 203 | if (isset($vars['required'])) { |
||
| 204 | $field->setVar('field_required', $vars['required']); //0 = no, 1 = yes |
||
| 205 | } |
||
| 206 | if (isset($vars['maxlength'])) { |
||
| 207 | $field->setVar('field_maxlength', $vars['maxlength']); |
||
| 208 | } |
||
| 209 | if (isset($vars['default'])) { |
||
| 210 | $field->setVar('field_default', $vars['default']); |
||
| 211 | } |
||
| 212 | if (isset($vars['notnull'])) { |
||
| 213 | $field->setVar('field_notnull', $vars['notnull']); |
||
| 214 | } |
||
| 215 | if (isset($vars['show'])) { |
||
| 216 | $field->setVar('field_show', $vars['show']); |
||
| 217 | } |
||
| 218 | if (isset($vars['edit'])) { |
||
| 219 | $field->setVar('field_edit', $vars['edit']); |
||
| 220 | } |
||
| 221 | if (isset($vars['config'])) { |
||
| 222 | $field->setVar('field_config', $vars['config']); |
||
| 223 | } |
||
| 224 | if (isset($vars['options'])) { |
||
| 225 | $field->setVar('field_options', $vars['options']); |
||
| 226 | } else { |
||
| 227 | $field->setVar('field_options', array()); |
||
| 228 | } |
||
| 229 | if ($this->insertField($field)) { |
||
| 230 | $msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database'; |
||
| 231 | } else { |
||
| 232 | $msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . implode(' ', $field->getErrors()) . $this->db2->errorInfo() . '</span>'; |
||
| 233 | } |
||
| 234 | unset($field); |
||
| 235 | return $msg; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * insert a new object in the database |
||
| 240 | * |
||
| 241 | * @param XoopsObject|ProfileProfile $obj reference to the object |
||
| 242 | * @param bool $force whether to force the query execution despite security settings |
||
| 243 | * |
||
| 244 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 245 | */ |
||
| 246 | public function insert(XoopsObject $obj, $force = false) |
||
| 247 | { |
||
| 248 | $uservars = $this->getUserVars(); |
||
| 249 | foreach ($uservars as $var) { |
||
| 250 | unset($obj->vars[$var]); |
||
| 251 | } |
||
| 252 | if (count($obj->vars) == 0) { |
||
| 253 | return true; |
||
| 254 | } |
||
| 255 | return parent::insert($obj, $force); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get array of standard variable names (user table) |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function getUserVars() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Search profiles and users |
||
| 270 | * |
||
| 271 | * @param CriteriaElement $criteria CriteriaElement |
||
| 272 | * @param array $searchvars Fields to be fetched |
||
| 273 | * @param array $groups for Usergroups is selected (only admin!) |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public function search(CriteriaElement $criteria, $searchvars = array(), $groups = null) |
||
| 336 | } |
||
| 337 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: