Total Complexity | 51 |
Total Lines | 394 |
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 | 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); |
||
|
|||
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); |
||
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); |
||
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); |
||
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); |
||
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); |
||
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); |
||
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 |
||
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 |
||
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); |
||
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); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $texto |
||
242 | * @return $this |
||
243 | */ |
||
244 | protected function groupBy(string $texto): self |
||
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); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param string $texto |
||
272 | * @return $this |
||
273 | */ |
||
274 | protected function andHaving(string $texto): self |
||
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); |
||
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); |
||
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); |
||
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); |
||
348 | } |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @return $this |
||
353 | */ |
||
354 | protected function executeQuery(): self |
||
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>'; |
||
382 | exit; |
||
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 |
||
409 | } |
||
410 | } |
||
411 | |||
412 | } |
||
413 |
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: