Conditions | 10 |
Paths | 32 |
Total Lines | 34 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
16 | public function scopeLike($query, $column, $value, $side='both', $isNotLike=false, $isAnd=true) |
||
17 | { |
||
18 | $operator=$isNotLike ? 'not like' : 'like'; |
||
19 | |||
20 | $escape_like_str=function($str) { |
||
21 | $like_escape_char='!'; |
||
22 | |||
23 | return str_replace([$like_escape_char, '%', '_'], [ |
||
24 | $like_escape_char.$like_escape_char, |
||
25 | $like_escape_char.'%', |
||
26 | $like_escape_char.'_', |
||
27 | ], $str); |
||
28 | }; |
||
29 | |||
30 | switch ($side) { |
||
31 | case 'none': |
||
32 | $value=$escape_like_str($value); |
||
33 | break; |
||
34 | case 'before': |
||
35 | case 'left': |
||
36 | $value="%{$escape_like_str($value)}"; |
||
37 | break; |
||
38 | case 'after': |
||
39 | case 'right': |
||
40 | $value="{$escape_like_str($value)}%"; |
||
41 | break; |
||
42 | case 'both': |
||
43 | case 'all': |
||
44 | default: |
||
45 | $value="%{$escape_like_str($value)}%"; |
||
46 | break; |
||
47 | } |
||
48 | |||
49 | return $isAnd ? $query->where($column, $operator, $value) : $query->orWhere($column, $operator, $value); |
||
50 | } |
||
67 |