| Total Complexity | 56 |
| Total Lines | 412 |
| 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 | 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 | * |
||
| 39 | * <code> |
||
| 40 | * $qb = $this->select('u.id, p.id')
|
||
| 41 | * ->where('phonenumbers=?', [$number]);
|
||
| 42 | * </code> |
||
| 43 | * @param string $fields |
||
| 44 | * @param array $paramns |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | public function select(string $fields = "*", array $paramns = []): CrudBuilder |
||
| 48 | {
|
||
| 49 | try {
|
||
| 50 | $query = "SELECT {$fields} FROM {$this->getTable()}";
|
||
| 51 | if (!empty($this->getTableAlias())) |
||
| 52 | $query .= " AS {$this->getTableAlias()}";
|
||
| 53 | $this->add($query, "main", $paramns); |
||
| 54 | return $this; |
||
| 55 | } catch (\PDOException $e) {
|
||
| 56 | $this->setError($e); |
||
|
|
|||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $fields |
||
| 62 | * @param array $paramns |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | public function insert(string $fields, array $paramns): self |
||
| 66 | {
|
||
| 67 | try {
|
||
| 68 | $numparams = ''; |
||
| 69 | foreach ($paramns as $item) {
|
||
| 70 | $numparams .= ',?'; |
||
| 71 | } |
||
| 72 | $numparams = substr($numparams, 1); |
||
| 73 | $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
|
||
| 74 | $this->add($query, "main", $paramns); |
||
| 75 | return $this; |
||
| 76 | } catch (\PDOException $e) {
|
||
| 77 | $this->setError($e); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $fields |
||
| 83 | * @param array $paramns |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function update(string $fields, array $paramns): self |
||
| 87 | {
|
||
| 88 | try {
|
||
| 89 | $fields_T = ''; |
||
| 90 | $atributos = explode(',', $fields);
|
||
| 91 | |||
| 92 | foreach ($atributos as $item) {
|
||
| 93 | $fields_T .= ", {$item} = ?";
|
||
| 94 | } |
||
| 95 | $fields_T = substr($fields_T, 2); |
||
| 96 | $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
|
||
| 97 | $this->add($query, "main", $paramns); |
||
| 98 | return $this; |
||
| 99 | } catch (\PDOException $e) {
|
||
| 100 | $this->setError($e); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $fields |
||
| 106 | * @param array $paramns |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function delete(): self |
||
| 110 | {
|
||
| 111 | try {
|
||
| 112 | $query = "DELETE FROM {$this->getTable()}";
|
||
| 113 | $this->add($query, "main"); |
||
| 114 | return $this; |
||
| 115 | } catch (\PDOException $e) {
|
||
| 116 | $this->setError($e); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $query |
||
| 122 | * @param array $paramns |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | public function query(string $query, array $paramns = []): self |
||
| 126 | {
|
||
| 127 | try {
|
||
| 128 | $this->add($query, "main", $paramns); |
||
| 129 | return $this; |
||
| 130 | } catch (\PDOException $e) {
|
||
| 131 | $this->setError($e); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $texto |
||
| 137 | * @param array $paramns |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function where(string $texto, array $paramns = []): self |
||
| 141 | {
|
||
| 142 | try {
|
||
| 143 | $query = "WHERE {$texto}";
|
||
| 144 | $this->add($query, "where", $paramns); |
||
| 145 | return $this; |
||
| 146 | } catch (\PDOException $e) {
|
||
| 147 | $this->setError($e); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $condition |
||
| 153 | * @param array $paramns |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function andWhere(string $condition, array $paramns = []): self |
||
| 157 | {
|
||
| 158 | try {
|
||
| 159 | $query = "AND {$condition}";
|
||
| 160 | $this->add($query, "andWhere", $paramns); |
||
| 161 | return $this; |
||
| 162 | } catch (\PDOException $e) {
|
||
| 163 | $this->setError($e); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $condition |
||
| 169 | * @param array $paramns |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function orWhere(string $condition, array $paramns = []): self |
||
| 173 | {
|
||
| 174 | try {
|
||
| 175 | $query = "OR {$condition}";
|
||
| 176 | $this->add($query, "orWhere", $paramns); |
||
| 177 | return $this; |
||
| 178 | } catch (\PDOException $e) {
|
||
| 179 | $this->setError($e); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $parameter |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function orderBy(string $parameter, $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); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | public function addOrderBy(string $parameter, $order = null): self |
||
| 199 | {
|
||
| 200 | try {
|
||
| 201 | $query = ", {$parameter} ".($order ?? 'ASC')." ";
|
||
| 202 | $this->add($query, "addOrderBy"); |
||
| 203 | return $this; |
||
| 204 | } catch (\PDOException $e) {
|
||
| 205 | $this->setError($e); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $texto |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function limit(string $texto): self |
||
| 214 | {
|
||
| 215 | $query = "LIMIT {$texto}";
|
||
| 216 | $this->add($query,"limit"); |
||
| 217 | return $this; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $texto |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function offset(string $texto): self |
||
| 225 | {
|
||
| 226 | try {
|
||
| 227 | $query = "OFFSET {$texto}";
|
||
| 228 | $this->add($query,"offset"); |
||
| 229 | return $this; |
||
| 230 | } catch (\PDOException $e) {
|
||
| 231 | $this->setError($e); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $texto |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function groupBy(string $texto): self |
||
| 240 | {
|
||
| 241 | try {
|
||
| 242 | $query = "GROUP BY {$texto}";
|
||
| 243 | $this->add($query,"groupBy"); |
||
| 244 | return $this; |
||
| 245 | } catch (\PDOException $e) {
|
||
| 246 | $this->setError($e); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $texto |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function having(string $texto): self |
||
| 255 | {
|
||
| 256 | try {
|
||
| 257 | $query = "HAVING {$texto}";
|
||
| 258 | $this->add($query,"having"); |
||
| 259 | return $this; |
||
| 260 | } catch (\PDOException $e) {
|
||
| 261 | $this->setError($e); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $texto |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function andHaving(string $texto): self |
||
| 270 | {
|
||
| 271 | try {
|
||
| 272 | $query = "AND {$texto}";
|
||
| 273 | $this->add($query,"andHaving"); |
||
| 274 | return $this; |
||
| 275 | } catch (\PDOException $e) {
|
||
| 276 | $this->setError($e); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $codition |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function orHaving(string $codition): self |
||
| 285 | {
|
||
| 286 | try {
|
||
| 287 | $query = "OR {$codition}";
|
||
| 288 | $this->add($query,"orHaving"); |
||
| 289 | return $this; |
||
| 290 | } catch (\PDOException $e) {
|
||
| 291 | $this->setError($e); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $table |
||
| 297 | * @param string $alias |
||
| 298 | * @param string $codition |
||
| 299 | * @return $this |
||
| 300 | */ |
||
| 301 | public function innerJoin(string $table, string $alias, string $codition): self |
||
| 302 | {
|
||
| 303 | try {
|
||
| 304 | $query = "INNER JOIN {$table} AS {$alias} ON $codition";
|
||
| 305 | $this->add($query,"join"); |
||
| 306 | return $this; |
||
| 307 | } catch (\PDOException $e) {
|
||
| 308 | $this->setError($e); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $table |
||
| 314 | * @param string $alias |
||
| 315 | * @param string $codition |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function leftJoin(string $table, string $alias, string $codition): self |
||
| 319 | {
|
||
| 320 | try {
|
||
| 321 | $query = "LEFT JOIN {$table} AS {$alias} ON {$codition}";
|
||
| 322 | $this->add($query,"join"); |
||
| 323 | return $this; |
||
| 324 | } catch (\PDOException $e) {
|
||
| 325 | $this->setError($e); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $table |
||
| 331 | * @param string $alias |
||
| 332 | * @param string $codition |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function rightJoin(string $table, string $alias, string $codition): self |
||
| 336 | {
|
||
| 337 | try {
|
||
| 338 | $query = "RIGHT JOIN {$table} AS {$alias} ON $codition";
|
||
| 339 | $this->add($query,"join"); |
||
| 340 | return $this; |
||
| 341 | } catch (\PDOException $e) {
|
||
| 342 | $this->setError($e); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function executeQuery(): self |
||
| 350 | {
|
||
| 351 | try {
|
||
| 352 | foreach ($this->sqlPartsSelect as $key => $part){
|
||
| 353 | if (is_array($part)) {
|
||
| 354 | foreach ($part as $item){
|
||
| 355 | $this->query .= $item; |
||
| 356 | } |
||
| 357 | } else {
|
||
| 358 | $this->query .= $part; |
||
| 359 | } |
||
| 360 | |||
| 361 | } |
||
| 362 | |||
| 363 | $this->executeSQL($this->query, $this->params); |
||
| 364 | return $this; |
||
| 365 | } catch (\PDOException $e) {
|
||
| 366 | $this->setError($e); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function debug() |
||
| 374 | {
|
||
| 375 | try {
|
||
| 376 | echo $this->query . '<pre>' . print_r($this->params) . '</pre>'; |
||
| 377 | exit; |
||
| 378 | } catch (\PDOException $e) {
|
||
| 379 | $this->setError($e); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return false|string |
||
| 385 | */ |
||
| 386 | public function lastInsertId(): ?string |
||
| 387 | {
|
||
| 388 | try {
|
||
| 389 | return $this->lastId(); |
||
| 390 | } catch (\PDOException $e) {
|
||
| 391 | $this->setError($e); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return string|null |
||
| 397 | */ |
||
| 398 | protected function getSQL(): ?string |
||
| 399 | {
|
||
| 400 | try {
|
||
| 401 | return $this->logSQL ?? ""; |
||
| 402 | } catch (\PDOException $e) {
|
||
| 403 | $this->setError($e); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | public function setParameter($params): self |
||
| 411 | } |
||
| 412 | |||
| 413 | private function add(string $text, string $type, array $params = []) |
||
| 414 | {
|
||
| 415 | $text = $text." "; |
||
| 416 | try {
|
||
| 417 | if (is_array($this->sqlPartsSelect[$type])) {
|
||
| 418 | $this->sqlPartsSelect[$type][] = $text; |
||
| 419 | } else {
|
||
| 420 | $this->sqlPartsSelect[$type] = $text; |
||
| 421 | } |
||
| 422 | |||
| 423 | if (!empty($params)) |
||
| 424 | $this->params = array_merge($this->params, $params); |
||
| 425 | } catch (\PDOException $e) {
|
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | } |
||
| 431 |
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: