2017-06-03 12 views
0

我正在尝试改进下面的switch语句。这里发生的事情是,基于x个找到的令牌多次调用代码,所以下面的代码每个令牌运行一次。在PHP中改进switch语句或建议替代解决方法

如果找不到$post->ID,则会向该令牌发送通知,并将该ID添加到数据库中。

但是在某些情况下,它可以在检测到大约40%的令牌后停止,这大概是因为找到了ID?由于我在wordpress上,我使用update_option将ID存储在表中,但可能使用其他方法?

$os = $this->os; 
switch ($os) { 

    case "iOS": 
     $iOS_pastPushSavedID = get_option('iOS_pastPushSavedID', $default = false); 
     if($post->ID != $iOS_pastPushSavedID) { 
      update_option('iOS_pastPushSavedID', $post->ID, no); 
      $sendPush = true; 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Android": 
     $android_pastPushSavedID = get_option('android_pastPushSavedID', $default = false); 
     if($post->ID != $android_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $android_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('android_pastPushSavedID', $post->ID, no); 
      $sendPush = true;   
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $android_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Fire OS": 
     $fireos_pastPushSavedID = get_option('fireos_pastPushSavedID', $default = false); 
     if($post->ID != $fireos_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $fireos_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('fireos_pastPushSavedID', $post->ID, no); 
      $sendPush = true;   
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $fireos_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Safari": 
     $safari_pastPushSavedID = get_option('safari_pastPushSavedID', $default = false); 
     if($post->ID != $safari_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $safari_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('safari_pastPushSavedID', $post->ID, no); 
      $sendPush = true; 

     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $safari_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Chrome": 
     $chrome_pastPushSavedID = get_option('chrome_pastPushSavedID', $default = false); 
     if($post->ID != $chrome_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $chrome_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('chrome_pastPushSavedID', $post->ID, no); 
      $sendPush = true;   
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $chrome_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Firefox": 
     $firefox_pastPushSavedID = get_option('firefox_pastPushSavedID', $default = false); 
     if($post->ID != $firefox_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $firefox_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('firefox_pastPushSavedID', $post->ID, no); 
      $sendPush = true; 

     } else { 
     //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $firefox_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    default: 
     $sendPush = false; 
} 

回答

1

除非我误解你的过程,这是一个非常干燥的/简明的方式做到没有详细的switch/case块:

$os_opts=[ 
    'iOS'=>'iOS', 
    'Android'=>'android', 
    'Fire OS'=>'fireos', 
    'Safari'=>'safari', 
    'Chrome'=>'chrome', 
    'Firefox'=>'firefox' 
]; 

$os=$this->os; 
$sendPush=false; 
if(isset($os_opts[$os])){       // deny empty and invalid options 
    $os_opt="{$os_opts[$os]}_pastPushSavedID";  // build string for next two functions 
    if($post->ID!=get_option($os_opt,$default=false)){ 
     update_option($os_opt,$post->ID,no); 
     $sendPush = true; 
    } 
} 

$os_opts阵列具有匹配$os的密钥,以及与get_option() & update_option()一起使用的值。这将大大减少代码长度,并使未来的修改非常容易。

由于get_option()结果只使用一次,所以将其声明为变量没有意义;只要在if条件下使用它。

get_option()update_option()的第一个参数始终以相同的子字符串结尾。我有意义将$os_opts[$os]的值作为前缀并将其声明为变量。变量声明不是必需但我的个人规则是;如果您打算多次使用数据,请使用变量,如果只使用一次,则不要声明它。

1

你可以这样做。你可以像这样缩短你的代码。

$optionName='';//added some default values 
$sendPush = false;;//added some default values 
switch ($os) { 

    case "iOS": 
     $optionName='iOS_pastPushSavedID'; 
    break; 

    case "Android": 
     $optionName='android_pastPushSavedID'; 
    break; 

    case "Fire OS": 
     $optionName='fireos_pastPushSavedID'; 
    break; 

    case "Safari": 
     $optionName='safari_pastPushSavedID'; 
    break; 

    case "Chrome": 
     $optionName='chrome_pastPushSavedID'; 
    break; 

    case "Firefox": 
     $optionName='firefox_pastPushSavedID'; 
    break; 

    default: 
     $sendPush = false; 
} 
//this is operation which is common when $optionName is not empty. 
if(!empty($optionName)) 
{ 
    $optionData = get_option($optionName, $default = false); 
    if($post->ID != $optionData) { 
     update_option($optionData, $post->ID, no); 
     $sendPush = true; 
    } else { 
     $sendPush = false; 
    } 
} 
+0

谢谢!将检查出来并恢复。感谢您的快速回复。你会建议一个更快的替代方法来将var存储在MySQL Wordpress中吗?或者我的解决方案好吗? Tx – Jason

+0

@Jason欢迎...我认为它是好的,这是你在这里试图做的事情的最短代码。我的代码,我给你一种方式,你可以防止自己重写相同的代码.. :) –

+0

谢谢!明天会试试这个,并会让你知道! – Jason

1

编号写更多像这样

function getOptionSpecifier() { 

    switch ($this->os) { 
     case "iOS": 
      return 'iOS_pastPushSavedID'; 
     case "Android": 
      return 'android_pastPushSavedID'; 
     case "Android": 
      return 'android_pastPushSavedID'; 
     case "Fire OS": 
      return 'fireos_pastPushSavedID'; 
     case "Safari": 
      return 'safari_pastPushSavedID'; 
     case "Chrome": 
      return 'chrome_pastPushSavedID'; 
     case "Firefox": 
      return 'firefox_pastPushSavedID'; 
     default: 
      return ''; 

    } 
} 

function send_notification($id) { 
    $optionSpecifier = getOptionSpecifier(); 

    if ($optionSpecifier === NULL) { 
     return false; 
    } 

    $pastPushSavedID = get_option($optionSpecifier, $default = false); 

    if($id != $pastPushSavedID) { 
     update_option($optionSpecifier, $id, no); 
     return true; 
     //$title = ($os . '_New Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
    } else { 
     //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
     return false; 
    } 
} 

$sendPush = send_notification($post->ID); 

ALA“关注点分离”多种功能等等......