| Total Complexity | 41 |
| Total Lines | 300 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Crud 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 Crud, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class Crud |
||
| 19 | {
|
||
| 20 | use DatalayerTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param string $fields |
||
| 24 | * @param string $add |
||
| 25 | * @param array|null $values |
||
| 26 | * @param bool $returnModel |
||
| 27 | * @param bool $debug |
||
| 28 | * @return array|false|void|null |
||
| 29 | * @throws DatabaseException |
||
| 30 | */ |
||
| 31 | public function select(string $fields = '*', string $add = '', ?array $values = null, bool $returnModel = false, bool $debug = false) |
||
| 32 | {
|
||
| 33 | try {
|
||
| 34 | if (strlen($add) > 0) {
|
||
| 35 | $add = ' ' . $add; |
||
| 36 | } |
||
| 37 | |||
| 38 | $sql = "SELECT {$fields} FROM {$this->getTableName()}";
|
||
| 39 | |||
| 40 | if (!empty($this->getTableAlias())) |
||
| 41 | $sql .= " AS {$this->getTableAlias()}";
|
||
| 42 | |||
| 43 | $sql .= $add; |
||
| 44 | |||
| 45 | if ($debug) {
|
||
| 46 | echo $sql; |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | $this->executeSQL($sql, $values); |
||
| 50 | if ($returnModel) {
|
||
| 51 | return $this->fetchArrayClass(); |
||
| 52 | } else {
|
||
| 53 | return $this->fetchArrayObj(); |
||
| 54 | } |
||
| 55 | } catch (PDOException $e) {
|
||
| 56 | throw new DatabaseException( |
||
| 57 | "Query failed: {$e->getMessage()}",
|
||
| 58 | $e->getCode(), |
||
| 59 | $e, |
||
| 60 | $sql ?? '', |
||
| 61 | $values |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $fields |
||
| 68 | * @param array|null $values |
||
| 69 | * @param bool $debug |
||
| 70 | * @return bool|void |
||
| 71 | * @throws DatabaseException |
||
| 72 | */ |
||
| 73 | public function insert(string $fields, ?array $values = null, bool $debug = false) |
||
| 74 | {
|
||
| 75 | try {
|
||
| 76 | $numparams = ''; |
||
| 77 | foreach ($values as $item) {
|
||
| 78 | $numparams .= ',?'; |
||
| 79 | } |
||
| 80 | $numparams = substr($numparams, 1); |
||
| 81 | $sql = "INSERT INTO {$this->getTableName()} ({$fields}) VALUES ({$numparams})";
|
||
| 82 | if ($debug) {
|
||
| 83 | echo $sql . '<pre>' . print_r($values) . '</pre>'; |
||
|
|
|||
| 84 | return; |
||
| 85 | } |
||
| 86 | $result = $this->executeSQL($sql, $values); |
||
| 87 | return !empty($result); |
||
| 88 | } catch (PDOException $e) {
|
||
| 89 | throw new DatabaseException( |
||
| 90 | "Insert failed: {$e->getMessage()}",
|
||
| 91 | $e->getCode(), |
||
| 92 | $e, |
||
| 93 | $sql ?? '', |
||
| 94 | $values |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param object $object |
||
| 101 | * @return bool|null |
||
| 102 | * @throws DatabaseException |
||
| 103 | */ |
||
| 104 | public function insertObject(object $object) |
||
| 105 | {
|
||
| 106 | try {
|
||
| 107 | $args = []; |
||
| 108 | $params = []; |
||
| 109 | |||
| 110 | // Filtrar propriedades do objeto que não são null |
||
| 111 | foreach ($object as $chave => $valor) {
|
||
| 112 | if ($valor !== null) { // Verifica explicitamente se o valor não é null
|
||
| 113 | $args[] = $chave; |
||
| 114 | $params[] = $valor; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | // Se houver colunas a serem inseridas |
||
| 119 | if (!empty($args)) {
|
||
| 120 | $columns = implode(',', $args);
|
||
| 121 | return $this->insert($columns, $params); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Retorna falso se todos os valores forem null |
||
| 125 | return false; |
||
| 126 | } catch (PDOException $e) {
|
||
| 127 | throw new DatabaseException( |
||
| 128 | "Insert object failed: {$e->getMessage()}",
|
||
| 129 | $e->getCode(), |
||
| 130 | $e |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param array $object |
||
| 137 | * @return bool |
||
| 138 | * @throws DatabaseException |
||
| 139 | */ |
||
| 140 | public function insertArray(array $object) |
||
| 141 | {
|
||
| 142 | try {
|
||
| 143 | $args = []; |
||
| 144 | $params = []; |
||
| 145 | foreach ($object as $chave => $valor) {
|
||
| 146 | if ($valor !== null) {
|
||
| 147 | $args[] = $chave; |
||
| 148 | $params[] = $valor; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | if (!empty($args)) {
|
||
| 153 | $args = implode(',', $args);
|
||
| 154 | return $this->insert($args, $params); |
||
| 155 | } |
||
| 156 | |||
| 157 | return false; |
||
| 158 | } catch (PDOException $e) {
|
||
| 159 | throw new DatabaseException( |
||
| 160 | "Insert array failed: {$e->getMessage()}",
|
||
| 161 | $e->getCode(), |
||
| 162 | $e |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $fields |
||
| 169 | * @param array|null $values |
||
| 170 | * @param string|null $where |
||
| 171 | * @param bool $debug |
||
| 172 | * @return bool|void |
||
| 173 | * @throws DatabaseException |
||
| 174 | */ |
||
| 175 | public function update(string $fields, ?array $values = null, ?string $where = null, bool $debug = false) |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param object $object |
||
| 208 | * @param string $where |
||
| 209 | * @return bool|null |
||
| 210 | * @throws DatabaseException |
||
| 211 | */ |
||
| 212 | public function updateObject(object $object, string $where) |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $object |
||
| 241 | * @param string $where |
||
| 242 | * @return bool |
||
| 243 | * @throws DatabaseException |
||
| 244 | */ |
||
| 245 | public function updateArray(array $object, string $where) |
||
| 246 | {
|
||
| 247 | try {
|
||
| 248 | $args = []; |
||
| 249 | $params = []; |
||
| 250 | |||
| 251 | foreach ($object as $chave => $valor) {
|
||
| 252 | if ($valor !== null) {
|
||
| 253 | $args[] = $chave; |
||
| 254 | $params[] = $valor; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | if (!empty($args)) {
|
||
| 259 | $args = implode(',', $args);
|
||
| 260 | return $this->update($args, $params, $where); |
||
| 261 | } |
||
| 262 | |||
| 263 | return false; |
||
| 264 | } catch (PDOException $e) {
|
||
| 265 | throw new DatabaseException( |
||
| 266 | "Update array failed: {$e->getMessage()}",
|
||
| 267 | $e->getCode(), |
||
| 268 | $e |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param array|null $values |
||
| 275 | * @param string|null $where |
||
| 276 | * @param bool $debug |
||
| 277 | * @return bool|void |
||
| 278 | * @throws DatabaseException |
||
| 279 | */ |
||
| 280 | public function delete(?array $values = null, ?string $where = null, bool $debug = false) |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return false|string |
||
| 306 | */ |
||
| 307 | public function lastInsertId(): ?string |
||
| 308 | {
|
||
| 309 | return $this->lastId(); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string|null |
||
| 314 | */ |
||
| 315 | public function getLogSQL(): ?string |
||
| 318 | } |
||
| 319 | } |