我正在尝试改进下面的switch语句。这里发生的是基于找到的x个令牌多次调用代码,因此下面的代码每个令牌运行一次。
如果找不到$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;
}
答案 0 :(得分: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;
}
}
答案 1 :(得分:1)
Id写得更像这样
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“关注点分离”等......
答案 2 :(得分: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]
值并将其声明为变量。变量声明不是必要的,但我的个人规则是;如果您打算多次使用数据,请使用变量,如果只有一次没有声明它。