should this method be decomposed into separate methods?

121 Views Asked by At

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;
}
3

There are 3 best solutions below

0
On BEST ANSWER

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];
...
1
On

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.

1
On

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.