Passed
Push — master ( e2745c...f14e7e )
by Nícollas
01:10
created

CRUD::update()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 51
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 51
rs 8.4444
cc 8
nc 96
nop 4

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace SimplePHP\Model;
4
5
use PDO;
6
use PDOException;
7
8
trait CRUD {
9
    
10
    public function update($table, String $params, Array $values, $where) {
11
12
        $where = $where != '' ? $where = "WHERE ".$where : $where = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $where is dead and can be removed.
Loading history...
13
14
        $params = explode(', ', $params);
15
16
        $data = [];
17
18
        for($i = 0; $i < count($params); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
43
44
            $data[$i] = ":".$params[$i][0].$params[$i][1].$params[$i][2];
45
        
46
        }
47
48
        for($i = 0; $i < count($data); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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
62
        }
63
64
    }
65
66
}