【发布时间】:2013-04-20 11:15:09
【问题描述】:
我认为我的代码逻辑有错误,因为有两个连续的 elseif 块具有几乎相同的代码:
//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 它是从 netbeans IDE 格式的代码复制和粘贴。最重要的是基本思想本身。即两个连续的elseif几乎相同的代码!
-
我不在乎它来自哪里,它很难阅读,因此很难给出任何具体的建议。如果您希望人们分析您的代码中的错误或建议,请不要让他们不必要地爆炸。
-
是的,这很难读。我通常不会对此投反对票,但如果有人可能会帮助你说“请缩进代码”,如果我需要帮助,我会这样做。