| Conditions | 15 |
| Paths | 27 |
| Total Lines | 74 |
| Code Lines | 44 |
| 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 |
||
| 24 | protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){ |
||
| 25 | $operator = strtoupper(trim($operator)); |
||
| 26 | $join = strtoupper(trim($join)); |
||
| 27 | |||
| 28 | $operators = [ |
||
| 29 | '=', '>=', '>', '<=', '<', '<>', '!=', |
||
| 30 | '|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD', |
||
| 31 | 'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' |
||
| 32 | ]; |
||
| 33 | |||
| 34 | if(in_array($operator, $operators, true)){ |
||
| 35 | $where = [$this->quote($val1)]; |
||
|
|
|||
| 36 | $values = []; |
||
| 37 | |||
| 38 | if(in_array($operator, ['IN', 'NOT IN'], true)){ |
||
| 39 | |||
| 40 | if(is_array($val2)){ |
||
| 41 | if($bind){ |
||
| 42 | $where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')'; |
||
| 43 | $values = array_merge($values, $val2); |
||
| 44 | } |
||
| 45 | else{ |
||
| 46 | $where[] = 'IN('.implode(',', $val2).')'; // @todo: quote |
||
| 47 | } |
||
| 48 | } |
||
| 49 | else if($val2 instanceof StatementInterface){ |
||
| 50 | $where[] = 'IN('.$val2->sql().')'; |
||
| 51 | $values = array_merge($values, $val2->bindValues()); |
||
| 52 | } |
||
| 53 | |||
| 54 | } |
||
| 55 | else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){ |
||
| 56 | // @todo |
||
| 57 | } |
||
| 58 | else{ |
||
| 59 | $where[] = $operator; |
||
| 60 | |||
| 61 | if(is_null($val2)){ |
||
| 62 | $where[] = 'NULL'; |
||
| 63 | } |
||
| 64 | else if(is_bool($val2)){ |
||
| 65 | $where[] = $val2 ? 'TRUE' : 'FALSE'; |
||
| 66 | } |
||
| 67 | else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){ |
||
| 68 | $where[] = strtoupper($val2); |
||
| 69 | } |
||
| 70 | else if($val2 instanceof StatementInterface){ |
||
| 71 | $where[] = '('.$val2->sql().')'; |
||
| 72 | $values = array_merge($values, $val2->bindValues()); |
||
| 73 | } |
||
| 74 | else{ |
||
| 75 | if($bind){ |
||
| 76 | $where[] = '?'; |
||
| 77 | $values[] = $val2; |
||
| 78 | } |
||
| 79 | else{ |
||
| 80 | if(!empty($val2)){ |
||
| 81 | $where[] = $this->quote($val2); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 88 | $this->bindValues = array_merge($this->bindValues, $values); |
||
| 89 | $this->where[] = [ |
||
| 90 | 'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND', |
||
| 91 | 'stmt' => implode(' ', $where), |
||
| 92 | ]; |
||
| 93 | |||
| 94 | } |
||
| 95 | |||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | |||
| 157 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.