| Conditions | 8 |
| Paths | 96 |
| Total Lines | 51 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 10 | public function update($table, String $params, Array $values, $where) { |
||
| 11 | |||
| 12 | $where = $where != '' ? $where = "WHERE ".$where : $where = ''; |
||
|
|
|||
| 13 | |||
| 14 | $params = explode(', ', $params); |
||
| 15 | |||
| 16 | $data = []; |
||
| 17 | |||
| 18 | for($i = 0; $i < count($params); $i++) { |
||
| 19 | |||
| 20 | $data[$i] = ":".$params[$i][0].$params[$i][1].$params[$i][2].", "; |
||
| 21 | |||
| 22 | } |
||
| 23 | |||
| 24 | $result = ''; |
||
| 25 | |||
| 26 | $final = array_map(null, $params, $data); |
||
| 27 | |||
| 28 | foreach($final as $key => $vals) { |
||
| 29 | |||
| 30 | foreach($vals as $chave => $val) { |
||
| 31 | |||
| 32 | $result .= str_replace(':', ' = :', $val); |
||
| 33 | |||
| 34 | } |
||
| 35 | |||
| 36 | } |
||
| 37 | |||
| 38 | $result = rtrim($result, ', '); |
||
| 39 | |||
| 40 | $sql = $this->conn->prepare("UPDATE $table SET $result $where"); |
||
| 41 | |||
| 42 | for($i = 0; $i < count($params); $i++) { |
||
| 43 | |||
| 44 | $data[$i] = ":".$params[$i][0].$params[$i][1].$params[$i][2]; |
||
| 45 | |||
| 46 | } |
||
| 47 | |||
| 48 | for($i = 0; $i < count($data); $i++) { |
||
| 49 | |||
| 50 | $sql->bindParam($data[$i], $values[$i]); |
||
| 51 | |||
| 52 | } |
||
| 53 | |||
| 54 | if($sql->execute()) { |
||
| 55 | |||
| 56 | return true; |
||
| 57 | |||
| 58 | } else { |
||
| 59 | |||
| 60 | echo "Erro:". $sql->errorInfo(); |
||
| 61 | |||
| 66 | } |