I was wondering if the following code could be refactored/improved/simplified? I'm posting this here, as I'd like another programmer's perspective/view; it's always good to see what others would do.
<?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"> => {$announc开发者_StackOverflowement}</option>
<option value="3"> => {$sticky}</option>
<option value="4"> => Move</option>
<option value="5"> => Delete</option>
</select>
</form>
</div>
EOT;
}
}
?>
I'd start by looking for duplication. I see 6 very similar occurrences of a query string:
$topic_query = "UPDATE forum_topics SET status = 'unlocked' WHERE topic_id = '{$topic_id}'";
$topic_query = "UPDATE forum_topics SET status = 'locked' WHERE topic_id = '{$topic_id}'";
$topic_query = "UPDATE forum_topics SET topic_type = 1 WHERE topic_id = '{$topic_id}'";
$topic_query = "UPDATE forum_topics SET topic_type = 2 WHERE topic_id = '{$topic_id}'";
$topic_query = "UPDATE forum_topics SET topic_type = 1 WHERE topic_id = '{$topic_id}'";
$topic_query = "UPDATE forum_topics SET topic_type = 3 WHERE topic_id = '{$topic_id}'";
Each of these is followed by an execution of the query. So how about a method?
function setField($topic_id, $field, $value) {
$topic_query = "UPDATE forum_topics SET '{$field}' = '{$value}' WHERE topic_id = '{$topic_id}'";
doQuery($topic_query);
}
Now the first bit of your switch statement looks like this:
switch ($action) {
case 1:
if ($locked_count > 0) {
setField($topic_id, 'status', 'unlocked');
} else {
setField($topic_id, 'status', 'locked');
}
break;
case 2:
if ($announcement_count > 0) {
setField($topic_id, 'topic_type', 1);
} else {
setField($topic_id, 'topic_type', 2);
}
break;
case 3:
if ($sticky_count > 0) {
setField($topic_id, 'topic_type', 1);
} else {
setField($topic_id, 'topic_type', 3);
}
break;
But I see I've screwed that up - sometimes the field is a string, and sometimes an int - and the int cases are all topic_type. So make it work like this:
switch ($action) {
case 1:
if ($locked_count > 0) {
setField($topic_id, 'status', 'unlocked');
} else {
setField($topic_id, 'status', 'locked');
}
break;
case 2:
if ($announcement_count > 0) {
setTopicType($topic_id, 1);
} else {
setTopicType($topic_id, 2);
}
break;
case 3:
if ($sticky_count > 0) {
setTopicType($topic_id, 1);
} else {
setTopicType($topic_id, 3);
}
break;
Carry on in that vein and you may find that soon you are much happier with the state of your code.
精彩评论