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

CRUD   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B update() 0 51 8
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
}