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

Pet*_*ter 2 php decomposition

此方法搜索搜索关键字并解析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;
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 5

Kent Beck或Martin Fowler的思想学派实际上会建议你将这些大型方法重构为许多小方法.在我看来,这并不容易阅读,这将是重构的主要原因.