Passed
Push — 9.0-dev ( 09a935...fea9bd )
by Radu
01:30
created

DatabaseTrait::insertIgnore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
trait DatabaseTrait
5
{
6
    public function insert($tableName, $data = [])
7
    {
8
        return $this->add(self::QUERY_TYPE_INSERT, $tableName, $data);
0 ignored issues
show
Bug introduced by
The constant WebServCo\Framework\Trai...rait::QUERY_TYPE_INSERT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
9
    }
10
    
11
    public function insertIgnore($tableName, $data = [])
12
    {
13
        return $this->add(self::QUERY_TYPE_INSERT_IGNORE, $tableName, $data);
0 ignored issues
show
Bug introduced by
The constant WebServCo\Framework\Trai...UERY_TYPE_INSERT_IGNORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
    }
15
    
16
    public function replace($tableName, $data = [])
17
    {
18
        return $this->add(self::QUERY_TYPE_REPLACE, $tableName, $data);
0 ignored issues
show
Bug introduced by
The constant WebServCo\Framework\Trai...ait::QUERY_TYPE_REPLACE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
    }
20
    
21
    protected function add($queryType, $tableName, $data = [])
22
    {
23
        if (empty($tableName) || empty($data)) {
24
            throw new \ErrorException('No data specified');
25
        }
26
        
27
        $query = $this->generateAddQuery($queryType, $tableName, $data);
28
        
29
        return $this->query($query, $data);
0 ignored issues
show
Bug introduced by
It seems like query() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        return $this->/** @scrutinizer ignore-call */ query($query, $data);
Loading history...
30
    }
31
    
32
    protected function getKeysValues($data = [])
33
    {
34
        $multiDimensional = is_array($data[key($data)]);
35
        if ($multiDimensional) {
36
            $keys = array_keys(call_user_func_array('array_merge', $data));
37
            // fill any missing keys with empty data
38
            $key_pair = array_combine($keys, array_fill(0, count($keys), null));
39
            $data = array_map(function ($e) use ($key_pair) {
40
                return array_merge($key_pair, $e);
41
            }, $data);
42
        } else {
43
            $keys = array_keys($data);
44
        }
45
        
46
        return [$keys, $data];
47
    }
48
    
49
    protected function generateAddQuery($queryType, $tableName, $data)
50
    {
51
        $multiDimensional = is_array($data[key($data)]);
52
        
53
        list($keys, $data) = $this->getKeysValues($data);
54
        
55
        switch ($queryType) {
56
            case self::QUERY_TYPE_REPLACE:
0 ignored issues
show
Bug introduced by
The constant WebServCo\Framework\Trai...ait::QUERY_TYPE_REPLACE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57
                $query = self::QUERY_TYPE_REPLACE . ' INTO';
58
                break;
59
            case self::QUERY_TYPE_INSERT_IGNORE:
0 ignored issues
show
Bug introduced by
The constant WebServCo\Framework\Trai...UERY_TYPE_INSERT_IGNORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60
                $query = self::QUERY_TYPE_INSERT_IGNORE . ' INTO';
61
                break;
62
            case self::QUERY_TYPE_INSERT:
0 ignored issues
show
Bug introduced by
The constant WebServCo\Framework\Trai...rait::QUERY_TYPE_INSERT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
            default:
64
                $query = self::QUERY_TYPE_INSERT . ' INTO';
65
                break;
66
        }
67
        $query .= ' '.$this->escapeIdentifier($tableName).' (' .
0 ignored issues
show
Bug introduced by
It seems like escapeIdentifier() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $query .= ' '.$this->/** @scrutinizer ignore-call */ escapeIdentifier($tableName).' (' .
Loading history...
68
        implode(', ', array_map([$this, 'escapeIdentifier'], $keys)) .
69
        ') VALUES';
70
        if ($multiDimensional) {
71
            $valuesStrings = [];
72
            foreach ($data as $item) {
73
                $valuesStrings[] = $this->generateValuesString($item);
74
            }
75
            $query .= implode(', ', $valuesStrings);
76
        } else {
77
            $query .= $this->generateValuesString($data);
78
        }
79
        return $query;
80
    }
81
    
82
    protected function generateValuesString($data)
83
    {
84
        return ' (' . implode(', ', array_map(function ($v) {
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

84
        return ' (' . implode(', ', array_map(function (/** @scrutinizer ignore-unused */ $v) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
            return '?';
86
        }, $data)) . ')';
87
    }
88
}
89