-4

我认为我的代码逻辑有错误,因为有两个连续的 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 块之间的复制和粘贴代码块感觉不好?有什么想法可以改进此代码吗?还是很好?

4

2 回答 2

2

嵌套你的ifs

...
elseif (empty($siteUserId)) {
    // common code
    if ($socialUser) {
        // social-user specific code
    } else {
        // non-social-user specific code
    }
    // more common code
}
...
于 2013-04-20T11:18:26.370 回答
1

您应该创建另一个嵌套if-else

elseif (empty($siteUserId)) {
    ...
    if ($socialUser) {
        ...
    } else {
        ...
    }
    ...
}

这样,您可以分离常见的代码empty($siteUserId)iftrue并根据 的布尔值区分其余代码$socialUser

于 2013-04-20T11:20:20.320 回答