Passed
Push — main ( 914853...3912a5 )
by BRUNO
01:57
created

CrudBuilder::execute()   A

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 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
    * @param string $fields
21
    * @param array $paramns
22
    * @return $this
23
     */
24
    public function select(string $fields = "*", array $paramns = []): CrudBuilder
25
    {
26
        try {
27
            $query = "SELECT {$fields} FROM {$this->getTable()} ";
28
            if (!empty($this->getTableAlias()))
29
                $query .= "AS {$this->getTableAlias()} ";
30
            $this->add($query, $paramns);
31
            return $this;
32
        } catch (\PDOException $e) {
33
            $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...
34
        }
35
    }
36
37
    /**
38
    * @param string $fields
39
    * @param array $paramns
40
    * @return $this
41
     */
42
    public function insert(string $fields, array $paramns): self
43
    {
44
        try {
45
            $numparams = '';
46
            foreach ($paramns as $item) {
47
                $numparams .= ',?';
48
            }
49
            $numparams = substr($numparams, 1);
50
            $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
51
            $this->add($query, $paramns);
52
            return $this;
53
        } catch (\PDOException $e) {
54
            $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...
55
        }
56
    }
57
58
    /**
59
    * @param string $fields
60
    * @param array $paramns
61
    * @return $this
62
     */
63
    public function update(string $fields, array $paramns): self
64
    {
65
        try {
66
            $fields_T = '';
67
            $atributos = explode(',', $fields);
68
69
            foreach ($atributos as $item) {
70
                $fields_T .= ", {$item} = ?";
71
            }
72
            $fields_T = substr($fields_T, 2);
73
            $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
74
            $this->add($query, $paramns);
75
            return $this;
76
        } catch (\PDOException $e) {
77
            $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...
78
        }
79
    }
80
81
    /**
82
    * @param string $fields
83
    * @param array $paramns
84
    * @return $this
85
     */
86
    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

86
    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...
87
    {
88
        try {
89
            $query = "DELETE FROM {$this->getTable()}";
90
            $this->add($query, $paramns);
91
            return $this;
92
        } catch (\PDOException $e) {
93
            $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...
94
        }
95
    }
96
97
    /**
98
    * @param string $texto
99
    * @param array $paramns
100
    * @return $this
101
     */
102
    public function query(string $texto, array $paramns = []): self
103
    {
104
        try {
105
            $this->add($texto, $paramns);
106
            return $this;
107
        } catch (\PDOException $e) {
108
            $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...
109
        }
110
    }
111
112
    /**
113
    * @param string $texto
114
    * @param array $paramns
115
    * @return $this
116
     */
117
    public function where(string $texto, array $paramns = []): self
118
    {
119
        try {
120
            $query = "WHERE {$texto} ";
121
            $this->add($query, $paramns);
122
            return $this;
123
        } catch (\PDOException $e) {
124
            $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...
125
        }
126
    }
127
128
    /**
129
    * @param string $condition
130
    * @param array $paramns
131
    * @return $this
132
     */
133
    public function andWhere(string $condition, array $paramns = []): self
134
    {
135
        try {
136
            $query = "AND {$condition} ";
137
            $this->add($query, $paramns);
138
            return $this;
139
        } catch (\PDOException $e) {
140
            $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...
141
        }
142
    }
143
144
    /**
145
    * @param string $texto
146
    * @param array $paramns
147
    * @return $this
148
     */
149
    public function orWhere(string $texto, array $paramns = []): self
150
    {
151
        try {
152
            $query = "OR {$texto} ";
153
            $this->add($query, $paramns);
154
            return $this;
155
        } catch (\PDOException $e) {
156
            $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...
157
        }
158
    }
159
160
    /**
161
    * @param string $texto
162
    * @return $this
163
     */
164
    public function orderBy(string $texto): self
165
    {
166
        try {
167
            $query = "ORDER BY {$texto} ";
168
            $this->add($query);
169
            return $this;
170
        } catch (\PDOException $e) {
171
            $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...
172
        }
173
    }
174
175
    /**
176
    * @param string $texto
177
    * @return $this
178
     */
179
    public function limit(string $texto): self
180
    {
181
        $query = "LIMIT {$texto} ";
182
        $this->add($query);
183
        return $this;
184
    }
185
186
    /**
187
    * @param string $texto
188
    * @return $this
189
     */
190
    public function offset(string $texto): self
191
    {
192
        try {
193
            $query = "OFFSET {$texto} ";
194
            $this->add($query);
195
            return $this;
196
        } catch (\PDOException $e) {
197
            $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...
198
        }
199
    }
200
201
    /**
202
    * @param string $texto
203
    * @return $this
204
     */
205
    public function groupBy(string $texto): self
206
    {
207
        try {
208
            $query = "GROUP BY {$texto} ";
209
            $this->add($query);
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
    public function having(string $texto): self
221
    {
222
        try {
223
            $query = "HAVING {$texto} ";
224
            $this->add($query);
225
            return $this;
226
        } catch (\PDOException $e) {
227
            $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...
228
        }
229
    }
230
231
    /**
232
    * @param string $table
233
    * @param string $alias
234
    * @param string $codition
235
    * @return $this
236
     */
237
    public function innerJoin(string $table, string $alias, string $codition): self
238
    {
239
        try {
240
            $query = "INNER JOIN {$table} AS {$alias} ON $codition ";
241
            $this->add($query);
242
            return $this;
243
        } catch (\PDOException $e) {
244
            $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...
245
        }
246
    }
247
248
    /**
249
    * @param string $table
250
    * @param string $alias
251
    * @param string $codition
252
    * @return $this
253
     */
254
    public function leftJoin(string $table, string $alias, string $codition): self
255
    {
256
        try {
257
            $query = "LEFT JOIN {$table} AS {$alias} ON $codition ";
258
            $this->add($query);
259
            return $this;
260
        } catch (\PDOException $e) {
261
            $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...
262
        }
263
    }
264
265
    /**
266
    * @param string $table
267
    * @param string $alias
268
    * @param string $codition
269
    * @return $this
270
     */
271
    public function rightJoin(string $table, string $alias, string $codition): self
272
    {
273
        try {
274
            $query = "RIGHT JOIN {$table} AS {$alias} ON $codition ";
275
            $this->add($query);
276
            return $this;
277
        } catch (\PDOException $e) {
278
            $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...
279
        }
280
    }
281
282
    /**
283
     * @return $this
284
     */
285
    public function execute(): self
286
    {
287
        try {
288
            $this->executeSQL($this->query, $this->params);
289
            return $this;
290
        } catch (\PDOException $e) {
291
            $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...
292
        }
293
    }
294
295
    /**
296
    * @return void
297
     */
298
    public function debug()
299
    {
300
        try {
301
            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

301
            echo $this->query . '<pre>' . /** @scrutinizer ignore-type */ print_r($this->params) . '</pre>';
Loading history...
302
            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...
303
        } catch (\PDOException $e) {
304
            $this->setError($e);
305
        }
306
    }
307
308
    /**
309
     * @return false|string
310
     */
311
    public function lastInsertId(): ?string
312
    {
313
        try {
314
            return $this->lastId();
315
        } catch (\PDOException $e) {
316
            $this->setError($e);
317
        }
318
    }
319
320
    /**
321
     * @return string|null
322
     */
323
    public function getLogSQL(): ?string
324
    {
325
        try {
326
            return $this->logSQL ?? "";
327
        } 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...
328
            $this->setError($e);
329
        }
330
    }
331
332
    private function add(string $text, array $params = [])
333
    {
334
        try {
335
            if (!empty($params))
336
                $this->params = array_merge($this->params, $params);
337
            $this->query .= $text;
338
        } catch (\PDOException $e) {
339
            $this->setError($e);
340
        }
341
    }
342
343
}
344