Conditions | 16 |
Paths | 100 |
Total Lines | 76 |
Code Lines | 41 |
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 |
||
48 | public function where($val1, $val2 = null, string $operator = null, bool $bind = null, string $join = null):Statement{ |
||
49 | $operator = $operator !== null ? strtoupper(trim($operator)) : '='; |
||
50 | $bind = $bind ?? true; |
||
51 | |||
52 | $join = strtoupper(trim($join)); |
||
|
|||
53 | $join = in_array($join, $this->joinArgs, true) ? $join : 'AND'; |
||
54 | |||
55 | if(in_array($operator, $this->operators, true)){ |
||
56 | $where = [ |
||
57 | is_array($val1) |
||
58 | ? strtoupper($val1[1]).'('.$this->dialect->quote($val1[0]).')' |
||
59 | : $this->dialect->quote($val1) |
||
60 | ]; |
||
61 | |||
62 | if(in_array($operator, ['IN', 'NOT IN', 'ANY', 'SOME',], true)){ |
||
63 | |||
64 | if(is_array($val2)){ |
||
65 | |||
66 | if($bind){ |
||
67 | $where[] = $operator.'('.implode(',', array_fill(0, count($val2), '?')).')'; |
||
68 | $this->bindValues = array_merge($this->bindValues, $val2); |
||
69 | } |
||
70 | else{ |
||
71 | $where[] = $operator.'('.implode(',', array_map([$this->db, 'escape'], $val2)).')'; // @todo: quote |
||
72 | } |
||
73 | |||
74 | } |
||
75 | else if($val2 instanceof Statement){ |
||
76 | $where[] = $operator.'('.$val2->sql().')'; |
||
77 | $this->bindValues = array_merge($this->bindValues, $val2->bindValues()); |
||
78 | } |
||
79 | |||
80 | } |
||
81 | // else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){ |
||
82 | // @todo |
||
83 | // } |
||
84 | else{ |
||
85 | $where[] = $operator; |
||
86 | |||
87 | if($val2 instanceof Statement){ |
||
88 | $where[] = '('.$val2->sql().')'; |
||
89 | $this->bindValues = array_merge($this->bindValues, $val2->bindValues()); |
||
90 | } |
||
91 | elseif(is_null($val2)){ |
||
92 | $where[] = 'NULL'; |
||
93 | } |
||
94 | elseif(is_bool($val2)){ |
||
95 | $where[] = $val2 ? 'TRUE' : 'FALSE'; |
||
96 | } |
||
97 | elseif(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'], true)){ |
||
98 | $where[] = strtoupper($val2); |
||
99 | } |
||
100 | else { |
||
101 | |||
102 | if($bind){ |
||
103 | $where[] = '?'; |
||
104 | $this->bindValues[] = $val2; |
||
105 | } |
||
106 | else{ |
||
107 | if(!empty($val2)){ |
||
108 | $where[] = $val2; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | } |
||
113 | |||
114 | } |
||
115 | |||
116 | $this->where[] = [ |
||
117 | 'join' => $join, |
||
118 | 'stmt' => implode(' ', $where), |
||
119 | ]; |
||
120 | |||
121 | } |
||
122 | |||
123 | return $this; |
||
124 | } |
||
184 |