Total Complexity | 54 |
Total Lines | 411 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CrudBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CrudBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class CrudBuilder |
||
16 | { |
||
17 | use DatalayerTrait; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private array $sqlPartsSelect = [ |
||
23 | 'main' => [], |
||
24 | 'join' => [], |
||
25 | 'where' => "", |
||
26 | 'andWhere' => [], |
||
27 | 'orWhere' => [], |
||
28 | 'groupBy' => [], |
||
29 | 'having' => [], |
||
30 | 'andHaving' => [], |
||
31 | 'orHaving' => [], |
||
32 | 'orderBy' => "", |
||
33 | 'addOrderBy'=> [], |
||
34 | 'limit' => "", |
||
35 | 'offset' => "", |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * <code> |
||
41 | * $qb = $this->select('u.id, p.id') |
||
42 | * ->where('phonenumbers=?', [$number]); |
||
43 | * </code> |
||
44 | * @param string $fields |
||
45 | * @param array $paramns |
||
46 | * @return $this |
||
47 | */ |
||
48 | protected function select(string $fields = "*", array $paramns = []): CrudBuilder |
||
49 | { |
||
50 | try { |
||
51 | $query = "SELECT {$fields} FROM {$this->getTable()}"; |
||
52 | if (!empty($this->getTableAlias())) |
||
53 | $query .= " AS {$this->getTableAlias()}"; |
||
54 | $this->add($query, "main", $paramns); |
||
55 | return $this; |
||
56 | } catch (\PDOException $e) { |
||
57 | $this->setError($e); |
||
|
|||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $fields |
||
63 | * @param array $paramns |
||
64 | * @return $this |
||
65 | */ |
||
66 | protected function insert(string $fields, array $paramns): self |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param string $fields |
||
84 | * @param array $paramns |
||
85 | * @return $this |
||
86 | */ |
||
87 | protected function update(string $fields, array $paramns): self |
||
88 | { |
||
89 | try { |
||
90 | $fields_T = ''; |
||
91 | $atributos = explode(',', $fields); |
||
92 | |||
93 | foreach ($atributos as $item) { |
||
94 | $fields_T .= ", {$item} = ?"; |
||
95 | } |
||
96 | $fields_T = substr($fields_T, 2); |
||
97 | $query = "UPDATE {{$this->getTable()}} SET {$fields_T}"; |
||
98 | $this->add($query, "main", $paramns); |
||
99 | return $this; |
||
100 | } catch (\PDOException $e) { |
||
101 | $this->setError($e); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param string $fields |
||
107 | * @param array $paramns |
||
108 | * @return $this |
||
109 | */ |
||
110 | protected function delete(): self |
||
111 | { |
||
112 | try { |
||
113 | $query = "DELETE FROM {$this->getTable()}"; |
||
114 | $this->add($query, "main"); |
||
115 | return $this; |
||
116 | } catch (\PDOException $e) { |
||
117 | $this->setError($e); |
||
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); |
||
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); |
||
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); |
||
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 |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param string $parameter |
||
186 | * @return $this |
||
187 | */ |
||
188 | protected function orderBy(string $parameter, $order = null): self |
||
189 | { |
||
190 | try { |
||
191 | $query = "ORDER BY {$parameter} ".($order ?? 'ASC'); |
||
192 | $this->add($query, "orderBy"); |
||
193 | return $this; |
||
194 | } catch (\PDOException $e) { |
||
195 | $this->setError($e); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | protected function addOrderBy(string $parameter, $order = null): self |
||
200 | { |
||
201 | try { |
||
202 | $query = ", {$parameter} ".($order ?? 'ASC')." "; |
||
203 | $this->add($query, "addOrderBy"); |
||
204 | return $this; |
||
205 | } catch (\PDOException $e) { |
||
206 | $this->setError($e); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string $texto |
||
212 | * @return $this |
||
213 | */ |
||
214 | protected function limit(string $texto): self |
||
215 | { |
||
216 | $query = "LIMIT {$texto}"; |
||
217 | $this->add($query,"limit"); |
||
218 | return $this; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param string $texto |
||
223 | * @return $this |
||
224 | */ |
||
225 | protected function offset(string $texto): self |
||
226 | { |
||
227 | try { |
||
228 | $query = "OFFSET {$texto}"; |
||
229 | $this->add($query,"offset"); |
||
230 | return $this; |
||
231 | } catch (\PDOException $e) { |
||
232 | $this->setError($e); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param string $texto |
||
238 | * @return $this |
||
239 | */ |
||
240 | protected function groupBy(string $texto): self |
||
248 | } |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param string $texto |
||
253 | * @return $this |
||
254 | */ |
||
255 | protected function having(string $texto): self |
||
256 | { |
||
257 | try { |
||
258 | $query = "HAVING {$texto}"; |
||
259 | $this->add($query,"having"); |
||
260 | return $this; |
||
261 | } catch (\PDOException $e) { |
||
262 | $this->setError($e); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param string $texto |
||
268 | * @return $this |
||
269 | */ |
||
270 | protected function andHaving(string $texto): self |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param string $codition |
||
283 | * @return $this |
||
284 | */ |
||
285 | protected function orHaving(string $codition): self |
||
286 | { |
||
287 | try { |
||
288 | $query = "OR {$codition}"; |
||
289 | $this->add($query,"orHaving"); |
||
290 | return $this; |
||
291 | } catch (\PDOException $e) { |
||
292 | $this->setError($e); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @param string $table |
||
298 | * @param string $alias |
||
299 | * @param string $codition |
||
300 | * @return $this |
||
301 | */ |
||
302 | protected function innerJoin(string $table, string $alias, string $codition): self |
||
303 | { |
||
304 | try { |
||
305 | $query = "INNER JOIN {$table} AS {$alias} ON $codition"; |
||
306 | $this->add($query,"join"); |
||
307 | return $this; |
||
308 | } catch (\PDOException $e) { |
||
309 | $this->setError($e); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param string $table |
||
315 | * @param string $alias |
||
316 | * @param string $codition |
||
317 | * @return $this |
||
318 | */ |
||
319 | protected function leftJoin(string $table, string $alias, string $codition): self |
||
320 | { |
||
321 | try { |
||
322 | $query = "LEFT JOIN {$table} AS {$alias} ON {$codition}"; |
||
323 | $this->add($query,"join"); |
||
324 | return $this; |
||
325 | } catch (\PDOException $e) { |
||
326 | $this->setError($e); |
||
327 | } |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @param string $table |
||
332 | * @param string $alias |
||
333 | * @param string $codition |
||
334 | * @return $this |
||
335 | */ |
||
336 | protected function rightJoin(string $table, string $alias, string $codition): self |
||
337 | { |
||
338 | try { |
||
339 | $query = "RIGHT JOIN {$table} AS {$alias} ON $codition"; |
||
340 | $this->add($query,"join"); |
||
341 | return $this; |
||
342 | } catch (\PDOException $e) { |
||
343 | $this->setError($e); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return $this |
||
349 | */ |
||
350 | protected function executeQuery(): self |
||
368 | } |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return void |
||
373 | */ |
||
374 | protected function debug() |
||
375 | { |
||
376 | try { |
||
377 | echo $this->query . '<pre>' . print_r($this->params) . '</pre>'; |
||
378 | exit; |
||
379 | } catch (\PDOException $e) { |
||
380 | $this->setError($e); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @return false|string |
||
386 | */ |
||
387 | protected function lastInsertId(): ?string |
||
393 | } |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param $params |
||
398 | * @return self |
||
399 | */ |
||
400 | protected function setParameter($params): self |
||
401 | { |
||
402 | $this->params = array_merge($this->params, $params); |
||
403 | return $this; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @param string $query |
||
408 | * @param string $type |
||
409 | * @param array $params |
||
410 | * @return void |
||
411 | */ |
||
412 | private function add(string $query, string $type, array $params = []): void |
||
430 |
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: