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 boardController 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 boardController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class boardController extends board |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @brief initialization |
||
| 15 | **/ |
||
| 16 | function init() |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @brief insert document |
||
| 22 | **/ |
||
| 23 | function procBoardInsertDocument() |
||
| 24 | { |
||
| 25 | // check grant |
||
| 26 | if($this->module_info->module != "board") |
||
|
|
|||
| 27 | { |
||
| 28 | return new Object(-1, "msg_invalid_request"); |
||
| 29 | } |
||
| 30 | if(!$this->grant->write_document) |
||
| 31 | { |
||
| 32 | return new Object(-1, 'msg_not_permitted'); |
||
| 33 | } |
||
| 34 | $logged_info = Context::get('logged_info'); |
||
| 35 | |||
| 36 | // setup variables |
||
| 37 | $obj = Context::getRequestVars(); |
||
| 38 | $obj->module_srl = $this->module_srl; |
||
| 39 | if($obj->is_notice!='Y'||!$this->grant->manager) $obj->is_notice = 'N'; |
||
| 40 | $obj->commentStatus = $obj->comment_status; |
||
| 41 | |||
| 42 | settype($obj->title, "string"); |
||
| 43 | View Code Duplication | if($obj->title == '') $obj->title = cut_str(trim(strip_tags(nl2br($obj->content))),20,'...'); |
|
| 44 | //setup dpcument title tp 'Untitled' |
||
| 45 | if($obj->title == '') $obj->title = 'Untitled'; |
||
| 46 | |||
| 47 | // unset document style if the user is not the document manager |
||
| 48 | if(!$this->grant->manager) |
||
| 49 | { |
||
| 50 | unset($obj->title_color); |
||
| 51 | unset($obj->title_bold); |
||
| 52 | } |
||
| 53 | |||
| 54 | // generate document module model object |
||
| 55 | $oDocumentModel = getModel('document'); |
||
| 56 | |||
| 57 | // generate document module의 controller object |
||
| 58 | $oDocumentController = getController('document'); |
||
| 59 | |||
| 60 | // check if the document is existed |
||
| 61 | $oDocument = $oDocumentModel->getDocument($obj->document_srl, $this->grant->manager); |
||
| 62 | |||
| 63 | // update the document if it is existed |
||
| 64 | $is_update = false; |
||
| 65 | if($oDocument->isExists() && $oDocument->document_srl == $obj->document_srl) |
||
| 66 | { |
||
| 67 | $is_update = true; |
||
| 68 | } |
||
| 69 | |||
| 70 | // if use anonymous is true |
||
| 71 | if($this->module_info->use_anonymous == 'Y') |
||
| 72 | { |
||
| 73 | $this->module_info->admin_mail = ''; |
||
| 74 | $obj->notify_message = 'N'; |
||
| 75 | if($is_update===false) |
||
| 76 | { |
||
| 77 | $obj->member_srl = -1*$logged_info->member_srl; |
||
| 78 | } |
||
| 79 | $obj->email_address = $obj->homepage = $obj->user_id = ''; |
||
| 80 | $obj->user_name = $obj->nick_name = 'anonymous'; |
||
| 81 | $bAnonymous = true; |
||
| 82 | if($is_update===false) |
||
| 83 | { |
||
| 84 | $oDocument->add('member_srl', $obj->member_srl); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | else |
||
| 88 | { |
||
| 89 | $bAnonymous = false; |
||
| 90 | } |
||
| 91 | |||
| 92 | if($obj->is_secret == 'Y' || strtoupper($obj->status == 'SECRET')) |
||
| 93 | { |
||
| 94 | $use_status = explode('|@|', $this->module_info->use_status); |
||
| 95 | if(!is_array($use_status) || !in_array('SECRET', $use_status)) |
||
| 96 | { |
||
| 97 | unset($obj->is_secret); |
||
| 98 | $obj->status = 'PUBLIC'; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // update the document if it is existed |
||
| 103 | if($is_update) |
||
| 104 | { |
||
| 105 | if(!$oDocument->isGranted()) |
||
| 106 | { |
||
| 107 | return new Object(-1,'msg_not_permitted'); |
||
| 108 | } |
||
| 109 | |||
| 110 | if($this->module_info->use_anonymous == 'Y') { |
||
| 111 | $obj->member_srl = abs($oDocument->get('member_srl')) * -1; |
||
| 112 | $oDocument->add('member_srl', $obj->member_srl); |
||
| 113 | } |
||
| 114 | |||
| 115 | View Code Duplication | if($this->module_info->protect_content=="Y" && $oDocument->get('comment_count')>0 && $this->grant->manager==false) |
|
| 116 | { |
||
| 117 | return new Object(-1,'msg_protect_content'); |
||
| 118 | } |
||
| 119 | |||
| 120 | if(!$this->grant->manager) |
||
| 121 | { |
||
| 122 | // notice & document style same as before if not manager |
||
| 123 | $obj->is_notice = $oDocument->get('is_notice'); |
||
| 124 | $obj->title_color = $oDocument->get('title_color'); |
||
| 125 | $obj->title_bold = $oDocument->get('title_bold'); |
||
| 126 | } |
||
| 127 | |||
| 128 | // modify list_order if document status is temp |
||
| 129 | if($oDocument->get('status') == 'TEMP') |
||
| 130 | { |
||
| 131 | $obj->last_update = $obj->regdate = date('YmdHis'); |
||
| 132 | $obj->update_order = $obj->list_order = (getNextSequence() * -1); |
||
| 133 | } |
||
| 134 | |||
| 135 | $output = $oDocumentController->updateDocument($oDocument, $obj, true); |
||
| 136 | $msg_code = 'success_updated'; |
||
| 137 | |||
| 138 | // insert a new document otherwise |
||
| 139 | } else { |
||
| 140 | $output = $oDocumentController->insertDocument($obj, $bAnonymous); |
||
| 141 | $msg_code = 'success_registed'; |
||
| 142 | $obj->document_srl = $output->get('document_srl'); |
||
| 143 | |||
| 144 | // send an email to admin user |
||
| 145 | if($output->toBool() && $this->module_info->admin_mail) |
||
| 146 | { |
||
| 147 | $oMail = new Mail(); |
||
| 148 | $oMail->setTitle($obj->title); |
||
| 149 | $oMail->setContent( sprintf("From : <a href=\"%s\">%s</a><br/>\r\n%s", getFullUrl('','document_srl',$obj->document_srl), getFullUrl('','document_srl',$obj->document_srl), $obj->content)); |
||
| 150 | $oMail->setSender($obj->user_name, $obj->email_address); |
||
| 151 | |||
| 152 | $target_mail = explode(',',$this->module_info->admin_mail); |
||
| 153 | View Code Duplication | for($i=0;$i<count($target_mail);$i++) |
|
| 154 | { |
||
| 155 | $email_address = trim($target_mail[$i]); |
||
| 156 | if(!$email_address) continue; |
||
| 157 | $oMail->setReceiptor($email_address, $email_address); |
||
| 158 | $oMail->send(); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | // if there is an error |
||
| 164 | if(!$output->toBool()) |
||
| 165 | { |
||
| 166 | return $output; |
||
| 167 | } |
||
| 168 | |||
| 169 | // return the results |
||
| 170 | $this->add('mid', Context::get('mid')); |
||
| 171 | $this->add('document_srl', $output->get('document_srl')); |
||
| 172 | |||
| 173 | // alert a message |
||
| 174 | if(Context::get('xeVirtualRequestMethod') !== 'xml') |
||
| 175 | { |
||
| 176 | $this->setMessage($msg_code); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @brief delete the document |
||
| 182 | **/ |
||
| 183 | function procBoardDeleteDocument() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @brief vote |
||
| 224 | **/ |
||
| 225 | function procBoardVoteDocument() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @brief insert comments |
||
| 236 | **/ |
||
| 237 | function procBoardInsertComment() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @brief delete the comment |
||
| 352 | **/ |
||
| 353 | View Code Duplication | function procBoardDeleteComment() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @brief delete the tracjback |
||
| 382 | **/ |
||
| 383 | View Code Duplication | function procBoardDeleteTrackback() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @brief check the password for document and comment |
||
| 409 | **/ |
||
| 410 | function procBoardVerificationPassword() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @brief the trigger for displaying 'view document' link when click the user ID |
||
| 458 | **/ |
||
| 459 | function triggerMemberMenu(&$obj) |
||
| 502 | } |
||
| 503 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.