| Total Complexity | 56 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 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 */ |
||
| 22 | protected $instance; |
||
| 23 | protected $params; |
||
| 24 | protected $prepare = null; |
||
| 25 | protected $database = CONFIG_DATA_LAYER["dbname"]; |
||
| 26 | protected $classModel; |
||
| 27 | protected $tableName; |
||
| 28 | protected $resultArray = array(); |
||
| 29 | |||
| 30 | private $logSQL; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return PDO|null |
||
| 34 | */ |
||
| 35 | private function getInstance(): ?PDO |
||
| 36 | {
|
||
| 37 | if (strpos($_SERVER['SERVER_NAME'], "homologacao") && !strpos($this->database, "Homologacao") ) |
||
| 38 | $this->database .= "Homologacao"; |
||
| 39 | |||
| 40 | if (!isset($this->instance)) {
|
||
| 41 | $this->instance = Connect::getInstance($this->database); |
||
| 42 | return $this->instance; |
||
| 43 | } else {
|
||
| 44 | return $this->instance; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param String $query |
||
| 50 | * @param array|null $params |
||
| 51 | * @return false|mixed|\PDOStatement|null |
||
| 52 | */ |
||
| 53 | protected function executeSQL(String $query, ?array $params = null) |
||
| 54 | {
|
||
| 55 | try {
|
||
| 56 | $this->getInstance(); |
||
| 57 | $this->prepare = $this->instance->prepare($query); |
||
| 58 | $this->prepare->execute($params); |
||
| 59 | $this->setLogSQL($query, $params); |
||
| 60 | } catch (PDOException $e) {
|
||
| 61 | Connect::setError($e,$query); |
||
| 62 | return false; |
||
| 63 | } |
||
| 64 | |||
| 65 | return $this->prepare; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param $prepare |
||
| 70 | * @return int |
||
| 71 | */ |
||
| 72 | protected function count($prepare=null): int |
||
| 73 | {
|
||
| 74 | try {
|
||
| 75 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 76 | $qtd = $prepare->rowCount(); |
||
| 77 | return $qtd; |
||
| 78 | } catch (PDOException $e) {
|
||
| 79 | Connect::setError($e); |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $prepare |
||
| 88 | * @return array|false |
||
| 89 | */ |
||
| 90 | protected function fetchArrayAssoc($prepare=null): array |
||
| 91 | {
|
||
| 92 | try {
|
||
| 93 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 94 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
| 95 | $this->resultArray = $dados; |
||
| 96 | return $dados; |
||
| 97 | } catch (PDOException $e) {
|
||
| 98 | Connect::setError($e); |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param $prepare |
||
| 105 | * @return array|false |
||
| 106 | */ |
||
| 107 | protected function fetchArrayObj($prepare=null): array |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $prepare |
||
| 122 | * @param String|null $class |
||
| 123 | * @return array|false |
||
| 124 | */ |
||
| 125 | protected function fetchArrayClass($prepare=null, String $class=null): array |
||
| 126 | {
|
||
| 127 | try {
|
||
| 128 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 129 | $class = empty($class) ? $this->classModel : $class; |
||
| 130 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class); |
||
| 131 | $this->resultArray = $dados; |
||
| 132 | return $dados; |
||
| 133 | } catch (PDOException $e) {
|
||
| 134 | Connect::setError($e); |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param $prepare |
||
| 141 | * @return array|false |
||
| 142 | */ |
||
| 143 | protected function fetchOneAssoc($prepare=null): array |
||
| 144 | {
|
||
| 145 | try {
|
||
| 146 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 147 | $dados = $prepare->fetch(PDO::FETCH_ASSOC); |
||
| 148 | return $dados; |
||
| 149 | } catch (PDOException $e) {
|
||
| 150 | Connect::setError($e); |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param $prepare |
||
| 157 | * @return stdClass|false |
||
| 158 | */ |
||
| 159 | protected function fetchOneObj($prepare=null): stdClass |
||
| 160 | {
|
||
| 161 | try {
|
||
| 162 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 163 | $dados = $prepare->fetch(PDO::FETCH_OBJ); |
||
| 164 | return $dados; |
||
| 165 | } catch (PDOException $e) {
|
||
| 166 | Connect::setError($e); |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $prepare |
||
| 173 | * @param String|null $class |
||
| 174 | * @return array|false |
||
| 175 | */ |
||
| 176 | protected function fetchOneClass($prepare=null, String $class=null): object |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | protected function beginTrasaction(): bool |
||
| 201 | } |
||
| 202 | |||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | protected function commitTransaction(): bool |
||
| 209 | {
|
||
| 210 | try {
|
||
| 211 | $this->getInstance(); |
||
| 212 | $this->instance->commit(); |
||
| 213 | return true; |
||
| 214 | } catch (PDOException $e) {
|
||
| 215 | Connect::setError($e); |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | protected function rollBackTransaction(): bool |
||
| 224 | {
|
||
| 225 | |||
| 226 | try {
|
||
| 227 | $this->getInstance(); |
||
| 228 | $this->instance->rollBack(); |
||
| 229 | return true; |
||
| 230 | } catch (PDOException $e) {
|
||
| 231 | Connect::setError($e); |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $sql |
||
| 238 | * @param $params |
||
| 239 | * @param $class |
||
| 240 | * @return array|false |
||
| 241 | */ |
||
| 242 | protected function selectDB($sql, $params = null, $class = null) |
||
| 243 | {
|
||
| 244 | try {
|
||
| 245 | $this->getInstance(); |
||
| 246 | $this->prepare = $this->instance->prepare($sql); |
||
| 247 | $this->prepare->execute($params); |
||
| 248 | $this->setLogSQL($sql, $params); |
||
| 249 | |||
| 250 | if (!empty($class)) {
|
||
| 251 | $rs = $this->fetchArrayClass($this->prepare,$class); |
||
| 252 | } else {
|
||
| 253 | $rs = $this->fetchArrayObj($this->prepare); |
||
| 254 | } |
||
| 255 | } catch (PDOException $e) {
|
||
| 256 | Connect::setError($e,$sql); |
||
| 257 | return false; |
||
| 258 | } |
||
| 259 | return $rs; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param $sql |
||
| 264 | * @param $params |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | protected function insertDB($sql, $params = null): bool |
||
| 268 | {
|
||
| 269 | try {
|
||
| 270 | $this->getInstance(); |
||
| 271 | $this->prepare = $this->instance->prepare($sql); |
||
| 272 | $rs = $this->prepare->execute($params); |
||
| 273 | $this->logSQL = $this->prepare->queryString; |
||
| 274 | } catch (PDOException $e) {
|
||
| 275 | Connect::setError($e,$sql); |
||
| 276 | return false; |
||
| 277 | } |
||
| 278 | return $rs; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param $sql |
||
| 283 | * @param $params |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | protected function updateDB($sql, $params = null): bool |
||
| 287 | {
|
||
| 288 | try {
|
||
| 289 | $this->getInstance(); |
||
| 290 | $query = $this->instance->prepare($sql); |
||
| 291 | $rs = $query->execute($params); |
||
| 292 | $this->setLogSQL($sql, $params); |
||
| 293 | } catch (PDOException $e) {
|
||
| 294 | Connect::setError($e,$sql); |
||
| 295 | return false; |
||
| 296 | } |
||
| 297 | return $rs; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param $sql |
||
| 302 | * @param $params |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | protected function deleteDB($sql, $params = null): bool |
||
| 306 | {
|
||
| 307 | try {
|
||
| 308 | $this->getInstance();; |
||
| 309 | $this->prepare = $this->instance->prepare($sql); |
||
| 310 | $rs = $this->prepare->execute($params); |
||
| 311 | $this->setLogSQL($sql, $params); |
||
| 312 | } catch (PDOException $e) {
|
||
| 313 | Connect::setError($e,$sql); |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | return $rs; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * RETORNAR O ULTIMO ID INSERIDO |
||
| 321 | */ |
||
| 322 | private function lastId() |
||
| 323 | {
|
||
| 324 | $this->getInstance(); |
||
| 325 | $ultimo = $this->instance->lastInsertId(); |
||
| 326 | return $ultimo; |
||
| 327 | |||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | protected function printErrorInfo(): array |
||
| 336 | } |
||
| 337 | |||
| 338 | function setLogSQL($sql_string, array $params = null) {
|
||
| 339 | if (!empty($params)) {
|
||
| 340 | $indexed = $params == array_values($params); |
||
| 341 | foreach($params as $k=>$v) {
|
||
| 342 | if (is_object($v)) {
|
||
| 343 | if ($v instanceof \DateTime) $v = $v->format('Y-m-d H:i:s');
|
||
| 344 | else continue; |
||
| 345 | } |
||
| 346 | elseif (is_string($v)) $v="'$v'"; |
||
| 347 | elseif ($v === null) $v='NULL'; |
||
| 362 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.