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 CheckoutWizard 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 CheckoutWizard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class CheckoutWizard extends ZervWizard |
||
| 8 | { |
||
| 9 | function CheckoutWizard() |
||
| 35 | |||
| 36 | function prepare_Fieldname() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param $form |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | function process_Fieldname(&$form) |
||
| 80 | |||
| 81 | function prepare_Fieldtype() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $form |
||
| 93 | * |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | function process_Fieldtype(&$form) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param $form |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | function process_lookup(&$form) |
||
| 135 | |||
| 136 | function prepare_Settings() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param $form |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | function process_Settings(&$form) |
||
| 207 | |||
| 208 | function prepare_search() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param $form |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | function process_search(&$form) |
||
| 249 | |||
| 250 | function prepare_defaultvalue() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param $form |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | function process_defaultvalue(&$form) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param $form |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | function process_confirm(&$form) |
||
| 302 | |||
| 303 | function completeCallback() |
||
| 304 | { |
||
| 305 | global $xoopsDB; |
||
| 306 | //can this field be searched |
||
| 307 | $search = $this->getValue('hassearch'); |
||
| 308 | if ($search == "hassearch") { |
||
| 309 | $search = "1"; |
||
| 310 | $searchname = $this->getValue('searchname'); |
||
| 311 | $searchexplain = $this->getValue('searchexplain'); |
||
| 312 | } else { |
||
| 313 | $search = "0"; |
||
| 314 | $searchname = ""; |
||
| 315 | $searchexplain = ""; |
||
| 316 | } |
||
| 317 | //show in pedigree |
||
| 318 | $viewinpedigree = $this->getValue('viewinpedigree'); |
||
| 319 | if ($viewinpedigree == "viewinpedigree") { |
||
| 320 | $viewinpedigree = "1"; |
||
| 321 | } else { |
||
| 322 | $viewinpedigree = "0"; |
||
| 323 | } |
||
| 324 | //show in advanced |
||
| 325 | $viewinadvanced = $this->getValue('viewinadvanced'); |
||
| 326 | if ($viewinadvanced == "viewinadvanced") { |
||
| 327 | $viewinadvanced = "1"; |
||
| 328 | } else { |
||
| 329 | $viewinadvanced = "0"; |
||
| 330 | } |
||
| 331 | //show in pie |
||
| 332 | $viewinpie = $this->getValue('viewinpie'); |
||
| 333 | if ($viewinpie == "viewinpie") { |
||
| 334 | $viewinpie = "1"; |
||
| 335 | } else { |
||
| 336 | $viewinpie = "0"; |
||
| 337 | } |
||
| 338 | //view in list |
||
| 339 | $viewinlist = $this->getValue('viewinlist'); |
||
| 340 | if ($viewinlist == "viewinlist") { |
||
| 341 | $viewinlist = "1"; |
||
| 342 | } else { |
||
| 343 | $viewinlist = "0"; |
||
| 344 | } |
||
| 345 | //add a litter |
||
| 346 | $Litter = $this->getValue('Litter'); |
||
| 347 | if ($Litter == "Litter") { |
||
| 348 | $Litter = "1"; |
||
| 349 | } else { |
||
| 350 | $Litter = "0"; |
||
| 351 | } |
||
| 352 | //general litter |
||
| 353 | $Generallitter = $this->getValue('Generallitter'); |
||
| 354 | if ($Generallitter == "Generallitter") { |
||
| 355 | $Generallitter = "1"; |
||
| 356 | } else { |
||
| 357 | $Generallitter = "0"; |
||
| 358 | } |
||
| 359 | |||
| 360 | if (!$this->getValue('field') == 0) // field allready exists (editing mode) |
||
| 361 | { |
||
| 362 | $sql = "UPDATE " . $xoopsDB->prefix("pedigree_fields") . " SET FieldName = '" . htmlSpecialChars($this->getValue('name')) . "', FieldType = '" . $this->getValue('fieldtype') |
||
| 363 | . "', DefaultValue = '" . $this->getValue('defaultvalue') . "', FieldExplenation = '" . $this->getValue('explain') . "', HasSearch = '" . $search . "', Litter = '" . $Litter |
||
| 364 | . "', Generallitter = '" . $Generallitter . "', SearchName = '" . $searchname . "', SearchExplenation = '" . $searchexplain . "', ViewInPedigree = '" . $viewinpedigree |
||
| 365 | . "', ViewInAdvanced = '" . $viewinadvanced . "', ViewInPie = '" . $viewinpie . "', ViewInList = '" . $viewinlist . "' WHERE ID ='" . $this->getValue('field') . "'"; |
||
| 366 | $xoopsDB->queryF($sql); |
||
| 367 | //possible change defaultvalue for userfield |
||
| 368 | $sql |
||
| 369 | = "ALTER TABLE " . $xoopsDB->prefix("pedigree_tree") . " CHANGE `user" . $this->getValue('field') . "` `user" . $this->getValue( |
||
| 370 | 'field' |
||
| 371 | ) . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
| 372 | $xoopsDB->queryF($sql); |
||
| 373 | $sql |
||
| 374 | = "ALTER TABLE " . $xoopsDB->prefix("pedigree_temp") . " CHANGE `user" . $this->getValue('field') . "` `user" . $this->getValue( |
||
| 375 | 'field' |
||
| 376 | ) . "` VARCHAR( 1024 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
| 377 | $xoopsDB->queryF($sql); |
||
| 378 | $sql = "ALTER TABLE " . $xoopsDB->prefix("pedigree_trash") . " CHANGE `user" . $this->getValue('field') . "` `user" . $this->getValue( |
||
| 379 | 'field' |
||
| 380 | ) . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
| 381 | $xoopsDB->queryF($sql); |
||
| 382 | } else { //this is a new field |
||
| 383 | $sql = "SELECT MAX(ID) AS lid from " . $xoopsDB->prefix("pedigree_fields") . " LIMIT 1"; |
||
| 384 | $result = $xoopsDB->query($sql); |
||
| 385 | while ($row = $xoopsDB->fetchArray($result)) { |
||
| 386 | $nextfieldnum = $row['lid'] + 1; |
||
| 387 | } |
||
| 388 | //add userfield to various tables as a new field. |
||
| 389 | //allways add at the end of the table |
||
| 390 | $tables = array('pedigree_tree', 'pedigree_temp', 'pedigree_trash'); |
||
| 391 | foreach ($tables as $table) { |
||
| 392 | $SQL = "ALTER TABLE " . $xoopsDB->prefix($table) . " ADD `user" . $nextfieldnum . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue( |
||
| 393 | 'defaultvalue' |
||
| 394 | ) . "'"; |
||
| 395 | $xoopsDB->queryF($SQL); |
||
| 396 | } |
||
| 397 | //is a lookup table present |
||
| 398 | $lookup = $this->getValue('lookup1'); |
||
| 399 | if ($lookup == "") { |
||
| 400 | $lookup = "0"; |
||
| 401 | } else { |
||
| 402 | $lookup = "1"; |
||
| 403 | //create table for lookupfield |
||
| 404 | $createtable = "CREATE TABLE " . $xoopsDB->prefix("pedigree_lookup" . $nextfieldnum) . " (`ID` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM"; |
||
| 405 | $xoopsDB->queryF($createtable); |
||
| 406 | //fill table |
||
| 407 | $count = $this->getValue('fc'); |
||
| 408 | for ($x = 1; $x < $count + 1; ++$x) { |
||
| 409 | $y = $x - 1; |
||
| 410 | $sql = "INSERT INTO " . $xoopsDB->prefix("pedigree_lookup" . $nextfieldnum) . " ( `ID` , `value`, `order`) VALUES ('" . $y . "', '" . $this->getValue('lookup' . $x) . "','" . $y |
||
| 411 | . "')"; |
||
| 412 | $xoopsDB->queryF($sql); |
||
| 413 | } |
||
| 414 | |||
| 415 | } |
||
| 416 | |||
| 417 | //Insert new record into pedigree_config |
||
| 418 | $sql = "INSERT INTO " . $xoopsDB->prefix("pedigree_fields") . " VALUES ('" . $nextfieldnum . "', '1', '" . htmlSpecialChars( |
||
| 419 | $this->getValue('name') |
||
| 420 | ) . "', '" . $this->getValue('fieldtype') . "', '" . $lookup . "', '" . $this->getValue('defaultvalue') . "', '" . $this->getValue( |
||
| 421 | 'explain' |
||
| 422 | ) . "', '" . $search . "', '" . $Litter . "', '" . $Generallitter . "', '" . $searchname . "', '" . $searchexplain . "', '" . $viewinpedigree . "', '" . $viewinadvanced . "', '" |
||
| 423 | . $viewinpie . "', '" . $viewinlist . "','','" . $nextfieldnum . "')"; |
||
| 424 | $xoopsDB->queryF($sql); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Miscellaneous utility functions |
||
| 430 | * |
||
| 431 | * @param $email |
||
| 432 | * |
||
| 433 | * @return int |
||
| 434 | */ |
||
| 435 | |||
| 436 | function isValidEmail($email) |
||
| 440 | } |
||
| 441 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.