Passed
Push — main ( b51950...376464 )
by BRUNO
02:00
created

CrudBuilder::selectBuilder()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 7
nop 2
dl 0
loc 10
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
    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
    public function selectBuilder(string $fields = "*", array $paramns = []): self
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 insertBuilder(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 updateBuilder(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 deleteBuilder(): 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
    * @param $order
187
    * @return $this
188
     */
189
    protected function orderBy(string $parameter, $order = null): self
190
    {
191
        try {
192
            $query = "ORDER BY {$parameter} ".($order ?? 'ASC');
193
            $this->add($query, "orderBy");
194
            return $this;
195
        } catch (\PDOException $e) {
196
            $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...
197
        }
198
    }
199
200
    /**
201
    * @param string $parameter
202
    * @param $order
203
    * @return $this
204
     */
205
    protected function addOrderBy(string $parameter, $order = null): self
206
    {
207
        try {
208
            $query = ", {$parameter} ".($order ?? 'ASC')." ";
209
            $this->add($query, "addOrderBy");
210
            return $this;
211
        } catch (\PDOException $e) {
212
            $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...
213
        }
214
    }
215
216
    /**
217
    * @param string $texto
218
    * @return $this
219
     */
220
    protected function limit(string $texto): self
221
    {
222
        $query = "LIMIT {$texto}";
223
        $this->add($query,"limit");
224
        return $this;
225
    }
226
227
    /**
228
    * @param string $texto
229
    * @return $this
230
     */
231
    protected function offset(string $texto): self
232
    {
233
        try {
234
            $query = "OFFSET {$texto}";
235
            $this->add($query,"offset");
236
            return $this;
237
        } catch (\PDOException $e) {
238
            $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...
239
        }
240
    }
241
242
    /**
243
    * @param string $texto
244
    * @return $this
245
     */
246
    protected function groupBy(string $texto): self
247
    {
248
        try {
249
            $query = "GROUP BY {$texto}";
250
            $this->add($query,"groupBy");
251
            return $this;
252
        } catch (\PDOException $e) {
253
            $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...
254
        }
255
    }
256
257
    /**
258
    * @param string $texto
259
    * @return $this
260
     */
261
    protected function having(string $texto): self
262
    {
263
        try {
264
            $query = "HAVING {$texto}";
265
            $this->add($query,"having");
266
            return $this;
267
        } catch (\PDOException $e) {
268
            $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...
269
        }
270
    }
271
272
    /**
273
     * @param string $texto
274
     * @return $this
275
     */
276
    protected function andHaving(string $texto): self
277
    {
278
        try {
279
            $query = "AND {$texto}";
280
            $this->add($query,"andHaving");
281
            return $this;
282
        } catch (\PDOException $e) {
283
            $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...
284
        }
285
    }
286
287
    /**
288
     * @param string $codition
289
     * @return $this
290
     */
291
    protected function orHaving(string $codition): self
292
    {
293
        try {
294
            $query = "OR {$codition}";
295
            $this->add($query,"orHaving");
296
            return $this;
297
        } catch (\PDOException $e) {
298
            $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...
299
        }
300
    }
301
302
    /**
303
    * @param string $table
304
    * @param string $alias
305
    * @param string $codition
306
    * @return $this
307
     */
308
    protected function innerJoin(string $table, string $alias, string $codition): self
309
    {
310
        try {
311
            $query = "INNER JOIN {$table} AS {$alias} ON $codition";
312
            $this->add($query,"join");
313
            return $this;
314
        } catch (\PDOException $e) {
315
            $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...
316
        }
317
    }
318
319
    /**
320
    * @param string $table
321
    * @param string $alias
322
    * @param string $codition
323
    * @return $this
324
     */
325
    protected function leftJoin(string $table, string $alias, string $codition): self
326
    {
327
        try {
328
            $query = "LEFT JOIN {$table} AS {$alias} ON {$codition}";
329
            $this->add($query,"join");
330
            return $this;
331
        } catch (\PDOException $e) {
332
            $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...
333
        }
334
    }
335
336
    /**
337
    * @param string $table
338
    * @param string $alias
339
    * @param string $codition
340
    * @return $this
341
     */
342
    protected function rightJoin(string $table, string $alias, string $codition): self
343
    {
344
        try {
345
            $query = "RIGHT JOIN {$table} AS {$alias} ON $codition";
346
            $this->add($query,"join");
347
            return $this;
348
        } catch (\PDOException $e) {
349
            $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...
350
        }
351
    }
352
353
    /**
354
     * @return $this
355
     */
356
    protected function executeQuery(): self
357
    {
358
        try {
359
            foreach ($this->sqlPartsSelect as $key => $part){
360
                if (is_array($part)) {
361
                    foreach ($part as $item){
362
                        $this->query .= $item;
363
                    }
364
                } else {
365
                    $this->query .= $part;
366
                }
367
368
            }
369
370
            $this->executeSQL($this->query, $this->params);
371
            return $this;
372
        } catch (\PDOException $e) {
373
            $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...
374
        }
375
    }
376
377
    /**
378
    * @return void
379
     */
380
    protected function debug(): void
381
    {
382
        try {
383
            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

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