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 XoopsTplFileHandler 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 XoopsTplFileHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class XoopsTplFileHandler extends XoopsPersistableObjectHandler |
||
| 41 | { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param Connection|null $db database |
||
| 47 | */ |
||
| 48 | 13 | public function __construct(Connection $db = null) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * retrieve a specific XoopsTplFile |
||
| 55 | * |
||
| 56 | * @param int $id tpl_id of the block to retrieve |
||
| 57 | * @param bool $getsource true = also return source |
||
| 58 | * |
||
| 59 | * @return XoopsTplFile|bool |
||
| 60 | */ |
||
| 61 | 1 | public function getById($id, $getsource = false) |
|
| 62 | { |
||
| 63 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
| 64 | 1 | $eb = $qb->expr(); |
|
| 65 | 1 | $tplfile = false; |
|
| 66 | 1 | $id = (int)($id); |
|
| 67 | 1 | if ($id > 0) { |
|
| 68 | 1 | if (!$getsource) { |
|
| 69 | 1 | $qb->select('*') |
|
| 70 | 1 | ->fromPrefix('system_tplfile', 'f') |
|
| 71 | 1 | ->where($eb->eq('f.tpl_id', ':tplid')) |
|
| 72 | 1 | ->setParameter(':tplid', $id, \PDO::PARAM_INT); |
|
| 73 | View Code Duplication | } else { |
|
| 74 | 1 | $qb->select('f.*') |
|
| 75 | 1 | ->addSelect('s.tpl_source') |
|
| 76 | 1 | ->fromPrefix('system_tplfile', 'f') |
|
| 77 | 1 | ->leftJoinPrefix('f', 'system_tplsource', 's', $eb->eq('s.tpl_id', 'f.tpl_id')) |
|
| 78 | 1 | ->where($eb->eq('f.tpl_id', ':tplid')) |
|
| 79 | 1 | ->setParameter(':tplid', $id, \PDO::PARAM_INT); |
|
| 80 | } |
||
| 81 | 1 | $result = $qb->execute(); |
|
| 82 | 1 | if (!$result) { |
|
| 83 | return $tplfile; |
||
| 84 | } |
||
| 85 | 1 | $allrows = $result->fetchAll(); |
|
| 86 | 1 | if (count($allrows) == 1) { |
|
| 87 | 1 | $tplfile = new XoopsTplFile(); |
|
| 88 | 1 | $tplfile->assignVars(reset($allrows)); |
|
| 89 | } |
||
| 90 | } |
||
| 91 | 1 | return $tplfile; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * loadSource |
||
| 96 | * |
||
| 97 | * @param XoopsTplFile $tplfile object |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | 1 | public function loadSource(XoopsTplFile $tplfile) |
|
| 102 | { |
||
| 103 | 1 | if (!$tplfile->getVar('tpl_source')) { |
|
| 104 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
| 105 | 1 | $eb = $qb->expr(); |
|
| 106 | 1 | $qb->select('tpl_source') |
|
| 107 | 1 | ->fromPrefix('system_tplsource', null) |
|
| 108 | 1 | ->where($eb->eq('tpl_id', ':tplid')) |
|
| 109 | 1 | ->setParameter(':tplid', $tplfile->getVar('tpl_id'), \PDO::PARAM_INT); |
|
| 110 | 1 | if (!$result = $qb->execute()) { |
|
| 111 | return false; |
||
| 112 | } |
||
| 113 | 1 | $myrow = $result->fetch(\PDO::FETCH_ASSOC); |
|
| 114 | 1 | $tplfile->assignVar('tpl_source', $myrow['tpl_source']); |
|
| 115 | } |
||
| 116 | 1 | return true; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * write a new TplFile into the database |
||
| 121 | * |
||
| 122 | * @param XoopsTplFile $tplfile object |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 2 | public function insertTpl(XoopsTplFile $tplfile) |
|
| 127 | { |
||
| 128 | 2 | $tpl_id = 0; |
|
| 129 | 2 | if (!$tplfile->isDirty()) { |
|
| 130 | 1 | return true; |
|
| 131 | } |
||
| 132 | 1 | if (!$tplfile->cleanVars()) { |
|
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | 1 | $vars = $tplfile->cleanVars; |
|
| 137 | 1 | $tpl_module = $vars['tpl_module']; |
|
| 138 | 1 | $tpl_refid = $vars['tpl_refid']; |
|
| 139 | 1 | $tpl_tplset = $vars['tpl_tplset']; |
|
| 140 | 1 | $tpl_file = $vars['tpl_file']; |
|
| 141 | 1 | $tpl_desc = $vars['tpl_desc']; |
|
| 142 | 1 | $tpl_lastmodified = $vars['tpl_lastmodified']; |
|
| 143 | 1 | $tpl_lastimported = $vars['tpl_lastimported']; |
|
| 144 | 1 | $tpl_type = $vars['tpl_type']; |
|
| 145 | 1 | if (isset($vars['tpl_id'])) { |
|
| 146 | $tpl_id = $vars['tpl_id']; |
||
| 147 | } |
||
| 148 | 1 | if (isset($vars['tpl_source'])) { |
|
| 149 | $tpl_source = $vars['tpl_source']; |
||
| 150 | } |
||
| 151 | |||
| 152 | 1 | if ($tplfile->isNew()) { |
|
| 153 | 1 | $tpl_id = 0; |
|
| 154 | $values = array( |
||
| 155 | // 'tpl_id' => $tpl_id, |
||
| 156 | 1 | 'tpl_module' => $tpl_module, |
|
| 157 | 1 | 'tpl_refid' => $tpl_refid, |
|
| 158 | 1 | 'tpl_tplset' => $tpl_tplset, |
|
| 159 | 1 | 'tpl_file' => $tpl_file, |
|
| 160 | 1 | 'tpl_desc' => $tpl_desc, |
|
| 161 | 1 | 'tpl_lastmodified' => $tpl_lastmodified, |
|
| 162 | 1 | 'tpl_lastimported' => $tpl_lastimported, |
|
| 163 | 1 | 'tpl_type' => $tpl_type, |
|
| 164 | ); |
||
| 165 | 1 | if (!$this->db2->insertPrefix('system_tplfile', $values)) { |
|
| 166 | return false; |
||
| 167 | } |
||
| 168 | 1 | if (empty($tpl_id)) { |
|
| 169 | 1 | $tpl_id = $this->db2->lastInsertId(); |
|
| 170 | } |
||
| 171 | 1 | if (isset($tpl_source) && $tpl_source != '') { |
|
| 172 | $values = array( |
||
| 173 | 'tpl_id' => $tpl_id, |
||
| 174 | 'tpl_source' => $tpl_source, |
||
| 175 | ); |
||
| 176 | if (!$this->db2->insertPrefix('system_tplsource', $values)) { |
||
| 177 | $this->db2->deletePrefix('system_tplfile', array('tpl_id' => $tpl_id)); |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | 1 | $tplfile->assignVar('tpl_id', $tpl_id); |
|
| 182 | } else { |
||
| 183 | $values = array( |
||
| 184 | // 'tpl_id' => $tpl_id, |
||
| 185 | 'tpl_module' => $tpl_module, |
||
| 186 | 'tpl_refid' => $tpl_refid, |
||
| 187 | 'tpl_tplset' => $tpl_tplset, |
||
| 188 | 'tpl_file' => $tpl_file, |
||
| 189 | 'tpl_desc' => $tpl_desc, |
||
| 190 | 'tpl_lastmodified' => $tpl_lastmodified, |
||
| 191 | 'tpl_lastimported' => $tpl_lastimported, |
||
| 192 | 'tpl_type' => $tpl_type, |
||
| 193 | ); |
||
| 194 | if (!$this->db2->updatePrefix('system_tplfile', $values, array('tpl_id' => $tpl_id))) { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | View Code Duplication | if (isset($tpl_source) && $tpl_source != '') { |
|
| 199 | $values = array( |
||
| 200 | // 'tpl_id' => $tpl_id, |
||
| 201 | 'tpl_source' => $tpl_source, |
||
| 202 | ); |
||
| 203 | if ($this->db2->updatePrefix('system_tplsource', $values, array('tpl_id' => $tpl_id))) { |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | 1 | return true; |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * forceUpdate |
||
| 214 | * |
||
| 215 | * @param XoopsTplFile $tplfile object |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | 1 | public function forceUpdate(XoopsTplFile $tplfile) |
|
| 220 | { |
||
| 221 | 1 | if (!$tplfile->isDirty()) { |
|
| 222 | 1 | return true; |
|
| 223 | } |
||
| 224 | if (!$tplfile->cleanVars()) { |
||
| 225 | return false; |
||
| 226 | } |
||
| 227 | |||
| 228 | $vars = $tplfile->cleanVars; |
||
| 229 | $tpl_module = $vars['tpl_module']; |
||
| 230 | $tpl_refid = $vars['tpl_refid']; |
||
| 231 | $tpl_tplset = $vars['tpl_tplset']; |
||
| 232 | $tpl_file = $vars['tpl_file']; |
||
| 233 | $tpl_desc = $vars['tpl_desc']; |
||
| 234 | $tpl_lastmodified = $vars['tpl_lastmodified']; |
||
| 235 | $tpl_lastimported = $vars['tpl_lastimported']; |
||
| 236 | $tpl_type = $vars['tpl_type']; |
||
| 237 | //$tpl_id = $vars['tpl_id']; |
||
| 238 | if (isset($vars['tpl_source'])) { |
||
| 239 | $tpl_source = $vars['tpl_source']; |
||
| 240 | } |
||
| 241 | |||
| 242 | if (!$tplfile->isNew()) { |
||
| 243 | $tpl_id = 0; |
||
| 244 | $values = array( |
||
| 245 | // 'tpl_id' => $tpl_id, |
||
| 246 | 'tpl_module' => $tpl_module, |
||
| 247 | 'tpl_refid' => $tpl_refid, |
||
| 248 | 'tpl_tplset' => $tpl_tplset, |
||
| 249 | 'tpl_file' => $tpl_file, |
||
| 250 | 'tpl_desc' => $tpl_desc, |
||
| 251 | 'tpl_lastmodified' => $tpl_lastmodified, |
||
| 252 | 'tpl_lastimported' => $tpl_lastimported, |
||
| 253 | 'tpl_type' => $tpl_type, |
||
| 254 | ); |
||
| 255 | if (!$this->db2->updatePrefix('system_tplfile', $values, array('tpl_id' => $tpl_id))) { |
||
| 256 | return false; |
||
| 257 | } |
||
| 258 | |||
| 259 | View Code Duplication | if (isset($tpl_source) && $tpl_source != '') { |
|
| 260 | $tpl_id = 0; |
||
| 261 | $values = array( |
||
| 262 | // 'tpl_id' => $tpl_id, |
||
| 263 | 'tpl_source' => $tpl_source, |
||
| 264 | ); |
||
| 265 | if ($this->db2->updatePrefix('system_tplsource', $values, array('tpl_id' => $tpl_id))) { |
||
| 266 | return false; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | return true; |
||
| 271 | } else { |
||
| 272 | return false; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * delete a block from the database |
||
| 278 | * |
||
| 279 | * @param XoopsTplFile $tplfile object |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 1 | public function deleteTpl(XoopsTplFile $tplfile) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * getTplObjects |
||
| 295 | * |
||
| 296 | * @param CriteriaElement|null $criteria criteria to match |
||
| 297 | * @param bool $getsource include the source |
||
| 298 | * @param bool $id_as_key use the object id as array key |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | 2 | public function getTplObjects(CriteriaElement $criteria = null, $getsource = false, $id_as_key = false) |
|
| 303 | { |
||
| 304 | 2 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
| 305 | 2 | $eb = $qb->expr(); |
|
| 306 | |||
| 307 | 2 | $ret = array(); |
|
| 308 | |||
| 309 | 2 | View Code Duplication | if (!$getsource) { |
| 310 | 2 | $qb->select('*') |
|
| 311 | 2 | ->fromPrefix('system_tplfile', 'f'); |
|
| 312 | } else { |
||
| 313 | 1 | $qb->select('f.*') |
|
| 314 | 1 | ->addSelect('s.tpl_source') |
|
| 315 | 1 | ->fromPrefix('system_tplfile', 'f') |
|
| 316 | 1 | ->leftJoinPrefix('f', 'system_tplsource', 's', $eb->eq('s.tpl_id', 'f.tpl_id')); |
|
| 317 | } |
||
| 318 | 2 | if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
|
| 319 | 2 | $criteria->renderQb($qb); |
|
| 320 | } |
||
| 321 | 2 | $result = $qb->execute(); |
|
| 322 | 2 | if (!$result) { |
|
| 323 | return $ret; |
||
| 324 | } |
||
| 325 | 2 | View Code Duplication | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
| 326 | 2 | $tplfile = new XoopsTplFile(); |
|
| 327 | 2 | $tplfile->assignVars($myrow); |
|
| 328 | 2 | if (!$id_as_key) { |
|
| 329 | 2 | $ret[] = $tplfile; |
|
| 330 | } else { |
||
| 331 | 1 | $ret[$myrow['tpl_id']] = $tplfile; |
|
| 332 | } |
||
| 333 | 2 | unset($tplfile); |
|
| 334 | } |
||
| 335 | 2 | return $ret; |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * getModuleTplCount |
||
| 340 | * |
||
| 341 | * @param string $tplset tpl set name |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | 1 | public function getModuleTplCount($tplset) |
|
| 346 | { |
||
| 347 | 1 | $qb = $this->db2->createXoopsQueryBuilder(); |
|
| 348 | 1 | $eb = $qb->expr(); |
|
| 349 | |||
| 350 | 1 | $qb->select('tpl_module') |
|
| 351 | 1 | ->addSelect('COUNT(tpl_id) AS count') |
|
| 352 | 1 | ->fromPrefix('system_tplfile', null) |
|
| 353 | 1 | ->where($eb->eq('tpl_tplset', ':tpset')) |
|
| 354 | 1 | ->groupBy('tpl_module') |
|
| 355 | 1 | ->setParameter(':tpset', $tplset, \PDO::PARAM_STR); |
|
| 356 | |||
| 357 | 1 | $ret = array(); |
|
| 358 | 1 | $result = $qb->execute(); |
|
| 359 | 1 | if (!$result) { |
|
| 360 | return $ret; |
||
| 361 | } |
||
| 362 | 1 | while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
| 363 | 1 | if ($myrow['tpl_module'] != '') { |
|
| 364 | 1 | $ret[$myrow['tpl_module']] = $myrow['count']; |
|
| 365 | } |
||
| 366 | } |
||
| 367 | 1 | return $ret; |
|
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Find Template File |
||
| 372 | * |
||
| 373 | * @param string|null $tplset template set |
||
| 374 | * @param string|null $type template type |
||
| 375 | * @param string|null $refid reference id |
||
| 376 | * @param string|null $module module |
||
| 377 | * @param string|null $file file name |
||
| 378 | * @param bool $getsource include template source |
||
| 379 | * |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | 1 | public function find($tplset = null, $type = null, $refid = null, $module = null, $file = null, $getsource = false) |
|
| 383 | { |
||
| 384 | 1 | $criteria = new CriteriaCompo(); |
|
| 385 | 1 | if (isset($tplset)) { |
|
| 386 | 1 | $criteria->add(new Criteria('tpl_tplset', $tplset)); |
|
| 387 | } |
||
| 388 | 1 | if (isset($module)) { |
|
| 389 | 1 | $criteria->add(new Criteria('tpl_module', $module)); |
|
| 390 | } |
||
| 391 | 1 | if (isset($refid)) { |
|
| 392 | 1 | $criteria->add(new Criteria('tpl_refid', $refid)); |
|
| 393 | } |
||
| 394 | 1 | if (isset($file)) { |
|
| 395 | 1 | $criteria->add(new Criteria('tpl_file', $file)); |
|
| 396 | } |
||
| 397 | 1 | View Code Duplication | if (isset($type)) { |
| 398 | 1 | if (is_array($type)) { |
|
| 399 | 1 | $criteria2 = new CriteriaCompo(); |
|
| 400 | 1 | foreach ($type as $t) { |
|
| 401 | 1 | $criteria2->add(new Criteria('tpl_type', $t), 'OR'); |
|
| 402 | } |
||
| 403 | 1 | $criteria->add($criteria2); |
|
| 404 | } else { |
||
| 405 | 1 | $criteria->add(new Criteria('tpl_type', $type)); |
|
| 406 | } |
||
| 407 | } |
||
| 408 | 1 | return $this->getTplObjects($criteria, $getsource, false); |
|
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Template Exists |
||
| 413 | * |
||
| 414 | * @param string $tplname template name |
||
| 415 | * @param string $tplset_name set name |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | 1 | public function templateExists($tplname, $tplset_name) |
|
| 428 | } |
||
| 429 |