Conditions | 20 |
Paths | 144 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
39 | protected function buildBasicFilter($where) |
||
40 | { |
||
41 | $operator = isset($where['operator']) ? $where['operator'] : '='; |
||
42 | $operator = strtolower($operator); |
||
43 | |||
44 | // != is same as <>, so just use <> |
||
45 | if ($operator == '!=') { |
||
46 | $operator = '<>'; |
||
47 | } |
||
48 | |||
49 | $value = isset($where['value']) ? $where['value'] : null; |
||
50 | $field = $this->getField($where['column']); |
||
51 | |||
52 | switch ($operator) { |
||
53 | case '>': |
||
54 | return $field->gt($value); |
||
55 | case '>=': |
||
56 | return $field->ge($value); |
||
57 | case '<': |
||
58 | return $field->lt($value); |
||
59 | case '<=': |
||
60 | return $field->le($value); |
||
61 | case '<>': |
||
62 | return $field->ne($value); |
||
63 | case 'contains': |
||
64 | return $field->contains($value); |
||
65 | case 'exists': |
||
66 | $field = $field->rDefault(null); |
||
67 | |||
68 | return ($value) ? $field : $field->not(); |
||
69 | case 'type': |
||
70 | return $field->typeOf()->eq(strtoupper($value)); |
||
71 | case 'mod': |
||
72 | $mod = $field->mod((int) $value[0])->eq((int) $value[1]); |
||
73 | |||
74 | return $field->typeOf()->eq('NUMBER')->rAnd($mod); |
||
75 | case 'size': |
||
76 | $size = $field->count()->eq((int) $value); |
||
77 | |||
78 | return $field->typeOf()->eq('ARRAY')->rAnd($size); |
||
79 | case 'regexp': |
||
80 | $match = $field->match($value); |
||
81 | |||
82 | return $field->typeOf()->eq('STRING')->rAnd($match); |
||
83 | case 'not regexp': |
||
84 | $match = $field->match($value)->not(); |
||
85 | |||
86 | return $field->typeOf()->eq('STRING')->rAnd($match); |
||
87 | case 'like': |
||
88 | $regex = str_replace('%', '', $value); |
||
89 | // Convert like to regular expression. |
||
90 | if (!starts_with($value, '%')) { |
||
91 | $regex = '^'.$regex; |
||
92 | } |
||
93 | if (!ends_with($value, '%')) { |
||
94 | $regex = $regex.'$'; |
||
95 | } |
||
96 | $match = $field->match('(?i)'.$regex); |
||
97 | |||
98 | return $field->typeOf()->eq('STRING')->rAnd($match); |
||
99 | default: |
||
100 | return $field->eq($value); |
||
101 | } |
||
102 | } |
||
103 | |||
157 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: