| Total Complexity | 51 |
| Total Lines | 396 |
| 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 | 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 | public function selectBuilder(string $fields = "*", array $paramns = []): self |
||
| 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 insertBuilder(string $fields, array $paramns): self |
||
| 67 | {
|
||
| 68 | try {
|
||
| 69 | $numparams = ''; |
||
| 70 | foreach ($paramns as $item) {
|
||
| 71 | $numparams .= ',?'; |
||
| 72 | } |
||
| 73 | $numparams = substr($numparams, 1); |
||
| 74 | $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
|
||
| 75 | $this->add($query, "main", $paramns); |
||
| 76 | return $this; |
||
| 77 | } catch (\PDOException $e) {
|
||
| 78 | $this->setError($e); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $fields |
||
| 84 | * @param array $paramns |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | protected function updateBuilder(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 deleteBuilder(): 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 | * @param $order |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | protected function orderBy(string $parameter, $order = null): self |
||
| 190 | {
|
||
| 191 | try {
|
||
| 192 | $query = "ORDER BY {$parameter} ".($order ?? 'ASC');
|
||
| 193 | $this->add($query, "orderBy"); |
||
| 194 | return $this; |
||
| 195 | } catch (\PDOException $e) {
|
||
| 196 | $this->setError($e); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $parameter |
||
| 202 | * @param $order |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | protected function addOrderBy(string $parameter, $order = null): self |
||
| 206 | {
|
||
| 207 | try {
|
||
| 208 | $query = ", {$parameter} ".($order ?? 'ASC')." ";
|
||
| 209 | $this->add($query, "addOrderBy"); |
||
| 210 | return $this; |
||
| 211 | } catch (\PDOException $e) {
|
||
| 212 | $this->setError($e); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $texto |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | protected function limit(string $texto): self |
||
| 221 | {
|
||
| 222 | $query = "LIMIT {$texto}";
|
||
| 223 | $this->add($query,"limit"); |
||
| 224 | return $this; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $texto |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | protected function offset(string $texto): self |
||
| 232 | {
|
||
| 233 | try {
|
||
| 234 | $query = "OFFSET {$texto}";
|
||
| 235 | $this->add($query,"offset"); |
||
| 236 | return $this; |
||
| 237 | } catch (\PDOException $e) {
|
||
| 238 | $this->setError($e); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $texto |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | protected function groupBy(string $texto): self |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $texto |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | protected function having(string $texto): self |
||
| 262 | {
|
||
| 263 | try {
|
||
| 264 | $query = "HAVING {$texto}";
|
||
| 265 | $this->add($query,"having"); |
||
| 266 | return $this; |
||
| 267 | } catch (\PDOException $e) {
|
||
| 268 | $this->setError($e); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $texto |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | protected function andHaving(string $texto): self |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $codition |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | protected function orHaving(string $codition): self |
||
| 292 | {
|
||
| 293 | try {
|
||
| 294 | $query = "OR {$codition}";
|
||
| 295 | $this->add($query,"orHaving"); |
||
| 296 | return $this; |
||
| 297 | } catch (\PDOException $e) {
|
||
| 298 | $this->setError($e); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $table |
||
| 304 | * @param string $alias |
||
| 305 | * @param string $codition |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | protected function innerJoin(string $table, string $alias, string $codition): self |
||
| 309 | {
|
||
| 310 | try {
|
||
| 311 | $query = "INNER JOIN {$table} AS {$alias} ON $codition";
|
||
| 312 | $this->add($query,"join"); |
||
| 313 | return $this; |
||
| 314 | } catch (\PDOException $e) {
|
||
| 315 | $this->setError($e); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $table |
||
| 321 | * @param string $alias |
||
| 322 | * @param string $codition |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | protected function leftJoin(string $table, string $alias, string $codition): self |
||
| 326 | {
|
||
| 327 | try {
|
||
| 328 | $query = "LEFT JOIN {$table} AS {$alias} ON {$codition}";
|
||
| 329 | $this->add($query,"join"); |
||
| 330 | return $this; |
||
| 331 | } catch (\PDOException $e) {
|
||
| 332 | $this->setError($e); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $table |
||
| 338 | * @param string $alias |
||
| 339 | * @param string $codition |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | protected function rightJoin(string $table, string $alias, string $codition): self |
||
| 343 | {
|
||
| 344 | try {
|
||
| 345 | $query = "RIGHT JOIN {$table} AS {$alias} ON $codition";
|
||
| 346 | $this->add($query,"join"); |
||
| 347 | return $this; |
||
| 348 | } catch (\PDOException $e) {
|
||
| 349 | $this->setError($e); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | protected function executeQuery(): self |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | protected function debug(): void |
||
| 381 | {
|
||
| 382 | try {
|
||
| 383 | echo $this->query . '<pre>' . print_r($this->params) . '</pre>'; |
||
| 384 | exit; |
||
| 385 | } catch (\PDOException $e) {
|
||
| 386 | $this->setError($e); |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $query |
||
| 393 | * @param string $type |
||
| 394 | * @param array $params |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | private function add(string $query, string $type, array $params = []): void |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | } |
||
| 415 |
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: