Passed
Push — main ( 99a7ee...701a87 )
by BRUNO
02:24
created

CrudBuilder::setParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BMorais\Database;
4
5
/**
6
 * CLASS CrudBuilder
7
 * Basic class to make connection between the database and the application
8
 *
9
 * @author Bruno Morais <[email protected]>
10
 * @copyright MIT, bmorais.com
11
 * @package bmorais\database
12
 * @subpackage class
13
 * @access protected
14
 */
15
class CrudBuilder
16
{
17
    use DatalayerTrait;
18
19
    /**
20
    * @var array
21
     */
22
    private array $sqlPartsSelect = [
23
        'main'      => [],
24
        'join'      => [],
25
        'where'     => "",
26
        'andWhere'  => [],
27
        'orWhere'   => [],
28
        'groupBy'   => [],
29
        'having'    => [],
30
        'andHaving' => [],
31
        'orHaving'  => [],
32
        'orderBy'   => "",
33
        'addOrderBy'=> [],
34
        'limit'     => "",
35
        'offset'    => "",
36
    ];
37
38
    /**
39
     *
40
    * <code>
41
    *   $qb = $this->select('u.id, p.id')
42
    *           ->where('phonenumbers=?', [$number]);
43
    * </code>
44
    * @param string $fields
45
    * @param array $paramns
46
    * @return $this
47
     */
48
    protected function select(string $fields = "*", array $paramns = []): CrudBuilder
49
    {
50
        try {
51
            $query = "SELECT {$fields} FROM {$this->getTable()}";
52
            if (!empty($this->getTableAlias()))
53
                $query .= " AS {$this->getTableAlias()}";
54
            $this->add($query, "main", $paramns);
55
            return $this;
56
        } catch (\PDOException $e) {
57
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
58
        }
59
    }
60
61
    /**
62
    * @param string $fields
63
    * @param array $paramns
64
    * @return $this
65
     */
66
    protected function insert(string $fields, array $paramns): self
67
    {
68
        try {
69
            $numparams = '';
70
            foreach ($paramns as $item) {
71
                $numparams .= ',?';
72
            }
73
            $numparams = substr($numparams, 1);
74
            $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
75
            $this->add($query, "main", $paramns);
76
            return $this;
77
        } catch (\PDOException $e) {
78
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
79
        }
80
    }
81
82
    /**
83
    * @param string $fields
84
    * @param array $paramns
85
    * @return $this
86
     */
87
    protected function update(string $fields, array $paramns): self
88
    {
89
        try {
90
            $fields_T = '';
91
            $atributos = explode(',', $fields);
92
93
            foreach ($atributos as $item) {
94
                $fields_T .= ", {$item} = ?";
95
            }
96
            $fields_T = substr($fields_T, 2);
97
            $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
98
            $this->add($query, "main", $paramns);
99
            return $this;
100
        } catch (\PDOException $e) {
101
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
102
        }
103
    }
104
105
    /**
106
    * @param string $fields
107
    * @param array $paramns
108
    * @return $this
109
     */
110
    protected function delete(): self
111
    {
112
        try {
113
            $query = "DELETE FROM {$this->getTable()}";
114
            $this->add($query, "main");
115
            return $this;
116
        } catch (\PDOException $e) {
117
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
118
        }
119
    }
120
121
    /**
122
    * @param string $query
123
    * @param array $paramns
124
    * @return $this
125
     */
126
    protected function query(string $query, array $paramns = []): self
127
    {
128
        try {
129
            $this->add($query, "main", $paramns);
130
            return $this;
131
        } catch (\PDOException $e) {
132
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
133
        }
134
    }
135
136
    /**
137
    * @param string $texto
138
    * @param array $paramns
139
    * @return $this
140
     */
141
    protected function where(string $texto, array $paramns = []): self
142
    {
143
        try {
144
            $query = "WHERE {$texto}";
145
            $this->add($query, "where", $paramns);
146
            return $this;
147
        } catch (\PDOException $e) {
148
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
149
        }
150
    }
151
152
    /**
153
    * @param string $condition
154
    * @param array $paramns
155
    * @return $this
156
     */
157
    protected function andWhere(string $condition, array $paramns = []): self
158
    {
159
        try {
160
            $query = "AND {$condition}";
161
            $this->add($query, "andWhere", $paramns);
162
            return $this;
163
        } catch (\PDOException $e) {
164
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
165
        }
166
    }
167
168
    /**
169
    * @param string $condition
170
    * @param array $paramns
171
    * @return $this
172
     */
173
    protected function orWhere(string $condition, array $paramns = []): self
174
    {
175
        try {
176
            $query = "OR {$condition}";
177
            $this->add($query, "orWhere", $paramns);
178
            return $this;
179
        } catch (\PDOException $e) {
180
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
181
        }
182
    }
183
184
    /**
185
    * @param string $parameter
186
    * @return $this
187
     */
188
    protected function orderBy(string $parameter, $order = null): self
189
    {
190
        try {
191
            $query = "ORDER BY {$parameter} ".($order ?? 'ASC');
192
            $this->add($query, "orderBy");
193
            return $this;
194
        } catch (\PDOException $e) {
195
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
196
        }
197
    }
198
199
    protected function addOrderBy(string $parameter, $order = null): self
200
    {
201
        try {
202
            $query = ", {$parameter} ".($order ?? 'ASC')." ";
203
            $this->add($query, "addOrderBy");
204
            return $this;
205
        } catch (\PDOException $e) {
206
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
207
        }
208
    }
209
210
    /**
211
    * @param string $texto
212
    * @return $this
213
     */
214
    protected function limit(string $texto): self
215
    {
216
        $query = "LIMIT {$texto}";
217
        $this->add($query,"limit");
218
        return $this;
219
    }
220
221
    /**
222
    * @param string $texto
223
    * @return $this
224
     */
225
    protected function offset(string $texto): self
226
    {
227
        try {
228
            $query = "OFFSET {$texto}";
229
            $this->add($query,"offset");
230
            return $this;
231
        } catch (\PDOException $e) {
232
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
233
        }
234
    }
235
236
    /**
237
    * @param string $texto
238
    * @return $this
239
     */
240
    protected function groupBy(string $texto): self
241
    {
242
        try {
243
            $query = "GROUP BY {$texto}";
244
            $this->add($query,"groupBy");
245
            return $this;
246
        } catch (\PDOException $e) {
247
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
248
        }
249
    }
250
251
    /**
252
    * @param string $texto
253
    * @return $this
254
     */
255
    protected function having(string $texto): self
256
    {
257
        try {
258
            $query = "HAVING {$texto}";
259
            $this->add($query,"having");
260
            return $this;
261
        } catch (\PDOException $e) {
262
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
263
        }
264
    }
265
266
    /**
267
     * @param string $texto
268
     * @return $this
269
     */
270
    protected function andHaving(string $texto): self
271
    {
272
        try {
273
            $query = "AND {$texto}";
274
            $this->add($query,"andHaving");
275
            return $this;
276
        } catch (\PDOException $e) {
277
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
278
        }
279
    }
280
281
    /**
282
     * @param string $codition
283
     * @return $this
284
     */
285
    protected function orHaving(string $codition): self
286
    {
287
        try {
288
            $query = "OR {$codition}";
289
            $this->add($query,"orHaving");
290
            return $this;
291
        } catch (\PDOException $e) {
292
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
293
        }
294
    }
295
296
    /**
297
    * @param string $table
298
    * @param string $alias
299
    * @param string $codition
300
    * @return $this
301
     */
302
    protected function innerJoin(string $table, string $alias, string $codition): self
303
    {
304
        try {
305
            $query = "INNER JOIN {$table} AS {$alias} ON $codition";
306
            $this->add($query,"join");
307
            return $this;
308
        } catch (\PDOException $e) {
309
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
310
        }
311
    }
312
313
    /**
314
    * @param string $table
315
    * @param string $alias
316
    * @param string $codition
317
    * @return $this
318
     */
319
    protected function leftJoin(string $table, string $alias, string $codition): self
320
    {
321
        try {
322
            $query = "LEFT JOIN {$table} AS {$alias} ON {$codition}";
323
            $this->add($query,"join");
324
            return $this;
325
        } catch (\PDOException $e) {
326
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
327
        }
328
    }
329
330
    /**
331
    * @param string $table
332
    * @param string $alias
333
    * @param string $codition
334
    * @return $this
335
     */
336
    protected function rightJoin(string $table, string $alias, string $codition): self
337
    {
338
        try {
339
            $query = "RIGHT JOIN {$table} AS {$alias} ON $codition";
340
            $this->add($query,"join");
341
            return $this;
342
        } catch (\PDOException $e) {
343
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
344
        }
345
    }
346
347
    /**
348
     * @return $this
349
     */
350
    protected function executeQuery(): self
351
    {
352
        try {
353
            foreach ($this->sqlPartsSelect as $key => $part){
354
                if (is_array($part)) {
355
                    foreach ($part as $item){
356
                        $this->query .= $item;
357
                    }
358
                } else {
359
                    $this->query .= $part;
360
                }
361
362
            }
363
364
            $this->executeSQL($this->query, $this->params);
365
            return $this;
366
        } catch (\PDOException $e) {
367
            $this->setError($e);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BMorais\Database\CrudBuilder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
368
        }
369
    }
370
371
    /**
372
    * @return void
373
     */
374
    protected function debug()
375
    {
376
        try {
377
            echo $this->query . '<pre>' . print_r($this->params) . '</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->params) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

377
            echo $this->query . '<pre>' . /** @scrutinizer ignore-type */ print_r($this->params) . '</pre>';
Loading history...
378
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
379
        } catch (\PDOException $e) {
380
            $this->setError($e);
381
        }
382
    }
383
384
    /**
385
     * @return false|string
386
     */
387
    protected function lastInsertId(): ?string
388
    {
389
        try {
390
            return $this->lastId();
391
        } catch (\PDOException $e) {
392
            $this->setError($e);
393
        }
394
    }
395
396
    /**
397
    * @param $params
398
    * @return self
399
     */
400
    protected function setParameter($params): self
401
    {
402
        $this->params = array_merge($this->params, $params);
403
        return $this;
404
    }
405
406
    /**
407
    * @param string $query
408
    * @param string $type
409
    * @param array $params
410
    * @return void
411
     */
412
    private function add(string $query, string $type, array $params = []): void
413
    {
414
        $query = $query." ";
415
        try {
416
            if (is_array($this->sqlPartsSelect[$type])) {
417
                $this->sqlPartsSelect[$type][] = $query;
418
            } else {
419
                $this->sqlPartsSelect[$type] = $query;
420
            }
421
422
            if (!empty($params))
423
                $this->params = array_merge($this->params, $params);
424
        } catch (\PDOException $e) {
425
            $this->setError($e);
426
        }
427
    }
428
429
}
430