| Total Complexity | 71 |
| Total Lines | 444 |
| Duplicated Lines | 0 % |
| Changes | 27 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DatalayerTrait 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 DatalayerTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | trait DatalayerTrait |
||
| 20 | { |
||
| 21 | /** @var PDO|null |
||
| 22 | * @deprecated |
||
| 23 | * */ |
||
| 24 | protected $instance = null; |
||
| 25 | |||
| 26 | /** @var string |
||
| 27 | * @deprecated |
||
| 28 | */ |
||
| 29 | protected string $fields; |
||
| 30 | |||
| 31 | /** @var PDOStatement|null |
||
| 32 | * @deprecated */ |
||
| 33 | protected $prepare = null; |
||
| 34 | |||
| 35 | /** @var string |
||
| 36 | * @deprecated |
||
| 37 | */ |
||
| 38 | protected $database = CONFIG_DATA_LAYER["dbname"]; |
||
| 39 | |||
| 40 | /** @var string |
||
| 41 | * @deprecated |
||
| 42 | */ |
||
| 43 | protected $classModel; |
||
| 44 | |||
| 45 | /** @var string |
||
| 46 | * @deprecated |
||
| 47 | */ |
||
| 48 | protected $tableName; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $tableAlias; |
||
| 52 | |||
| 53 | /** @var array |
||
| 54 | */ |
||
| 55 | private array $resultArray = []; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $logSQL; |
||
| 59 | |||
| 60 | /** @var PDOException */ |
||
| 61 | private $error; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | private string $query = ""; |
||
| 65 | |||
| 66 | /** @var array */ |
||
| 67 | private array $params = []; |
||
| 68 | |||
| 69 | |||
| 70 | /** @return PDO|false */ |
||
| 71 | private function getConnect() |
||
| 72 | { |
||
| 73 | try { |
||
| 74 | if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->getDatabase(), ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
| 75 | $database = $this->getDatabase().ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
| 76 | $this->setDatabase($database); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (empty($this->instance)) { |
||
|
|
|||
| 80 | $this->instance = new PDO( |
||
| 81 | CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->getDatabase() . ';port=' . CONFIG_DATA_LAYER['port'], |
||
| 82 | CONFIG_DATA_LAYER['username'], |
||
| 83 | CONFIG_DATA_LAYER['passwd'], |
||
| 84 | CONFIG_DATA_LAYER['options'] |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $this->instance; |
||
| 89 | } catch (PDOException $e) { |
||
| 90 | $this->setError($e); |
||
| 91 | } |
||
| 92 | |||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param ?PDO $pdo |
||
| 97 | * @return Crud |
||
| 98 | * |
||
| 99 | */ |
||
| 100 | protected function setInstance(?PDO $pdo): self |
||
| 104 | } |
||
| 105 | |||
| 106 | protected function getInstance(): PDO |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $database |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | protected function setDatabase(string $database): self |
||
| 116 | { |
||
| 117 | if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
| 118 | $database = $database.ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
| 119 | $this->database = $database; |
||
| 120 | } else { |
||
| 121 | $this->database = $database; |
||
| 122 | } |
||
| 123 | |||
| 124 | if (!empty($this->instance)){ |
||
| 125 | $this->executeSQL("USE {$this->getDatabase()}"); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | protected function getDatabase(): string |
||
| 135 | { |
||
| 136 | return $this->database ?? ""; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $fields |
||
| 141 | * @return Crud |
||
| 142 | */ |
||
| 143 | protected function setFields(string $fields): self |
||
| 144 | { |
||
| 145 | $this->fields = $fields; |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | protected function getFields():string |
||
| 153 | { |
||
| 154 | return $this->fields; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $tableName |
||
| 159 | * @param string $tableAlias |
||
| 160 | * @return CrudBuilder|Crud|DatalayerTrait |
||
| 161 | */ |
||
| 162 | protected function setTable(string $tableName, string $tableAlias = ""): self |
||
| 163 | { |
||
| 164 | if (!empty($tableAlias)) |
||
| 165 | $this->tableAlias = $tableAlias; |
||
| 166 | $this->tableName = $tableName; |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | protected function getTable(): string |
||
| 174 | { |
||
| 175 | return $this->tableName ?? ""; |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function getTableAlias(): string |
||
| 179 | { |
||
| 180 | return $this->tableAlias ?? ""; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $classModel |
||
| 185 | * @return Crud |
||
| 186 | */ |
||
| 187 | protected function setClassModel(string $classModel): self |
||
| 188 | { |
||
| 189 | $this->classModel = $classModel; |
||
| 190 | return $this; |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function getClassModel(): string |
||
| 194 | { |
||
| 195 | return $this->classModel; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $classModel |
||
| 200 | * @return Crud |
||
| 201 | */ |
||
| 202 | protected function setPrepare(PDOStatement $prepare): self |
||
| 203 | { |
||
| 204 | $this->prepare = $prepare; |
||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | protected function getPrepare(): PDOStatement |
||
| 209 | { |
||
| 210 | return $this->prepare; |
||
| 211 | } |
||
| 212 | |||
| 213 | protected function getResult(): array |
||
| 214 | { |
||
| 215 | return $this->resultArray; |
||
| 216 | } |
||
| 217 | |||
| 218 | protected function setResult(array $array): self |
||
| 219 | { |
||
| 220 | $this->resultArray = $array; |
||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $query |
||
| 226 | * @param array|null $params |
||
| 227 | * @return PDOStatement|void |
||
| 228 | */ |
||
| 229 | protected function executeSQL(string $query, ?array $params = null) |
||
| 230 | { |
||
| 231 | try { |
||
| 232 | $this->setPrepare($this->getInstance()->prepare($query)); |
||
| 233 | $this->setSQL($query, $params); |
||
| 234 | $this->getPrepare()->execute($params); |
||
| 235 | return $this->getPrepare(); |
||
| 236 | } catch (PDOException $e) { |
||
| 237 | $this->setError($e); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $prepare |
||
| 243 | * @return int|false |
||
| 244 | */ |
||
| 245 | protected function count(PDOStatement $prepare = null): ?int |
||
| 246 | { |
||
| 247 | try { |
||
| 248 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
| 249 | return $prepare->rowCount(); |
||
| 250 | } catch (PDOException $e) { |
||
| 251 | $this->setError($e);} |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param PDOStatement|null $prepare |
||
| 256 | * @return array|null |
||
| 257 | */ |
||
| 258 | protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
||
| 259 | { |
||
| 260 | try { |
||
| 261 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
| 262 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
| 263 | $this->setResult($dados); |
||
| 264 | return $dados; |
||
| 265 | } catch (PDOException $e) { |
||
| 266 | $this->setError($e); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param $prepare |
||
| 272 | * @return array|false |
||
| 273 | */ |
||
| 274 | protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param $prepare |
||
| 288 | * @param String|null $classModel |
||
| 289 | * @return array|false |
||
| 290 | */ |
||
| 291 | protected function fetchArrayClass(PDOStatement $prepare = null, string $classModel = null): ?array |
||
| 292 | { |
||
| 293 | try { |
||
| 294 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
| 295 | $classModel = empty($classModel) ? $this->getClassModel() : $classModel; |
||
| 296 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $classModel); |
||
| 297 | $this->setResult($dados); |
||
| 298 | return $dados; |
||
| 299 | } catch (PDOException $e) { |
||
| 300 | $this->setError($e); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param $prepare |
||
| 306 | * @return array|false |
||
| 307 | */ |
||
| 308 | protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
||
| 309 | { |
||
| 310 | try { |
||
| 311 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
| 312 | return $prepare->fetch(PDO::FETCH_ASSOC); |
||
| 313 | } catch (PDOException $e) { |
||
| 314 | $this->setError($e); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param PDOStatement|null $prepare |
||
| 320 | * @return stdClass|null |
||
| 321 | */ |
||
| 322 | protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
||
| 323 | { |
||
| 324 | try { |
||
| 325 | $prepare = empty($prepare) ? $this->getPrepare() : $prepare; |
||
| 326 | return $prepare->fetch(PDO::FETCH_OBJ); |
||
| 327 | } catch (PDOException $e) { |
||
| 328 | $this->setError($e); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $prepare |
||
| 334 | * @param String|null $class |
||
| 335 | * @return array|false |
||
| 336 | */ |
||
| 337 | protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | protected function beginTrasaction(): ?bool |
||
| 358 | } |
||
| 359 | |||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return bool|null |
||
| 364 | */ |
||
| 365 | protected function commitTransaction(): ?bool |
||
| 366 | { |
||
| 367 | try { |
||
| 368 | $this->getInstance()->commit(); |
||
| 369 | return true; |
||
| 370 | } catch (PDOException $e) { |
||
| 371 | $this->setError($e); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return bool|null |
||
| 377 | * |
||
| 378 | */ |
||
| 379 | protected function rollBackTransaction(): ?bool |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return string|null |
||
| 392 | * */ |
||
| 393 | private function lastId(): ?string |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param $sql_string |
||
| 404 | * @param array|null $params |
||
| 405 | * @return void |
||
| 406 | */ |
||
| 407 | private function setSQL($sql_string, ?array $params = null) |
||
| 408 | { |
||
| 409 | try { |
||
| 410 | if (!empty($params)) { |
||
| 411 | $indexed = $params == array_values($params); |
||
| 412 | foreach ($params as $k => $v) { |
||
| 413 | if (is_object($v)) { |
||
| 414 | if ($v instanceof \DateTime) { |
||
| 415 | $v = $v->format('Y-m-d H:i:s'); |
||
| 416 | } else { |
||
| 417 | continue; |
||
| 418 | } |
||
| 419 | } elseif (is_string($v)) { |
||
| 420 | $v = "'$v'"; |
||
| 421 | } elseif ($v === null) { |
||
| 422 | $v = 'NULL'; |
||
| 423 | } elseif (is_array($v)) { |
||
| 424 | $v = implode(',', $v); |
||
| 425 | } |
||
| 426 | |||
| 427 | if ($indexed) { |
||
| 428 | $sql_string = preg_replace('/\?/', $v, $sql_string, 1); |
||
| 429 | } else { |
||
| 430 | if ($k[0] != ':') { |
||
| 431 | $k = ':' . $k; |
||
| 432 | } //add leading colon if it was left out |
||
| 433 | $sql_string = str_replace($k, $v, $sql_string); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | } |
||
| 437 | $this->logSQL = $sql_string; |
||
| 438 | } catch (PDOException $e) { |
||
| 439 | $this->setError($e); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return string|null |
||
| 445 | */ |
||
| 446 | protected function getSQL(): ?string |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param PDOException $e |
||
| 457 | * @return void |
||
| 458 | */ |
||
| 459 | private function setError(PDOException $e) |
||
| 466 |