这个方法应该分解成单独的方法吗?
此方法采用搜索搜索关键字和解析的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
分解方法主要不是为了重用。这样做可以使代码更易于阅读、测试和维护。清晰的方法名称也可以替代内联注释。此方法执行两个可以分开的高级操作:构建带选项和不带选项的 where 子句。对我来说另一个提示是,使用选项构建 where 子句的逻辑看起来足够丰富,足以保证其自己的方法。
现在,您可以快速扫描
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)
更清晰一点。对于最后一个,此代码
可以替换为此
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.
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 theintval()
calls in yourfor
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 toif($searchable_column_val)
since both cast$searchable_column_val
to a boolean value and the latter passes when that casted boolean value equalstrue
.$where = substr_replace($where, "", -3)
can be replace with$where = substr($where, 0, -3)
and is a little clearer.For the last one, this code
can be replaced by this
确实是个人喜好。一些程序员会将其分成几个函数。就我个人而言,我认为你的方式很好。如果我看到一些我认为可以重用的东西,我会将其重构为可以包含的单独文件。
在我看来,一些程序员甚至在拥有可重用的东西之前就太快地让东西“可重用”。
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.