如何改进这些if / else块?

时间:2013-04-20 11:15:10

标签: php algorithm logic

我认为我的代码逻辑存在错误,因为有两个连续的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块中的复制和粘贴代码块感觉不舒服?有什么想法来改进这段代码吗?还是没事?

2 个答案:

答案 0 :(得分:2)

嵌套您的ifs

...
elseif (empty($siteUserId)) {
    // common code
    if ($socialUser) {
        // social-user specific code
    } else {
        // non-social-user specific code
    }
    // more common code
}
...

答案 1 :(得分:1)

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

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

通过这种方式,您可以将empty($siteUserId) true中常见的代码与$socialUser分开,并根据{{1}}的布尔值区分其余代码。