Test Failed
Push — master ( 7cde39...b3d10e )
by Mauro
05:55
created

AbstractQueryBuilder::appendComma()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the DbImporter package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace DbImporter\QueryBuilder;
12
13
use DbImporter\QueryBuilder\Contracts\QueryBuilderInterface;
14
use Doctrine\Common\Collections\ArrayCollection;
15
16
abstract class AbstractQueryBuilder implements QueryBuilderInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $table;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $debug;
27
28
    /**
29
     * @var array
30
     */
31
    protected $mapping;
32
33
    /**
34
     * @var ArrayCollection
35
     */
36
    protected $data;
37
38
    /**
39
     * MySqlQueryBuilderTest constructor.
40
     * @param $table
41
     * @param $debug
42
     * @param array $mapping
43
     * @param array $data
44
     */
45
    public function __construct(
46
        $table,
47
        array $mapping,
48
        array $data,
49
        $debug
50
    ) {
51
        $this->table = $table;
52
        $this->mapping = $mapping;
53
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type array is incompatible with the declared type Doctrine\Common\Collections\ArrayCollection of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->debug = $debug;
55
    }
56
57
    /**
58
     * @param $index
59
     * @param $array
60
     * @return string
61
     */
62
    protected function appendComma($index, $array)
63
    {
64
        if ($index < (count($array))) {
65
            return  ', ';
66
        }
67
    }
68
69
    /**
70
     * @param $mode
71
     * @return array
72
     */
73
    protected function getQueriesBody($mode, $limit)
74
    {
75
        if ($mode == 'multiple') {
76
            return $this->getMultipleInsertQueriesBody($limit);
77
        }
78
79
        return $this->getSingleInsertQueriesBody();
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    protected function getMultipleInsertQueriesBody($limit)
86
    {
87
        $sql = [];
88
        $data = array_chunk($this->data, $limit, true);
0 ignored issues
show
Bug introduced by
$this->data of type Doctrine\Common\Collections\ArrayCollection is incompatible with the type array expected by parameter $input of array_chunk(). ( Ignorable by Annotation )

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

88
        $data = array_chunk(/** @scrutinizer ignore-type */ $this->data, $limit, true);
Loading history...
89
90
        foreach ($data as $array) {
91
            $count = count($array);
92
            $string = '';
93
94
            for ($c = 1; $c <= $count; $c++) {
95
                $string .= '('.$this->getItemPlaceholders($c).')';
96
                $string .= $this->appendComma($c, $array);
97
            }
98
99
            $sql[] = $string;
100
        }
101
102
        return $sql;
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    protected function getSingleInsertQueriesBody()
109
    {
110
        $sql = [];
111
        $count = count($this->data);
112
113
        for ($c = 1; $c <= $count; $c++) {
114
            $sql[] = '('.$this->getItemPlaceholders().')';
115
        }
116
117
        return $sql;
118
    }
119
120
    /**
121
     * @param $index
122
     * @return string
123
     */
124
    private function getItemPlaceholders($index = null)
125
    {
126
        $sql = '';
127
        $c = 1;
128
        $values = array_values($this->mapping);
129
130
        foreach ($values as $map) {
131
            $sql .= ($index) ? ':'.$map.'_'.$index : ':'.$map;
132
            $sql .= $this->appendComma($c, $values);
133
            $c++;
134
        }
135
136
        return $sql;
137
    }
138
}
139