| Total Complexity | 49 |
| Total Lines | 299 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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 |
||
| 20 | trait DatalayerTrait |
||
| 21 | { |
||
| 22 | /** @var PDO|null */ |
||
| 23 | protected $instance = null; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | protected $fields; |
||
| 27 | |||
| 28 | /** @var PDOStatement|false */ |
||
| 29 | protected $prepare = null; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $database = CONFIG_DATA_LAYER["dbname"]; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $classModel; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | protected $tableName; |
||
| 39 | |||
| 40 | /** @var array */ |
||
| 41 | protected $resultArray = array(); |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $logSQL; |
||
| 45 | |||
| 46 | /** @var PDOException */ |
||
| 47 | private $error; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return PDO|false |
||
| 51 | */ |
||
| 52 | private function getInstance() |
||
| 53 | { |
||
| 54 | try { |
||
| 55 | if (strpos($_SERVER['SERVER_NAME'], mb_strtolower(CONFIG_DATA_LAYER["homologation"])) && !strpos($this->database, ucfirst(CONFIG_DATA_LAYER["homologation"]))) { |
||
| 56 | $this->database .= ucfirst(CONFIG_DATA_LAYER["homologation"] ?? ""); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (empty($this->instance)) { |
||
| 60 | $this->instance = new PDO( |
||
| 61 | CONFIG_DATA_LAYER['driver'] . ':host=' . CONFIG_DATA_LAYER['host'] . ';dbname=' . $this->database . ';port=' . CONFIG_DATA_LAYER['port'], |
||
| 62 | CONFIG_DATA_LAYER['username'], |
||
| 63 | CONFIG_DATA_LAYER['passwd'], |
||
| 64 | CONFIG_DATA_LAYER['options'] |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | return $this->instance; |
||
| 69 | } catch (PDOException $e) { |
||
| 70 | $this->setError($e); |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param String $query |
||
| 78 | * @param array|null $params |
||
| 79 | * @return false|mixed|\PDOStatement|null |
||
| 80 | */ |
||
| 81 | protected function executeSQL(string $query, ?array $params = null) |
||
| 82 | { |
||
| 83 | try { |
||
| 84 | $this->prepare = $this->getInstance()->prepare($query); |
||
| 85 | $this->setLogSQL($query, $params); |
||
| 86 | $this->prepare->execute($params); |
||
| 87 | return $this->prepare; |
||
| 88 | } catch (PDOException $e) { |
||
| 89 | $this->setError($e, $query); |
||
|
|
|||
| 90 | return false; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param $prepare |
||
| 96 | * @return int|false |
||
| 97 | */ |
||
| 98 | protected function count($prepare = null): ?int |
||
| 99 | { |
||
| 100 | try { |
||
| 101 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 102 | $qtd = $prepare->rowCount(); |
||
| 103 | return $qtd; |
||
| 104 | } catch (PDOException $e) { |
||
| 105 | $this->setError($e); |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $prepare |
||
| 114 | * @return array|false |
||
| 115 | */ |
||
| 116 | protected function fetchArrayAssoc($prepare = null): ?array |
||
| 117 | { |
||
| 118 | try { |
||
| 119 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 120 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
| 121 | $this->resultArray = $dados; |
||
| 122 | return $dados; |
||
| 123 | } catch (PDOException $e) { |
||
| 124 | $this->setError($e); |
||
| 125 | return null; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $prepare |
||
| 131 | * @return array|false |
||
| 132 | */ |
||
| 133 | protected function fetchArrayObj($prepare = null): ?array |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $prepare |
||
| 148 | * @param String|null $class |
||
| 149 | * @return array|false |
||
| 150 | */ |
||
| 151 | protected function fetchArrayClass($prepare = null, string $class = null): ?array |
||
| 152 | { |
||
| 153 | try { |
||
| 154 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 155 | $class = empty($class) ? $this->classModel : $class; |
||
| 156 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class); |
||
| 157 | $this->resultArray = $dados; |
||
| 158 | return $dados; |
||
| 159 | } catch (PDOException $e) { |
||
| 160 | $this->setError($e); |
||
| 161 | return null; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $prepare |
||
| 167 | * @return array|false |
||
| 168 | */ |
||
| 169 | protected function fetchOneAssoc($prepare = null): ?array |
||
| 170 | { |
||
| 171 | try { |
||
| 172 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 173 | $dados = $prepare->fetch(PDO::FETCH_ASSOC); |
||
| 174 | return $dados; |
||
| 175 | } catch (PDOException $e) { |
||
| 176 | $this->setError($e); |
||
| 177 | return null; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $prepare |
||
| 183 | * @return stdClass|false |
||
| 184 | */ |
||
| 185 | protected function fetchOneObj($prepare = null): ?stdClass |
||
| 186 | { |
||
| 187 | try { |
||
| 188 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
| 189 | $dados = $prepare->fetch(PDO::FETCH_OBJ); |
||
| 190 | return $dados; |
||
| 191 | } catch (PDOException $e) { |
||
| 192 | $this->setError($e); |
||
| 193 | return null; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param $prepare |
||
| 199 | * @param String|null $class |
||
| 200 | * @return array|false |
||
| 201 | */ |
||
| 202 | protected function fetchOneClass($prepare = null, string $class = null): ?object |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | protected function beginTrasaction(): ?bool |
||
| 226 | } |
||
| 227 | |||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | protected function commitTransaction(): ?bool |
||
| 234 | { |
||
| 235 | try { |
||
| 236 | $this->getInstance()->commit(); |
||
| 237 | return true; |
||
| 238 | } catch (PDOException $e) { |
||
| 239 | $this->setError($e); |
||
| 240 | return null; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | protected function rollBackTransaction(): ?bool |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string|null |
||
| 261 | */ |
||
| 262 | private function lastId(): ?string |
||
| 263 | { |
||
| 264 | try { |
||
| 265 | $ultimo = $this->getInstance()->lastInsertId(); |
||
| 266 | return $ultimo; |
||
| 267 | } catch (PDOException $e) { |
||
| 268 | $this->setError($e); |
||
| 269 | return null; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $sql_string |
||
| 275 | * @param array|null $params |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | private function setLogSQL($sql_string, array $params = null) |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param PDOException $e |
||
| 312 | * @param string $sql |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | private function setError(PDOException $e) |
||
| 319 | |||
| 320 | } |
||
| 322 |
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.