这个方法应该分解成单独的方法吗?

发布于 2024-10-19 04:09:18 字数 2914 浏览 5 评论 0原文

此方法采用搜索搜索关键字和解析的 mysql 查询,并重写 where 表达式以包含 LIKE %keyword%。

它工作得很好,但我不知道有这么多循环的方法是好还是坏的做法......

private function build_where($query_array, $options)  
{  
    //add WHERE starting point  
    $where = '';        

    if(!empty($query_array['WHERE']))
    {
        //build where array
        $where_array = $query_array['WHERE'];   

        //start the where
        $where .= 'WHERE ';

        //get columns array
        $columns_array = $this->build_columns_array($query_array);

        //if there is a search string           
        if(!empty($options['sSearch']))
        {
            //check for enabled columns
            $i = 0;
            $columns_length = count($columns_array);
            for($i; $i < intval($columns_length); $i++)
            {
                //create the options boolean array
                $searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i];
            }

            //loop through searchable_columns for true values
            foreach($searchable_columns as $searchable_column_key => $searchable_column_val)
            {
                if($searchable_column_val == true)
                {
                    //get an integer from the searchable_column key
                    $column_id = preg_replace("/[^0-9]/", '', $searchable_column_key);

                    //lookup column name by index
                    foreach($columns_array as $columns_array_key => $columns_array_val)
                    {
                        //if the $columns_array_key matches the $column_id
                        if($columns_array_key == $column_id)
                        {
                            //loop to build where foreach base expression
                            $i = 0;
                            $where_length = count($where_array);
                            for($i; $i < intval($where_length); $i++)
                            {
                                //append the existing WHERE Expressions
                                $where .= $where_array[$i]['base_expr'];
                            }                               

                            //append the LIKE '%$options['sSearch'])%'
                            $where .= ' AND '.$columns_array_val." LIKE '%".$options['sSearch']."%' OR ";
                        }
                    }   
                }
            }
            //remove the last OR
            $where = substr_replace($where, "", -3);                                    
        }
        else
        {
            //loop to build where
            $i = 0;
            $where_length = count($where_array);
            for($i; $i < intval($where_length); $i++)
            {
                $where .= $where_array[$i]['base_expr'];
            } 
        }            
    }

    //print_r($where_length);
    return $where;
}

This method takes search a search keyword and parsed mysql query, and rewrites the where expression to include LIKE %keyword%.

It works well, but I dont know if its good or bad practice to have a method with this many loops...

private function build_where($query_array, $options)  
{  
    //add WHERE starting point  
    $where = '';        

    if(!empty($query_array['WHERE']))
    {
        //build where array
        $where_array = $query_array['WHERE'];   

        //start the where
        $where .= 'WHERE ';

        //get columns array
        $columns_array = $this->build_columns_array($query_array);

        //if there is a search string           
        if(!empty($options['sSearch']))
        {
            //check for enabled columns
            $i = 0;
            $columns_length = count($columns_array);
            for($i; $i < intval($columns_length); $i++)
            {
                //create the options boolean array
                $searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i];
            }

            //loop through searchable_columns for true values
            foreach($searchable_columns as $searchable_column_key => $searchable_column_val)
            {
                if($searchable_column_val == true)
                {
                    //get an integer from the searchable_column key
                    $column_id = preg_replace("/[^0-9]/", '', $searchable_column_key);

                    //lookup column name by index
                    foreach($columns_array as $columns_array_key => $columns_array_val)
                    {
                        //if the $columns_array_key matches the $column_id
                        if($columns_array_key == $column_id)
                        {
                            //loop to build where foreach base expression
                            $i = 0;
                            $where_length = count($where_array);
                            for($i; $i < intval($where_length); $i++)
                            {
                                //append the existing WHERE Expressions
                                $where .= $where_array[$i]['base_expr'];
                            }                               

                            //append the LIKE '%$options['sSearch'])%'
                            $where .= ' AND '.$columns_array_val." LIKE '%".$options['sSearch']."%' OR ";
                        }
                    }   
                }
            }
            //remove the last OR
            $where = substr_replace($where, "", -3);                                    
        }
        else
        {
            //loop to build where
            $i = 0;
            $where_length = count($where_array);
            for($i; $i < intval($where_length); $i++)
            {
                $where .= $where_array[$i]['base_expr'];
            } 
        }            
    }

    //print_r($where_length);
    return $where;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

土豪我们做朋友吧 2024-10-26 04:09:18

Kent Beck 或 Martin Fowler 的思想流派实际上会建议您将这个大方法重构为许多小方法。在我看来,它不容易阅读,这将是重构的主要原因。

The school of thought of Kent Beck or Martin Fowler would actually advise you to refactor this large methods down to many small methods. It's not easily read in my opinion, which would be the main reason to refactor.

不再见 2024-10-26 04:09:18

分解方法主要不是为了重用。这样做可以使代码更易于阅读、测试和维护。清晰的方法名称也可以替代内联注释。此方法执行两个可以分开的高级操作:构建带选项和不带选项的 where 子句。对我来说另一个提示是,使用选项构建 where 子句的逻辑看起来足够丰富,足以保证其自己的方法。

private function build_where($query_array, $options) {
    if(!empty($query_array['WHERE'])) {
        $where_array = $query_array['WHERE'];
        $columns_array = $this->build_columns_array($query_array);
        if (empty($options['sSearch'])) {
            return $this->build_where_with_options($where_array, $columns_array, $options);
        }
        else {
            return $this->build_where_without_options($where_array, $columns_array);
        }
    }
    else {
        return '';
    }
}

现在,您可以快速扫描 build_where() 以查看 where 子句可能采用三种可能的形式,以及每种形式需要何时与输入一起生成其结果。

以下是您可以在整个代码中进行的一些小改进:

  • count() 返回一个整数,并且不需要在 forintval()代码>循环。即使您将它们留在里面,最好在循环外部应用调用,这样它只执行一次,因为每次都会产生相同的值。
  • if($searchable_column_val == true) 相当于 if($searchable_column_val) 因为两者都将 $searchable_column_val 转换为布尔值,并且后者传递当该转换的布尔值等于true时。
  • $where = substr_replace($where, "", -3) 可以替换为 $where = substr($where, 0, -3) 更清晰一点。
  • 您可以通过简单地获取带有该键的值来利用 PHP 数组,而不是循环遍历数组来查找特定键。

对于最后一个,此代码

foreach($columns_array as $columns_array_key => $columns_array_val)
{
    //if the $columns_array_key matches the $column_id
    if($columns_array_key == $column_id)
    { ... }
}

可以替换为此

$columns_array_val = $columns_array[$column_id];
...

Breaking up methods is not primarily about reuse. Doing so can make code easier to read, test, and maintain. Clear method names can also substitute for inline comments. This method does two high-level things which could be separated: building a where clause with options and without options. Another hint for me is that the logic that builds the where clause with options looks meaty enough to warrant its own method.

private function build_where($query_array, $options) {
    if(!empty($query_array['WHERE'])) {
        $where_array = $query_array['WHERE'];
        $columns_array = $this->build_columns_array($query_array);
        if (empty($options['sSearch'])) {
            return $this->build_where_with_options($where_array, $columns_array, $options);
        }
        else {
            return $this->build_where_without_options($where_array, $columns_array);
        }
    }
    else {
        return '';
    }
}

Now you can quickly scan build_where() to see that there are three possible forms the where clause may take and when along with the input each form needs to produce its result.

Here are some minor improvements you can make throughout your code:

  • count() returns an integer and doesn't need the intval() calls in your for loops. Even if you left those in, it would be better to apply the call outside the loop so its done only once as it yields the same value each time.
  • if($searchable_column_val == true) is equivalent to if($searchable_column_val) since both cast $searchable_column_val to a boolean value and the latter passes when that casted boolean value equals true.
  • $where = substr_replace($where, "", -3) can be replace with $where = substr($where, 0, -3) and is a little clearer.
  • Instead of looping through an array looking for a specific key you can take advantage of PHP's arrays by simply grabbing the value with that key.

For the last one, this code

foreach($columns_array as $columns_array_key => $columns_array_val)
{
    //if the $columns_array_key matches the $column_id
    if($columns_array_key == $column_id)
    { ... }
}

can be replaced by this

$columns_array_val = $columns_array[$column_id];
...
痴情 2024-10-26 04:09:18

确实是个人喜好。一些程序员会将其分成几个函数。就我个人而言,我认为你的方式很好。如果我看到一些我认为可以重用的东西,我会将其重构为可以包含的单独文件。

在我看来,一些程序员甚至在拥有可重用的东西之前就太快地让东西“可重用”。

Personal preference really. Some programmers would chop this up into several functions. Personally, I think it's fine the way you have it. If I saw something that I thought might be reusable, I'd refactor it out into a separate file that could be included.

In my opinion, some programmers are too quick to make things "reuesable" before they even have something to reuse it with.

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