这段 PHP 代码可以简化或改进吗?

发布于 2024-10-08 17:46:20 字数 2982 浏览 1 评论 0原文

我想知道以下代码是否可以重构/改进/简化?我将其发布在这里,因为我想要另一个程序员的观点/观点;看到别人会做什么总是好的。

<?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"> =&gt; {$locked}</option>
<option value="2"> =&gt; {$announcement}</option>
<option value="3"> =&gt; {$sticky}</option>
<option value="4"> =&gt; Move</option>
<option value="5"> =&gt; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-10-15 17:46:20

我首先要寻找重复的内容。我看到查询字符串出现 6 次非常相似:

$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}'";

每个字符串后面都执行一次查询。那么有什么方法呢?

function setField($topic_id, $field, $value) {
    $topic_query = "UPDATE forum_topics SET '{$field}' = '{$value}' WHERE topic_id = '{$topic_id}'";
    doQuery($topic_query);
}

现在你的 switch 语句的第一部分看起来像这样:

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;

但我发现我把它搞砸了 - 有时字段是字符串,有时是 int - 并且 int 情况都是 topic_type。因此,让它像这样工作:

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;

继续这样做,您可能会发现很快您对代码的状态感到更加满意。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文