| Total Complexity | 45 |
| Total Lines | 325 |
| 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 | * @param string $fields |
||
| 21 | * @param array $paramns |
||
| 22 | * @return $this |
||
| 23 | */ |
||
| 24 | public function select(string $fields = "*", array $paramns = []): CrudBuilder |
||
| 25 | {
|
||
| 26 | try {
|
||
| 27 | $query = "SELECT {$fields} FROM {$this->getTable()} ";
|
||
| 28 | if (!empty($this->getTableAlias())) |
||
| 29 | $query .= "AS {$this->getTableAlias()} ";
|
||
| 30 | $this->add($query, $paramns); |
||
| 31 | return $this; |
||
| 32 | } catch (\PDOException $e) {
|
||
| 33 | $this->setError($e); |
||
|
|
|||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $fields |
||
| 39 | * @param array $paramns |
||
| 40 | * @return $this |
||
| 41 | */ |
||
| 42 | public function insert(string $fields, array $paramns): self |
||
| 43 | {
|
||
| 44 | try {
|
||
| 45 | $numparams = ''; |
||
| 46 | foreach ($paramns as $item) {
|
||
| 47 | $numparams .= ',?'; |
||
| 48 | } |
||
| 49 | $numparams = substr($numparams, 1); |
||
| 50 | $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
|
||
| 51 | $this->add($query, $paramns); |
||
| 52 | return $this; |
||
| 53 | } catch (\PDOException $e) {
|
||
| 54 | $this->setError($e); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $fields |
||
| 60 | * @param array $paramns |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function update(string $fields, array $paramns): self |
||
| 64 | {
|
||
| 65 | try {
|
||
| 66 | $fields_T = ''; |
||
| 67 | $atributos = explode(',', $fields);
|
||
| 68 | |||
| 69 | foreach ($atributos as $item) {
|
||
| 70 | $fields_T .= ", {$item} = ?";
|
||
| 71 | } |
||
| 72 | $fields_T = substr($fields_T, 2); |
||
| 73 | $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
|
||
| 74 | $this->add($query, $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 delete(string $fields, array $paramns): self |
||
| 87 | {
|
||
| 88 | try {
|
||
| 89 | $query = "DELETE FROM {$this->getTable()}";
|
||
| 90 | $this->add($query, $paramns); |
||
| 91 | return $this; |
||
| 92 | } catch (\PDOException $e) {
|
||
| 93 | $this->setError($e); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $texto |
||
| 99 | * @param array $paramns |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function query(string $texto, array $paramns = []): self |
||
| 103 | {
|
||
| 104 | try {
|
||
| 105 | $this->add($texto, $paramns); |
||
| 106 | return $this; |
||
| 107 | } catch (\PDOException $e) {
|
||
| 108 | $this->setError($e); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $texto |
||
| 114 | * @param array $paramns |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function where(string $texto, array $paramns = []): self |
||
| 118 | {
|
||
| 119 | try {
|
||
| 120 | $query = "WHERE {$texto} ";
|
||
| 121 | $this->add($query, $paramns); |
||
| 122 | return $this; |
||
| 123 | } catch (\PDOException $e) {
|
||
| 124 | $this->setError($e); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $condition |
||
| 130 | * @param array $paramns |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function andWhere(string $condition, array $paramns = []): self |
||
| 134 | {
|
||
| 135 | try {
|
||
| 136 | $query = "AND {$condition} ";
|
||
| 137 | $this->add($query, $paramns); |
||
| 138 | return $this; |
||
| 139 | } catch (\PDOException $e) {
|
||
| 140 | $this->setError($e); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $texto |
||
| 146 | * @param array $paramns |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function orWhere(string $texto, array $paramns = []): self |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $texto |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function orderBy(string $texto): self |
||
| 165 | {
|
||
| 166 | try {
|
||
| 167 | $query = "ORDER BY {$texto} ";
|
||
| 168 | $this->add($query); |
||
| 169 | return $this; |
||
| 170 | } catch (\PDOException $e) {
|
||
| 171 | $this->setError($e); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $texto |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function limit(string $texto): self |
||
| 180 | {
|
||
| 181 | $query = "LIMIT {$texto} ";
|
||
| 182 | $this->add($query); |
||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $texto |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function offset(string $texto): self |
||
| 191 | {
|
||
| 192 | try {
|
||
| 193 | $query = "OFFSET {$texto} ";
|
||
| 194 | $this->add($query); |
||
| 195 | return $this; |
||
| 196 | } catch (\PDOException $e) {
|
||
| 197 | $this->setError($e); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $texto |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function groupBy(string $texto): self |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $texto |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function having(string $texto): self |
||
| 221 | {
|
||
| 222 | try {
|
||
| 223 | $query = "HAVING {$texto} ";
|
||
| 224 | $this->add($query); |
||
| 225 | return $this; |
||
| 226 | } catch (\PDOException $e) {
|
||
| 227 | $this->setError($e); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $table |
||
| 233 | * @param string $alias |
||
| 234 | * @param string $codition |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function innerJoin(string $table, string $alias, string $codition): self |
||
| 238 | {
|
||
| 239 | try {
|
||
| 240 | $query = "INNER JOIN {$table} AS {$alias} ON $codition ";
|
||
| 241 | $this->add($query); |
||
| 242 | return $this; |
||
| 243 | } catch (\PDOException $e) {
|
||
| 244 | $this->setError($e); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $table |
||
| 250 | * @param string $alias |
||
| 251 | * @param string $codition |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function leftJoin(string $table, string $alias, string $codition): self |
||
| 255 | {
|
||
| 256 | try {
|
||
| 257 | $query = "LEFT JOIN {$table} AS {$alias} ON $codition ";
|
||
| 258 | $this->add($query); |
||
| 259 | return $this; |
||
| 260 | } catch (\PDOException $e) {
|
||
| 261 | $this->setError($e); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $table |
||
| 267 | * @param string $alias |
||
| 268 | * @param string $codition |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function rightJoin(string $table, string $alias, string $codition): self |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function execute(): self |
||
| 286 | {
|
||
| 287 | try {
|
||
| 288 | $this->executeSQL($this->query, $this->params); |
||
| 289 | return $this; |
||
| 290 | } catch (\PDOException $e) {
|
||
| 291 | $this->setError($e); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public function debug() |
||
| 299 | {
|
||
| 300 | try {
|
||
| 301 | echo $this->query . '<pre>' . print_r($this->params) . '</pre>'; |
||
| 302 | exit; |
||
| 303 | } catch (\PDOException $e) {
|
||
| 304 | $this->setError($e); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return false|string |
||
| 310 | */ |
||
| 311 | public function lastInsertId(): ?string |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return string|null |
||
| 322 | */ |
||
| 323 | public function getLogSQL(): ?string |
||
| 324 | {
|
||
| 325 | try {
|
||
| 326 | return $this->logSQL ?? ""; |
||
| 327 | } catch (\PDOException $e) {
|
||
| 328 | $this->setError($e); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | private function add(string $text, array $params = []) |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | } |
||
| 344 |
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: