| Total Complexity | 50 |
| Total Lines | 284 |
| 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 |
||
| 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) |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * RETORNAR O ULTIMO ID INSERIDO |
||
| 264 | */ |
||
| 265 | private function lastId() |
||
| 266 | {
|
||
| 267 | $this->getInstance(); |
||
| 268 | $ultimo = $this->instance->lastInsertId(); |
||
| 269 | return $ultimo; |
||
| 270 | |||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | protected function printErrorInfo(): array |
||
| 279 | } |
||
| 280 | |||
| 281 | function setLogSQL($sql_string, array $params = null) {
|
||
| 303 | } |
||
| 304 | } |
||
| 305 |
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.