0
我想知道下面的代碼是否可以重構/改進/簡化?我在這裏發佈這個,因爲我想要另一個程序員的視角/視圖;看到別人會做什麼總是很好的。這個PHP代碼可以被簡化或改進嗎?
<?php
function moderateTopic($topic_id, $action = NULL) {
$locked_query = "SELECT topic_id FROM forum_topics WHERE status = 'locked' AND topic_id = '{$topic_id}'";
$locked_count = totalResults($locked_query);
$announcement_query = "SELECT topic_id FROM forum_topics WHERE topic_type = 2 AND topic_id = '{$topic_id}'";
$announcement_count = totalResults($announcement_query);
$sticky_query = "SELECT topic_id FROM forum_topics WHERE topic_type = 3 AND topic_id = '{$topic_id}'";
$sticky_count = totalResults($sticky_query);
if (is_null($action) == FALSE) {
switch ($action) {
case 1:
if ($locked_count > 0) {
$topic_query = "UPDATE forum_topics SET status = 'unlocked' WHERE topic_id = '{$topic_id}'";
} else {
$topic_query = "UPDATE forum_topics SET status = 'locked' WHERE topic_id = '{$topic_id}'";
}
doQuery($topic_query);
break;
case 2:
if ($announcement_count > 0) {
$topic_query = "UPDATE forum_topics SET topic_type = 1 WHERE topic_id = '{$topic_id}'";
} else {
$topic_query = "UPDATE forum_topics SET topic_type = 2 WHERE topic_id = '{$topic_id}'";
}
doQuery($topic_query);
break;
case 3:
if ($sticky_count > 0) {
$topic_query = "UPDATE forum_topics SET topic_type = 1 WHERE topic_id = '{$topic_id}'";
} else {
$topic_query = "UPDATE forum_topics SET topic_type = 3 WHERE topic_id = '{$topic_id}'";
}
doQuery($topic_query);
break;
case 4:
header('Location: ' . urlify(9, $topic_id));
break;
case 5:
header('Location: ' . urlify(11, $topic_id));
break;
}
header('Location: ' . urlify(2, $topic_id));
} else {
$locked = $locked_count > 0 ? 'Unlock' : 'Lock';
$announcement = $announcement_count > 0 ? 'Unannounce' : 'Announce';
$sticky = $sticky_count > 0 ? 'Unsticky' : 'Sticky';
return <<<EOT
<div style="float: right;">
<form method="POST">
<select name="action" onChange="document.forms[0].submit();">
<option value="">- - Moderate - -</option>
<option value="1"> => {$locked}</option>
<option value="2"> => {$announcement}</option>
<option value="3"> => {$sticky}</option>
<option value="4"> => Move</option>
<option value="5"> => Delete</option>
</select>
</form>
</div>
EOT;
}
}
?>
http://refactormycode.com/是這類問題的更好選擇。 – 2010-12-18 17:37:21
@從此處鏈接到的這個不良網站無法更好地編寫任何代碼。最壞的,最有可能的 – 2010-12-18 17:40:52
@Col。哈哈,我承認我沒有用過它。但是Stackoverflow引擎並不真正適用於代碼重構。也許有更好的選擇。 – 2010-12-18 17:46:47