| Total Complexity | 54 |
| Total Lines | 399 |
| 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 | 'limit' => "", |
||
| 32 | 'offset' => "", |
||
| 33 | |||
| 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 | public function select(string $fields = "*", array $paramns = []): CrudBuilder |
||
| 47 | {
|
||
| 48 | try {
|
||
| 49 | $query = "SELECT {$fields} FROM {$this->getTable()} ";
|
||
| 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 | public function insert(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->getTable()} ({$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 | public function update(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->getTable()}} 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 | public function delete(string $fields, array $paramns): self |
||
| 109 | {
|
||
| 110 | try {
|
||
| 111 | $query = "DELETE FROM {$this->getTable()}";
|
||
| 112 | $this->add($query, "main", $paramns); |
||
| 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 | public 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 | public 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 | public 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 $texto |
||
| 168 | * @param array $paramns |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function orWhere(string $texto, array $paramns = []): self |
||
| 172 | {
|
||
| 173 | try {
|
||
| 174 | $query = "OR {$texto} ";
|
||
| 175 | $this->add($query, "orWhere", $paramns); |
||
| 176 | return $this; |
||
| 177 | } catch (\PDOException $e) {
|
||
| 178 | $this->setError($e); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $texto |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function orderBy(string $texto, $order = null): self |
||
| 187 | {
|
||
| 188 | try {
|
||
| 189 | $query = "ORDER BY {$texto} ($order ?? 'ASC')";
|
||
| 190 | $this->add($query, "orderBy"); |
||
| 191 | return $this; |
||
| 192 | } catch (\PDOException $e) {
|
||
| 193 | $this->setError($e); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $texto |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function limit(string $texto): self |
||
| 202 | {
|
||
| 203 | $query = "LIMIT {$texto} ";
|
||
| 204 | $this->add($query,"limit"); |
||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $texto |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | public function offset(string $texto): self |
||
| 213 | {
|
||
| 214 | try {
|
||
| 215 | $query = "OFFSET {$texto} ";
|
||
| 216 | $this->add($query,"offset"); |
||
| 217 | return $this; |
||
| 218 | } catch (\PDOException $e) {
|
||
| 219 | $this->setError($e); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $texto |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function groupBy(string $texto): self |
||
| 228 | {
|
||
| 229 | try {
|
||
| 230 | $query = "GROUP BY {$texto} ";
|
||
| 231 | $this->add($query,"groupBy"); |
||
| 232 | return $this; |
||
| 233 | } catch (\PDOException $e) {
|
||
| 234 | $this->setError($e); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $texto |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function having(string $texto): self |
||
| 243 | {
|
||
| 244 | try {
|
||
| 245 | $query = "HAVING {$texto} ";
|
||
| 246 | $this->add($query,"having"); |
||
| 247 | return $this; |
||
| 248 | } catch (\PDOException $e) {
|
||
| 249 | $this->setError($e); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $texto |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function andHaving(string $texto): self |
||
| 258 | {
|
||
| 259 | try {
|
||
| 260 | $query = "AND {$texto} ";
|
||
| 261 | $this->add($query,"andHaving"); |
||
| 262 | return $this; |
||
| 263 | } catch (\PDOException $e) {
|
||
| 264 | $this->setError($e); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $codition |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function orHaving(string $codition): self |
||
| 273 | {
|
||
| 274 | try {
|
||
| 275 | $query = "OR {$codition} ";
|
||
| 276 | $this->add($query,"orHaving"); |
||
| 277 | return $this; |
||
| 278 | } catch (\PDOException $e) {
|
||
| 279 | $this->setError($e); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $table |
||
| 285 | * @param string $alias |
||
| 286 | * @param string $codition |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function innerJoin(string $table, string $alias, string $codition): self |
||
| 290 | {
|
||
| 291 | try {
|
||
| 292 | $query = "INNER JOIN {$table} AS {$alias} ON $codition ";
|
||
| 293 | $this->add($query,"join"); |
||
| 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 | public function leftJoin(string $table, string $alias, string $codition): self |
||
| 307 | {
|
||
| 308 | try {
|
||
| 309 | $query = "LEFT 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 | public function rightJoin(string $table, string $alias, string $codition): self |
||
| 324 | {
|
||
| 325 | try {
|
||
| 326 | $query = "RIGHT 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 | * @return $this |
||
| 336 | */ |
||
| 337 | public function executeQuery(): self |
||
| 338 | {
|
||
| 339 | try {
|
||
| 340 | foreach ($this->sqlPartsSelect as $key => $part){
|
||
| 341 | if (is_array($part)) {
|
||
| 342 | foreach ($part as $item){
|
||
| 343 | $this->query .= $item; |
||
| 344 | } |
||
| 345 | } else {
|
||
| 346 | $this->query .= $part; |
||
| 347 | } |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | $this->executeSQL($this->query, $this->params); |
||
| 352 | return $this; |
||
| 353 | } catch (\PDOException $e) {
|
||
| 354 | $this->setError($e); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function debug() |
||
| 362 | {
|
||
| 363 | try {
|
||
| 364 | echo $this->query . '<pre>' . print_r($this->params) . '</pre>'; |
||
| 365 | exit; |
||
| 366 | } catch (\PDOException $e) {
|
||
| 367 | $this->setError($e); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return false|string |
||
| 373 | */ |
||
| 374 | public function lastInsertId(): ?string |
||
| 375 | {
|
||
| 376 | try {
|
||
| 377 | return $this->lastId(); |
||
| 378 | } catch (\PDOException $e) {
|
||
| 379 | $this->setError($e); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return string|null |
||
| 385 | */ |
||
| 386 | protected function getSQL(): ?string |
||
| 387 | {
|
||
| 388 | try {
|
||
| 389 | return $this->logSQL ?? ""; |
||
| 390 | } catch (\PDOException $e) {
|
||
| 391 | $this->setError($e); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | public function setParameter($params): self |
||
| 399 | } |
||
| 400 | |||
| 401 | private function add(string $text, string $type, array $params = []) |
||
| 402 | {
|
||
| 403 | try {
|
||
| 404 | if (is_array($this->sqlPartsSelect[$type])) {
|
||
| 405 | $this->sqlPartsSelect[$type][] = $text; |
||
| 406 | } else {
|
||
| 407 | $this->sqlPartsSelect[$type] = $text; |
||
| 408 | } |
||
| 409 | |||
| 410 | if (!empty($params)) |
||
| 411 | $this->params = array_merge($this->params, $params); |
||
| 412 | } catch (\PDOException $e) {
|
||
| 413 | $this->setError($e); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | } |
||
| 418 |
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: