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 BaseSmartObject 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 BaseSmartObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace XoopsModules\Smartobject; |
||
| 59 | class BaseSmartObject extends \XoopsObject |
||
| 60 | { |
||
| 61 | public $_image_path; |
||
| 62 | public $_image_url; |
||
| 63 | |||
| 64 | public $seoEnabled = false; |
||
| 65 | public $titleField; |
||
| 66 | public $summaryField = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Reference to the handler managing this object |
||
| 70 | * |
||
| 71 | * @var PersistableObjectHandler reference to {@link SmartPersistableObjectHandler} |
||
| 72 | */ |
||
| 73 | public $handler; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * References to control objects, managing the form fields of this object |
||
| 77 | */ |
||
| 78 | public $controls = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * SmartObject constructor. |
||
| 82 | * @param $handler |
||
| 83 | */ |
||
| 84 | public function __construct($handler) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Checks if the user has a specific access on this object |
||
| 91 | * |
||
| 92 | * @param $perm_name |
||
| 93 | * @return bool: TRUE if user has access, false if not |
||
|
|
|||
| 94 | * @internal param string $gperm_name name of the permission to test |
||
| 95 | */ |
||
| 96 | public function accessGranted($perm_name) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param $section_name |
||
| 105 | * @param bool $value |
||
| 106 | * @param bool $hide |
||
| 107 | */ |
||
| 108 | public function addFormSection($section_name, $value = false, $hide = false) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param $section_name |
||
| 116 | */ |
||
| 117 | public function closeSection($section_name) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * |
||
| 124 | * @param string $key key of this field. This needs to be the name of the field in the related database table |
||
| 125 | * @param int $data_type set to one of XOBJ_DTYPE_XXX constants (set to XOBJ_DTYPE_OTHER if no data type ckecking nor text sanitizing is required) |
||
| 126 | * @param mixed $value default value of this variable |
||
| 127 | * @param bool $required set to TRUE if this variable needs to have a value set before storing the object in the table |
||
| 128 | * @param int $maxlength maximum length of this variable, for XOBJ_DTYPE_TXTBOX type only |
||
| 129 | * @param string $options does this data have any select options? |
||
| 130 | * @param bool $multilingual is this field needs to support multilingual features (NOT YET IMPLEMENTED...) |
||
| 131 | * @param string $form_caption caption of this variable in a {@link SmartobjectForm} and title of a column in a {@link SmartObjectTable} |
||
| 132 | * @param string $form_dsc description of this variable in a {@link SmartobjectForm} |
||
| 133 | * @param bool $sortby set to TRUE to make this field used to sort objects in SmartObjectTable |
||
| 134 | * @param bool $persistent set to FALSE if this field is not to be saved in the database |
||
| 135 | * @param bool $displayOnForm |
||
| 136 | */ |
||
| 137 | public function initVar( |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $key |
||
| 183 | * @param $data_type |
||
| 184 | * @param bool $itemName |
||
| 185 | * @param string $form_caption |
||
| 186 | * @param bool $sortby |
||
| 187 | * @param string $value |
||
| 188 | * @param bool $displayOnForm |
||
| 189 | * @param bool $required |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function initNonPersistableVar( |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Quickly initiate a var |
||
| 207 | * |
||
| 208 | * Since many vars do have the same config, let's use this method with some of these configuration as a convention ;-) |
||
| 209 | * |
||
| 210 | * - $maxlength = 0 unless $data_type is a TEXTBOX, then $maxlength will be 255 |
||
| 211 | * - all other vars are NULL or '' depending of the parameter |
||
| 212 | * |
||
| 213 | * @param string $key key of this field. This needs to be the name of the field in the related database table |
||
| 214 | * @param int $data_type set to one of XOBJ_DTYPE_XXX constants (set to XOBJ_DTYPE_OTHER if no data type ckecking nor text sanitizing is required) |
||
| 215 | * @param bool $required set to TRUE if this variable needs to have a value set before storing the object in the table |
||
| 216 | * @param string $form_caption caption of this variable in a {@link SmartobjectForm} and title of a column in a {@link SmartObjectTable} |
||
| 217 | * @param string $form_dsc description of this variable in a {@link SmartobjectForm} |
||
| 218 | * @param mixed $value default value of this variable |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function quickInitVar( |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param $varname |
||
| 234 | * @param bool $displayOnForm |
||
| 235 | * @param string $default |
||
| 236 | */ |
||
| 237 | public function initCommonVar($varname, $displayOnForm = true, $default = 'notdefined') |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set control information for an instance variable |
||
| 321 | * |
||
| 322 | * The $options parameter can be a string or an array. Using a string |
||
| 323 | * is the quickest way: |
||
| 324 | * |
||
| 325 | * $this->setControl('date', 'date_time'); |
||
| 326 | * |
||
| 327 | * This will create a date and time selectbox for the 'date' var on the |
||
| 328 | * form to edit or create this item. |
||
| 329 | * |
||
| 330 | * Here are the currently supported controls: |
||
| 331 | * |
||
| 332 | * - color |
||
| 333 | * - country |
||
| 334 | * - date_time |
||
| 335 | * - date |
||
| 336 | |||
| 337 | * - group |
||
| 338 | * - group_multi |
||
| 339 | * - image |
||
| 340 | * - imageupload |
||
| 341 | * - label |
||
| 342 | * - language |
||
| 343 | * - parentcategory |
||
| 344 | * - password |
||
| 345 | * - select_multi |
||
| 346 | * - select |
||
| 347 | * - text |
||
| 348 | * - textarea |
||
| 349 | * - theme |
||
| 350 | * - theme_multi |
||
| 351 | * - timezone |
||
| 352 | * - user |
||
| 353 | * - user_multi |
||
| 354 | * - yesno |
||
| 355 | * |
||
| 356 | * Now, using an array as $options, you can customize what information to |
||
| 357 | * use in the control. For example, if one needs to display a select box for |
||
| 358 | * the user to choose the status of an item. We only need to tell SmartObject |
||
| 359 | * what method to execute within what handler to retreive the options of the |
||
| 360 | * selectbox. |
||
| 361 | * |
||
| 362 | * $this->setControl('status', array('name' => false, |
||
| 363 | * 'itemHandler' => 'item', |
||
| 364 | * 'method' => 'getStatus', |
||
| 365 | * 'module' => 'smartshop')); |
||
| 366 | * |
||
| 367 | * In this example, the array elements are the following: |
||
| 368 | * - name: false, as we don't need to set a special control here. |
||
| 369 | * we will use the default control related to the object type (defined in initVar) |
||
| 370 | * - itemHandler: name of the object for which we will use the handler |
||
| 371 | * - method: name of the method of this handler that we will execute |
||
| 372 | * - module: name of the module from wich the handler is |
||
| 373 | * |
||
| 374 | * So in this example, SmartObject will create a selectbox for the variable 'status' and it will |
||
| 375 | * populate this selectbox with the result from SmartshopItemHandler::getStatus() |
||
| 376 | * |
||
| 377 | * Another example of the use of $options as an array is for TextArea: |
||
| 378 | * |
||
| 379 | * $this->setControl('body', array('name' => 'textarea', |
||
| 380 | * 'form_editor' => 'default')); |
||
| 381 | * |
||
| 382 | * In this example, SmartObject will create a TextArea for the variable 'body'. And it will use |
||
| 383 | * the 'default' editor, providing it is defined in the module |
||
| 384 | * preferences: $helper->getConfig('default_editor') |
||
| 385 | * |
||
| 386 | * Of course, you can force the use of a specific editor: |
||
| 387 | * |
||
| 388 | * $this->setControl('body', array('name' => 'textarea', |
||
| 389 | * 'form_editor' => 'koivi')); |
||
| 390 | * |
||
| 391 | * Here is a list of supported editor: |
||
| 392 | * - tiny: TinyEditor |
||
| 393 | * - dhtmltextarea: XOOPS DHTML Area |
||
| 394 | * - fckeditor: FCKEditor |
||
| 395 | * - inbetween: InBetween |
||
| 396 | * - koivi: Koivi |
||
| 397 | * - spaw: Spaw WYSIWYG Editor |
||
| 398 | * - htmlarea: HTMLArea |
||
| 399 | * - textarea: basic textarea with no options |
||
| 400 | * |
||
| 401 | * @param string $var name of the variable for which we want to set a control |
||
| 402 | * @param array $options |
||
| 403 | */ |
||
| 404 | public function setControl($var, $options = []) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get control information for an instance variable |
||
| 417 | * |
||
| 418 | * @param string $var |
||
| 419 | * @return bool|mixed |
||
| 420 | */ |
||
| 421 | public function getControl($var) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Create the form for this object |
||
| 428 | * |
||
| 429 | * @param $form_caption |
||
| 430 | * @param $form_name |
||
| 431 | * @param bool $form_action |
||
| 432 | * @param string $submit_button_caption |
||
| 433 | * @param bool $cancel_js_action |
||
| 434 | * @param bool $captcha |
||
| 435 | * @return \XoopsModules\Smartobject\Form\SmartobjectForm <a href='psi_element://SmartobjectForm'>SmartobjectForm</a> object for this object |
||
| 436 | * object for this object |
||
| 437 | * @see SmartObjectForm::SmartObjectForm() |
||
| 438 | */ |
||
| 439 | public function getForm( |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public function toArray() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * add an error |
||
| 494 | * |
||
| 495 | * @param $err_str |
||
| 496 | * @param bool $prefix |
||
| 497 | * @internal param string $value error to add |
||
| 498 | * @access public |
||
| 499 | */ |
||
| 500 | public function setErrors($err_str, $prefix = false) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param $field |
||
| 516 | * @param bool $required |
||
| 517 | */ |
||
| 518 | public function setFieldAsRequired($field, $required = true) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param $field |
||
| 531 | */ |
||
| 532 | public function setFieldForSorting($field) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @return bool |
||
| 545 | */ |
||
| 546 | public function hasError() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param $url |
||
| 553 | * @param $path |
||
| 554 | */ |
||
| 555 | public function setImageDir($url, $path) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Retreive the group that have been granted access to a specific permission for this object |
||
| 563 | * |
||
| 564 | * @param $group_perm |
||
| 565 | * @return string $group_perm name of the permission |
||
| 566 | */ |
||
| 567 | public function getGroupPerm($group_perm) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param bool $path |
||
| 587 | * @return mixed |
||
| 588 | */ |
||
| 589 | public function getImageDir($path = false) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param bool $path |
||
| 600 | * @return mixed |
||
| 601 | */ |
||
| 602 | public function getUploadDir($path = false) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $key |
||
| 613 | * @param string $info |
||
| 614 | * @return array |
||
| 615 | */ |
||
| 616 | public function getVarInfo($key = '', $info = '') |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get the id of the object |
||
| 629 | * |
||
| 630 | * @return int id of this object |
||
| 631 | */ |
||
| 632 | public function id() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Return the value of the title field of this object |
||
| 639 | * |
||
| 640 | * @param string $format |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public function title($format = 's') |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Return the value of the title field of this object |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public function summary() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Retreive the object admin side link, displayijng a SingleView page |
||
| 664 | * |
||
| 665 | * @param bool $onlyUrl wether or not to return a simple URL or a full <a> link |
||
| 666 | * @return string user side link to the object |
||
| 667 | */ |
||
| 668 | public function getAdminViewItemLink($onlyUrl = false) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Retreive the object user side link |
||
| 677 | * |
||
| 678 | * @param bool $onlyUrl wether or not to return a simple URL or a full <a> link |
||
| 679 | * @return string user side link to the object |
||
| 680 | */ |
||
| 681 | public function getItemLink($onlyUrl = false) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @param bool $onlyUrl |
||
| 690 | * @param bool $withimage |
||
| 691 | * @param bool $userSide |
||
| 692 | * @return string |
||
| 693 | */ |
||
| 694 | public function getEditItemLink($onlyUrl = false, $withimage = true, $userSide = false) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @param bool $onlyUrl |
||
| 703 | * @param bool $withimage |
||
| 704 | * @param bool $userSide |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | public function getDeleteItemLink($onlyUrl = false, $withimage = false, $userSide = false) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | public function getPrintAndMailLink() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @param $sortsel |
||
| 726 | * @return array|bool |
||
| 727 | */ |
||
| 728 | public function getFieldsForSorting($sortsel) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @param $key |
||
| 748 | * @param $newType |
||
| 749 | */ |
||
| 750 | public function setType($key, $newType) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param $key |
||
| 757 | * @param $info |
||
| 758 | * @param $value |
||
| 759 | */ |
||
| 760 | public function setVarInfo($key, $info, $value) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @param $key |
||
| 767 | * @param bool $editor |
||
| 768 | * @return string |
||
| 769 | */ |
||
| 770 | public function getValueFor($key, $editor = true) |
||
| 811 | |||
| 812 | /** |
||
| 813 | * clean values of all variables of the object for storage. |
||
| 814 | * also add slashes whereever needed |
||
| 815 | * |
||
| 816 | * We had to put this method in the SmartObject because the XOBJ_DTYPE_ARRAY does not work properly |
||
| 817 | * at least on PHP 5.1. So we have created a new type XOBJ_DTYPE_SIMPLE_ARRAY to handle 1 level array |
||
| 818 | * as a string separated by | |
||
| 819 | * |
||
| 820 | * @return bool true if successful |
||
| 821 | * @access public |
||
| 822 | */ |
||
| 823 | public function cleanVars() |
||
| 937 | |||
| 938 | /** |
||
| 939 | * returns a specific variable for the object in a proper format |
||
| 940 | * |
||
| 941 | * We had to put this method in the SmartObject because the XOBJ_DTYPE_ARRAY does not work properly |
||
| 942 | * at least on PHP 5.1. So we have created a new type XOBJ_DTYPE_SIMPLE_ARRAY to handle 1 level array |
||
| 943 | * as a string separated by | |
||
| 944 | * |
||
| 945 | * @access public |
||
| 946 | * @param string $key key of the object's variable to be returned |
||
| 947 | * @param string $format format to use for the output |
||
| 948 | * @return mixed formatted value of the variable |
||
| 949 | */ |
||
| 950 | public function getVar($key, $format = 's') |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * @param $key |
||
| 1214 | */ |
||
| 1215 | public function doMakeFieldreadOnly($key) |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * @param $key |
||
| 1225 | */ |
||
| 1226 | public function makeFieldReadOnly($key) |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * @param $key |
||
| 1239 | */ |
||
| 1240 | public function doHideFieldFromForm($key) |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * @param $key |
||
| 1249 | */ |
||
| 1250 | public function doHideFieldFromSingleView($key) |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * @param $key |
||
| 1259 | */ |
||
| 1260 | public function hideFieldFromForm($key) |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * @param $key |
||
| 1273 | */ |
||
| 1274 | public function hideFieldFromSingleView($key) |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * @param $key |
||
| 1287 | */ |
||
| 1288 | public function doShowFieldOnForm($key) |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Display an automatic SingleView of the object, based on the displayOnSingleView param of each vars |
||
| 1297 | * |
||
| 1298 | * @param bool $fetchOnly if set to TRUE, then the content will be return, if set to FALSE, the content will be outputed |
||
| 1299 | * @param bool $userSide for futur use, to do something different on the user side |
||
| 1300 | * @param array $actions |
||
| 1301 | * @param bool $headerAsRow |
||
| 1302 | * @return string content of the template if $fetchOnly or nothing if !$fetchOnly |
||
| 1303 | */ |
||
| 1304 | public function displaySingleObject( |
||
| 1328 | |||
| 1329 | /** |
||
| 1330 | * @param $key |
||
| 1331 | */ |
||
| 1332 | public function doDisplayFieldOnSingleView($key) |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * @param $field |
||
| 1341 | * @param bool $required |
||
| 1342 | */ |
||
| 1343 | public function doSetFieldAsRequired($field, $required = true) |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * @param $field |
||
| 1350 | */ |
||
| 1351 | public function doSetFieldForSorting($field) |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * @param $key |
||
| 1358 | */ |
||
| 1359 | public function showFieldOnForm($key) |
||
| 1369 | |||
| 1370 | /** |
||
| 1371 | * @param $key |
||
| 1372 | */ |
||
| 1373 | public function displayFieldOnSingleView($key) |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * @param $key |
||
| 1386 | */ |
||
| 1387 | public function doSetAdvancedFormFields($key) |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * @param $key |
||
| 1396 | */ |
||
| 1397 | public function setAdvancedFormFields($key) |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * @param $key |
||
| 1410 | * @return mixed |
||
| 1411 | */ |
||
| 1412 | View Code Duplication | public function getUrlLinkObj($key) |
|
| 1422 | |||
| 1423 | /** |
||
| 1424 | * @param $urlLinkObj |
||
| 1425 | * @return mixed |
||
| 1426 | */ |
||
| 1427 | public function &storeUrlLinkObj($urlLinkObj) |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * @param $key |
||
| 1436 | * @return mixed |
||
| 1437 | */ |
||
| 1438 | View Code Duplication | public function getFileObj($key) |
|
| 1448 | |||
| 1449 | /** |
||
| 1450 | * @param $fileObj |
||
| 1451 | * @return mixed |
||
| 1452 | */ |
||
| 1453 | public function &storeFileObj($fileObj) |
||
| 1459 | } |
||
| 1460 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.