Passed
Push — main ( 3912a5...5eac8c )
by BRUNO
02:06
created

CrudBuilder::executeQuery()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 3
nop 0
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace BMorais\Database;
4
5
/**
6
 * CLASS ABSTRACT QUERY
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 private
14
 */
15
class CrudBuilder
16
{
17
    use DatalayerTrait;
18
19
20
    private array $sqlPartsSelect = [
21
        'main'     => [],
22
        'join'     => [],
23
        'where'    => "",
24
        'andWhere' => [],
25
        'orWhere'  => [],
26
        'groupBy'  => [],
27
        'having'   => [],
28
        'andHaving'=> [],
29
        'orHaving' => [],
30
        'orderBy'  => "",
31
        'limit'    => "",
32
        'offset'   => "",
33
34
    ];
35
36
    /**
37
     *
38
    * <code>
39
    *   $qb = $this->select('u.id, p.id')
40
    *           ->where('phonenumbers=?', [$number]);
41
    * </code>
42
    * @param string $fields
43
    * @param array $paramns
44
    * @return $this
45
     */
46
    public function select(string $fields = "*", array $paramns = []): CrudBuilder
47
    {
48
        try {
49
            $query = "SELECT {$fields} FROM {$this->getTable()} ";
50
            if (!empty($this->getTableAlias()))
51
                $query .= "AS {$this->getTableAlias()} ";
52
            $this->add($query, "main", $paramns);
53
            return $this;
54
        } catch (\PDOException $e) {
55
            $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...
56
        }
57
    }
58
59
    /**
60
    * @param string $fields
61
    * @param array $paramns
62
    * @return $this
63
     */
64
    public function insert(string $fields, array $paramns): self
65
    {
66
        try {
67
            $numparams = '';
68
            foreach ($paramns as $item) {
69
                $numparams .= ',?';
70
            }
71
            $numparams = substr($numparams, 1);
72
            $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
73
            $this->add($query, "main", $paramns);
74
            return $this;
75
        } catch (\PDOException $e) {
76
            $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...
77
        }
78
    }
79
80
    /**
81
    * @param string $fields
82
    * @param array $paramns
83
    * @return $this
84
     */
85
    public function update(string $fields, array $paramns): self
86
    {
87
        try {
88
            $fields_T = '';
89
            $atributos = explode(',', $fields);
90
91
            foreach ($atributos as $item) {
92
                $fields_T .= ", {$item} = ?";
93
            }
94
            $fields_T = substr($fields_T, 2);
95
            $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
96
            $this->add($query, "main", $paramns);
97
            return $this;
98
        } catch (\PDOException $e) {
99
            $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...
100
        }
101
    }
102
103
    /**
104
    * @param string $fields
105
    * @param array $paramns
106
    * @return $this
107
     */
108
    public function delete(string $fields, array $paramns): self
0 ignored issues
show
Unused Code introduced by
The parameter $fields 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

108
    public function delete(/** @scrutinizer ignore-unused */ string $fields, array $paramns): self

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...
109
    {
110
        try {
111
            $query = "DELETE FROM {$this->getTable()}";
112
            $this->add($query, "main", $paramns);
113
            return $this;
114
        } catch (\PDOException $e) {
115
            $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...
116
        }
117
    }
118
119
    /**
120
    * @param string $query
121
    * @param array $paramns
122
    * @return $this
123
     */
124
    public function query(string $query, array $paramns = []): self
125
    {
126
        try {
127
            $this->add($query, "main", $paramns);
128
            return $this;
129
        } catch (\PDOException $e) {
130
            $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...
131
        }
132
    }
133
134
    /**
135
    * @param string $texto
136
    * @param array $paramns
137
    * @return $this
138
     */
139
    public function where(string $texto, array $paramns = []): self
140
    {
141
        try {
142
            $query = "WHERE {$texto} ";
143
            $this->add($query, "where", $paramns);
144
            return $this;
145
        } catch (\PDOException $e) {
146
            $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...
147
        }
148
    }
149
150
    /**
151
    * @param string $condition
152
    * @param array $paramns
153
    * @return $this
154
     */
155
    public function andWhere(string $condition, array $paramns = []): self
156
    {
157
        try {
158
            $query = "AND {$condition} ";
159
            $this->add($query, "andWhere", $paramns);
160
            return $this;
161
        } catch (\PDOException $e) {
162
            $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...
163
        }
164
    }
165
166
    /**
167
    * @param string $texto
168
    * @param array $paramns
169
    * @return $this
170
     */
171
    public function orWhere(string $texto, array $paramns = []): self
172
    {
173
        try {
174
            $query = "OR {$texto} ";
175
            $this->add($query, "orWhere", $paramns);
176
            return $this;
177
        } catch (\PDOException $e) {
178
            $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...
179
        }
180
    }
181
182
    /**
183
    * @param string $texto
184
    * @return $this
185
     */
186
    public function orderBy(string $texto, $order = null): self
187
    {
188
        try {
189
            $query = "ORDER BY {$texto} ($order ?? 'ASC')";
190
            $this->add($query, "orderBy");
191
            return $this;
192
        } catch (\PDOException $e) {
193
            $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...
194
        }
195
    }
196
197
    /**
198
    * @param string $texto
199
    * @return $this
200
     */
201
    public function limit(string $texto): self
202
    {
203
        $query = "LIMIT {$texto} ";
204
        $this->add($query,"limit");
205
        return $this;
206
    }
207
208
    /**
209
    * @param string $texto
210
    * @return $this
211
     */
212
    public function offset(string $texto): self
213
    {
214
        try {
215
            $query = "OFFSET {$texto} ";
216
            $this->add($query,"offset");
217
            return $this;
218
        } catch (\PDOException $e) {
219
            $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...
220
        }
221
    }
222
223
    /**
224
    * @param string $texto
225
    * @return $this
226
     */
227
    public function groupBy(string $texto): self
228
    {
229
        try {
230
            $query = "GROUP BY {$texto} ";
231
            $this->add($query,"groupBy");
232
            return $this;
233
        } catch (\PDOException $e) {
234
            $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...
235
        }
236
    }
237
238
    /**
239
    * @param string $texto
240
    * @return $this
241
     */
242
    public function having(string $texto): self
243
    {
244
        try {
245
            $query = "HAVING {$texto} ";
246
            $this->add($query,"having");
247
            return $this;
248
        } catch (\PDOException $e) {
249
            $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...
250
        }
251
    }
252
253
    /**
254
     * @param string $texto
255
     * @return $this
256
     */
257
    public function andHaving(string $texto): self
258
    {
259
        try {
260
            $query = "AND {$texto} ";
261
            $this->add($query,"andHaving");
262
            return $this;
263
        } catch (\PDOException $e) {
264
            $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...
265
        }
266
    }
267
268
    /**
269
     * @param string $codition
270
     * @return $this
271
     */
272
    public function orHaving(string $codition): self
273
    {
274
        try {
275
            $query = "OR {$codition} ";
276
            $this->add($query,"orHaving");
277
            return $this;
278
        } catch (\PDOException $e) {
279
            $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...
280
        }
281
    }
282
283
    /**
284
    * @param string $table
285
    * @param string $alias
286
    * @param string $codition
287
    * @return $this
288
     */
289
    public function innerJoin(string $table, string $alias, string $codition): self
290
    {
291
        try {
292
            $query = "INNER JOIN {$table} AS {$alias} ON $codition ";
293
            $this->add($query,"join");
294
            return $this;
295
        } catch (\PDOException $e) {
296
            $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...
297
        }
298
    }
299
300
    /**
301
    * @param string $table
302
    * @param string $alias
303
    * @param string $codition
304
    * @return $this
305
     */
306
    public function leftJoin(string $table, string $alias, string $codition): self
307
    {
308
        try {
309
            $query = "LEFT JOIN {$table} AS {$alias} ON $codition ";
310
            $this->add($query,"join");
311
            return $this;
312
        } catch (\PDOException $e) {
313
            $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...
314
        }
315
    }
316
317
    /**
318
    * @param string $table
319
    * @param string $alias
320
    * @param string $codition
321
    * @return $this
322
     */
323
    public function rightJoin(string $table, string $alias, string $codition): self
324
    {
325
        try {
326
            $query = "RIGHT JOIN {$table} AS {$alias} ON $codition ";
327
            $this->add($query,"join");
328
            return $this;
329
        } catch (\PDOException $e) {
330
            $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...
331
        }
332
    }
333
334
    /**
335
     * @return $this
336
     */
337
    public function executeQuery(): self
338
    {
339
        try {
340
            foreach ($this->sqlPartsSelect as $key => $part){
341
                if (is_array($part)) {
342
                    foreach ($part as $item){
343
                        $this->query .= $item;
344
                    }
345
                } else {
346
                    $this->query .= $part;
347
                }
348
349
            }
350
351
            $this->executeSQL($this->query, $this->params);
352
            return $this;
353
        } catch (\PDOException $e) {
354
            $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...
355
        }
356
    }
357
358
    /**
359
    * @return void
360
     */
361
    public function debug()
362
    {
363
        try {
364
            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

364
            echo $this->query . '<pre>' . /** @scrutinizer ignore-type */ print_r($this->params) . '</pre>';
Loading history...
365
            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...
366
        } catch (\PDOException $e) {
367
            $this->setError($e);
368
        }
369
    }
370
371
    /**
372
     * @return false|string
373
     */
374
    public function lastInsertId(): ?string
375
    {
376
        try {
377
            return $this->lastId();
378
        } catch (\PDOException $e) {
379
            $this->setError($e);
380
        }
381
    }
382
383
    /**
384
     * @return string|null
385
     */
386
    protected function getSQL(): ?string
387
    {
388
        try {
389
            return $this->logSQL ?? "";
390
        } catch (\PDOException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\PDOException $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
391
            $this->setError($e);
392
        }
393
    }
394
395
    public function setParameter($params): self
396
    {
397
        $this->params = array_merge($this->params, $params);
398
        return $this;
399
    }
400
401
    private function add(string $text, string $type, array $params = [])
402
    {
403
        try {
404
            if (is_array($this->sqlPartsSelect[$type])) {
405
                $this->sqlPartsSelect[$type][] = $text;
406
            } else {
407
                $this->sqlPartsSelect[$type] = $text;
408
            }
409
410
            if (!empty($params))
411
                $this->params = array_merge($this->params, $params);
412
        } catch (\PDOException $e) {
413
            $this->setError($e);
414
        }
415
    }
416
417
}
418