-4
我認爲有在我的代碼邏輯錯誤,因爲有兩個連續的ELSEIF塊枝條几乎相同的代碼:這些if/else塊如何改進?
//first elseif
elseif (!$socialUser && empty($siteUserId)) {
//first time user
$secretWord = $this->RandomString->getRand(7);
$data = array('User'=> array(
'username' => $this->userData['username'],
'password' => $this->Auth->password($secretWord),
'email' => $this->userData['email'],
'name' => $this->userData['name']
));
$siteUserId = $this->_addSiteUser($data);
if ($siteUserId){
$data = array('SocialUser' => array(
'title' => 'facebook',
'identifier' => $this->FB_userId,
'user_id' => $siteUserId
));
if ($this->_addSocialUser($data)){
$this->Auth->login($siteUserId);
$l = $this->Session->read('Auth.redirect');
if (empty($l)) $l = array('controller' => 'qurans', 'action' => 'index');
$this->controller->Session->setFlash(__('You are logined using Facebook Sucessfully!'.$this->userData['name'], true).' '.$secretWord, 'done_msg');
$this->Session->delete('Auth.redirect');
$this->controller->redirect($l);
}
else{
$this->controller->Session->setFlash(__('There is an error during adding you as a social member. Contact admin!',true), 'error_msg');
// $this->controller->redirect($this->Auth->loginAction);
$this->logout();
$this->controller->redirect(array('controller' => 'qurans', 'action' => 'index'));
}
}
}
//second elseif
elseif($socialUser && empty($siteUserId)){
$secretWord = $this->RandomString->getRand(7);
$data = array('User'=> array(
'username' => $this->userData['username'],
'password' => $this->Auth->password($secretWord),
'email' => $this->userData['email'],
'name' => $this->userData['name']
));
$siteUserId = $this->_addSiteUser($data);
if ($siteUserId){
//HERE IS ONLY THE DIFFERENCE
$data = $socialUser;
$data['SocialUser']['user_id'] = $siteUserId;
//DIFFERENCE END HERE
if ($this->_addSocialUser($data)){
$this->Auth->login($siteUserId);
$l = $this->Session->read('Auth.redirect');
if (empty($l)) $l = array('controller' => 'qurans', 'action' => 'index');
$this->controller->Session->setFlash(__('You are logined using Facebook Sucessfully!'.$this->userData['name'], true).' '.$secretWord, 'done_msg');
$this->Session->delete('Auth.redirect');
$this->controller->redirect($l);
}
else{
$this->controller->Session->setFlash(__('There is an error during adding you as a social member. Contact admin!',true), 'error_msg');
// $this->controller->redirect($this->Auth->loginAction);
$this->logout();
$this->controller->redirect(array('controller' => 'qurans', 'action' => 'index'));
}
}
}
我想代碼工作正常,但我不覺得很好用在兩個連續的elseif塊中複製和粘貼代碼塊?有沒有任何想法來改善這個代碼?還是很好?
請至少縮進代碼,這是非常難以閱讀。 – deceze 2013-04-20 11:16:59
@deceze從netbeans IDE格式化的代碼複製並粘貼。最重要的是基本思想本身。即兩個連續的elseif具有幾乎相同的代碼! – SaidbakR 2013-04-20 11:18:58
我不在乎它來自哪裏,它很難閱讀,因此很難給出任何具體的建議。如果你希望人們分析代碼中的錯誤或建議,不要讓他們的頭部不必要地爆炸。 – deceze 2013-04-20 11:22:42