这段 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;
}
}
?>
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"> => {$announcement}</option>
<option value="3"> => {$sticky}</option>
<option value="4"> => Move</option>
<option value="5"> => Delete</option>
</select>
</form>
</div>
EOT;
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先要寻找重复的内容。我看到查询字符串出现 6 次非常相似:
每个字符串后面都执行一次查询。那么有什么方法呢?
现在你的 switch 语句的第一部分看起来像这样:
但我发现我把它搞砸了 - 有时字段是字符串,有时是 int - 并且 int 情况都是 topic_type。因此,让它像这样工作:
继续这样做,您可能会发现很快您对代码的状态感到更加满意。
I'd start by looking for duplication. I see 6 very similar occurrences of a query string:
Each of these is followed by an execution of the query. So how about a method?
Now the first bit of your switch statement looks like this:
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:
Carry on in that vein and you may find that soon you are much happier with the state of your code.