CrudBuilder::debug()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
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
abstract class CrudBuilder extends Crud
16
{
17
    /**
18
    * @var array
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
        'addOrderBy'=> [],
32
        'limit'     => "",
33
        'offset'    => "",
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
    protected function selectBuilder(string $fields = "*", array $paramns = []): self
47
    {
48
        try {
49
            $query = "SELECT {$fields} FROM {$this->getTableName()}";
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
    protected function insertBuilder(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->getTableName()} ({$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
    protected function updateBuilder(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->getTableName()}} 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
    protected function deleteBuilder(): self
109
    {
110
        try {
111
            $query = "DELETE FROM {$this->getTableName()}";
112
            $this->add($query, "main");
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
    protected 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
    protected 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
    protected 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 $condition
168
    * @param array $paramns
169
    * @return $this
170
     */
171
    protected function orWhere(string $condition, array $paramns = []): self
172
    {
173
        try {
174
            $query = "OR {$condition}";
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 $parameter
184
    * @param $order
185
    * @return $this
186
     */
187
    protected function orderBy(string $parameter, ?string $order = null): self
188
    {
189
        try {
190
            $query = "ORDER BY {$parameter} ".($order ?? 'ASC');
191
            $this->add($query, "orderBy");
192
            return $this;
193
        } catch (\PDOException $e) {
194
            $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...
195
        }
196
    }
197
198
    /**
199
    * @param string $parameter
200
    * @param $order
201
    * @return $this
202
     */
203
    protected function addOrderBy(string $parameter, ?string $order = null): self
204
    {
205
        try {
206
            $query = ", {$parameter} ".($order ?? 'ASC')." ";
207
            $this->add($query, "addOrderBy");
208
            return $this;
209
        } catch (\PDOException $e) {
210
            $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...
211
        }
212
    }
213
214
    /**
215
    * @param string $texto
216
    * @return $this
217
     */
218
    protected function limit(string $texto): self
219
    {
220
        $query = "LIMIT {$texto}";
221
        $this->add($query,"limit");
222
        return $this;
223
    }
224
225
    /**
226
    * @param string $texto
227
    * @return $this
228
     */
229
    protected function offset(string $texto): self
230
    {
231
        try {
232
            $query = "OFFSET {$texto}";
233
            $this->add($query,"offset");
234
            return $this;
235
        } catch (\PDOException $e) {
236
            $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...
237
        }
238
    }
239
240
    /**
241
    * @param string $texto
242
    * @return $this
243
     */
244
    protected function groupBy(string $texto): self
245
    {
246
        try {
247
            $query = "GROUP BY {$texto}";
248
            $this->add($query,"groupBy");
249
            return $this;
250
        } catch (\PDOException $e) {
251
            $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...
252
        }
253
    }
254
255
    /**
256
    * @param string $texto
257
    * @return $this
258
     */
259
    protected function having(string $texto): self
260
    {
261
        try {
262
            $query = "HAVING {$texto}";
263
            $this->add($query,"having");
264
            return $this;
265
        } catch (\PDOException $e) {
266
            $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...
267
        }
268
    }
269
270
    /**
271
     * @param string $texto
272
     * @return $this
273
     */
274
    protected function andHaving(string $texto): self
275
    {
276
        try {
277
            $query = "AND {$texto}";
278
            $this->add($query,"andHaving");
279
            return $this;
280
        } catch (\PDOException $e) {
281
            $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...
282
        }
283
    }
284
285
    /**
286
     * @param string $codition
287
     * @return $this
288
     */
289
    protected function orHaving(string $codition): self
290
    {
291
        try {
292
            $query = "OR {$codition}";
293
            $this->add($query,"orHaving");
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
    protected function innerJoin(string $table, string $alias, string $codition): self
307
    {
308
        try {
309
            $query = "INNER 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
    protected function leftJoin(string $table, string $alias, string $codition): self
324
    {
325
        try {
326
            $query = "LEFT 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
    * @param string $table
336
    * @param string $alias
337
    * @param string $codition
338
    * @return $this
339
     */
340
    protected function rightJoin(string $table, string $alias, string $codition): self
341
    {
342
        try {
343
            $query = "RIGHT JOIN {$table} AS {$alias} ON $codition";
344
            $this->add($query,"join");
345
            return $this;
346
        } catch (\PDOException $e) {
347
            $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...
348
        }
349
    }
350
351
    /**
352
     * @return $this
353
     */
354
    protected function executeQuery(): self
355
    {
356
        try {
357
            foreach ($this->sqlPartsSelect as $key => $part){
358
                if (is_array($part)) {
359
                    foreach ($part as $item){
360
                        $this->setQuery($this->getQuery().$item);
361
                    }
362
                } else {
363
                    $this->setQuery($this->getQuery().$part);
364
                }
365
366
            }
367
368
            $this->executeSQL($this->getQuery(), $this->getParams());
369
            return $this;
370
        } catch (\PDOException $e) {
371
            $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...
372
        }
373
    }
374
375
    /**
376
    * @return void
377
     */
378
    protected function debug(): void
379
    {
380
        try {
381
            echo $this->getQuery() . '<pre>' . print_r($this->getParams()) . '</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->getParams()) 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

381
            echo $this->getQuery() . '<pre>' . /** @scrutinizer ignore-type */ print_r($this->getParams()) . '</pre>';
Loading history...
382
            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...
383
        } catch (\PDOException $e) {
384
            $this->setError($e);
385
        }
386
    }
387
388
389
    /**
390
    * @param string $query
391
    * @param string $type
392
    * @param array $params
393
    * @return void
394
     */
395
    private function add(string $query, string $type, array $params = []): void
396
    {
397
        $query = $query." ";
398
        try {
399
            if (is_array($this->sqlPartsSelect[$type])) {
400
                $this->sqlPartsSelect[$type][] = $query;
401
            } else {
402
                $this->sqlPartsSelect[$type] = $query;
403
            }
404
405
            if (!empty($params))
406
                $this->setParams($params);
407
        } catch (\PDOException $e) {
408
            $this->setError($e);
409
        }
410
    }
411
412
}
413