php - 我应该重构这段代码吗?

标签 php refactoring kohana

该代码用于查看辩论页面。该代码应该确定是否向查看用户显示添加回复表单。

如果用户已登录,并且该用户不是辩论的创建者,则检查该用户是否已经回复了辩论。

如果用户尚未回复辩论,则显示表单... 否则,检查用户是否想通过在 url 中查找回复 ID 来编辑其现有回复

如果这些测试中的任何一个没有通过,那么我将原因保存为 int 并将其传递给 View 中的 switch 语句。

逻辑看起来很简单,但我的代码似乎有点草率。

这是代码..(使用 Kohana V2.3.4)

public function view($id = 0)
{
    $debate = ORM::factory('debate')->with('user')->with('category')->find($id);

    if ($debate->loaded == FALSE)
    {
        url::redirect();
    }

    // series of tests to show an add reply form
    if ($this->logged_in)
    {
        // is the viewer the creator?
        if ($this->user->id != $debate->user->id)
        {
            // has the user already replied?
            if (ORM::factory('reply')
                ->where(array('debate_id' => $id, 'user_id' => $this->user->id))
                ->count_all() == 0)
            {
                $form = $errors = array
                (
                    'body'      => '',
                    'choice_id' => '',
                    'add'       => ''
                );

                if ($post = $this->input->post())
                {
                    $reply = ORM::factory('reply');

                    // validate and insert the reply
                    if ($reply->add($post, TRUE))
                    {
                        url::redirect(url::current());
                    }

                    $form   = arr::overwrite($form,   $post->as_array());
                    $errors = arr::overwrite($errors, $post->errors('reply_errors'));
                }
            }
            // editing a reply?
            else if (($rid = (int) $this->input->get('edit'))
                    AND ($reply = ORM::factory('reply')
                                ->where(array('debate_id' => $id, 'user_id' => $this->user->id))
                                ->find($rid)))
            {
                $form = $errors = array
                (
                    'body'      => '',
                    'choice_id' => '',
                    'add'       => ''
                );

                // autocomplete the form
                $form = arr::overwrite($form, $reply->as_array());

                if ($post = $this->input->post())
                {
                    // validate and insert the reply
                    if ($reply->edit($post, TRUE))
                    {
                        url::redirect(url::current());
                    }

                    $form   = arr::overwrite($form,   $post->as_array());
                    $errors = arr::overwrite($errors, $post->errors('reply_errors'));
                }
            }
            else
            {
                // user already replied
                $reason = 3;
            }
        }
        else
        {
            // user started the debate
            $reason = 2;
        }
    }
    else
    {
        // user is not logged in.
        $reason = 1;
    }

    $limits = Kohana::config('app/debate.limits');
    $page   = (int) $this->input->get('page', 1);
    $offset = ($page > 0) ? ($page - 1) * $limits['replies'] : 0;

    $replies = ORM::factory('reply')->with('user')->with('choice')->where('replies.debate_id', $id);

    $this->template->title  = $debate->topic;
    $this->template->debate = $debate;
    $this->template->body   = View::factory('debate/view')
        ->set('debate',     $debate)
        ->set('replies',    $replies->find_all($limits['replies'], $offset))
        ->set('pagination', Pagination::factory(array
            (
                'style'          => 'digg',
                'items_per_page' => $limits['replies'],
                'query_string'   => 'page',
                'auto_hide'      => TRUE,
                'total_items'    => $total = $replies->count_last_query()
            ))
        )
        ->set('total', $total);

    // are we showing the add reply form?
    if (isset($form, $errors))
    {
        $this->template->body->add_reply_form = View::factory('reply/add_reply_form')
            ->set('debate', $debate)
            ->set('form',   $form)
            ->set('errors', $errors);
    }
    else
    {
        $this->template->body->reason = $reason;
    }
}

这是 View ,这里有一些逻辑决定向用户显示什么消息。

<!-- Add Reply Form -->
<?php if (isset($add_reply_form)): ?>

    <?php echo $add_reply_form; ?>

<?php else: ?>

    <?php
        switch ($reason)
        {
            case 1 :
                // not logged in, show a message
                $message = 'Add your ' . html::anchor('login?url=' . url::current(TRUE), '<b>vote</b>') . ' to this discussion';
                break;

            case 2 :
                // started the debate. dont show a message for that.
                $message = NULL;
                break;

            case 3:
                // already replied, show a message
                $message = 'You have already replied to this debate';
                break;

            default:
                // unknown reason. dont show a message
                $message = NULL;
                break;
        }
    ?>

    <?php echo app::show_message($message, 'h2'); ?>

<?php endif; ?>
<!-- End Add Reply Form -->

我是否应该将添加回复逻辑重构为另一个函数或其他东西......这一切都有效,只是看起来很草率。

谢谢

编辑:我考虑了所有答案。由于我目前没有添加任何新内容并且有时间消磨,所以我选择重构代码。经过一番思考,我想到了一个更好的解决方案。整个过程花了我大约30分钟,我想说这是值得的。感谢大家的回答

最佳答案

是的,重构。删除 PHP 并使用真正的语言。 ;)

认真地说,请进行重构 - 避免如此深入地嵌套 if 语句(这样做会混淆逻辑并使测试变得更加困难)并将整体部分分块到单独的函数/方法中。

关于php - 我应该重构这段代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2633528/

相关文章:

c++ - 语法繁重的多个 lambda 包装器的替代方案——如何避免样板代码?

Kohana 查询生成器 : and or where clause

php - 我想使用 PHP 和 MySQL 对我选择的数据进行过滤

php - Codeigniter 对象未找到错误

php - 转义数组值中的引号或特殊字符

java - 如何避免将一个变量从一种方法传递到另一种方法

php - 使用 Total For Sum 函数排序

java - Eclipse/Java 中的重构 : Apply "Extract Method" (Alt+Shift+M) afterwards

php - MVC 项目的正确设计

javascript - 如何解决访问控制允许来源问题?