Total Complexity | 62 |
Total Lines | 387 |
Duplicated Lines | 0 % |
Changes | 13 | ||
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 | protected $instance = null; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $fields; |
||
26 | |||
27 | /** @var PDOStatement|null */ |
||
28 | protected $prepare = null; |
||
29 | |||
30 | /** @var string */ |
||
31 | protected $database = CONFIG_DATA_LAYER["dbname"]; |
||
32 | |||
33 | /** @var string */ |
||
34 | protected $classModel; |
||
35 | |||
36 | /** @var string */ |
||
37 | protected $tableName; |
||
38 | |||
39 | /** @var string */ |
||
40 | private $tableAlias; |
||
41 | |||
42 | /** @var array */ |
||
43 | protected $resultArray = array(); |
||
44 | |||
45 | /** @var string */ |
||
46 | private $logSQL; |
||
47 | |||
48 | /** @var PDOException */ |
||
49 | private $error; |
||
50 | |||
51 | /** @var string */ |
||
52 | private $query = ""; |
||
53 | |||
54 | /** @var array */ |
||
55 | private $params = []; |
||
56 | |||
57 | private QueryBuilder $queryBuild; |
||
58 | |||
59 | |||
60 | /** @return PDO|false */ |
||
61 | private function getInstance() |
||
80 | } |
||
81 | |||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param PDO $pdo |
||
86 | * @return Crud |
||
87 | * |
||
88 | */ |
||
89 | protected function setInstance(PDO $pdo) |
||
90 | { |
||
91 | $this->instance = $pdo; |
||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $database |
||
97 | * @return $this |
||
98 | */ |
||
99 | protected function setDatabase(string $database) |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function getDatabase(): string |
||
109 | { |
||
110 | return $this->database; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $tableName |
||
115 | * @return Crud |
||
116 | */ |
||
117 | protected function setFields(string $fields) |
||
118 | { |
||
119 | $this->fields = $fields; |
||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | protected function getFields():string |
||
127 | { |
||
128 | return $this->fields; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param string $tableName |
||
133 | * @return Crud |
||
134 | */ |
||
135 | protected function setTable(string $tableName, string $tableAlias = ""): self |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function getTable(): string |
||
149 | } |
||
150 | |||
151 | public function getQueryBuilder(): QueryBuilder |
||
156 | } |
||
157 | |||
158 | public function getTableAlias(): string |
||
159 | { |
||
160 | return $this->tableAlias; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $classModel |
||
165 | * @return Crud |
||
166 | */ |
||
167 | protected function setClassModel(string $classModel): self |
||
168 | { |
||
169 | $this->classModel = $classModel; |
||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | protected function getClassModel() |
||
174 | { |
||
175 | return $this->classModel; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param String $query |
||
180 | * @param array|null $params |
||
181 | * @return false|mixed|\PDOStatement|null |
||
182 | */ |
||
183 | protected function executeSQL(string $query, ?array $params = null) |
||
184 | { |
||
185 | try { |
||
186 | $this->prepare = $this->getInstance()->prepare($query); |
||
187 | $this->setLogSQL($query, $params); |
||
188 | $this->prepare->execute($params); |
||
189 | return $this->prepare; |
||
190 | } catch (PDOException $e) { |
||
191 | $this->setError($e); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param $prepare |
||
197 | * @return int|false |
||
198 | */ |
||
199 | protected function count(PDOStatement $prepare = null): ?int |
||
200 | { |
||
201 | try { |
||
202 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
203 | return $prepare->rowCount(); |
||
|
|||
204 | } catch (PDOException $e) { |
||
205 | $this->setError($e);} |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param $prepare |
||
210 | * @return array|false |
||
211 | */ |
||
212 | protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array |
||
213 | { |
||
214 | try { |
||
215 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
216 | $dados = $prepare->fetchAll(PDO::FETCH_ASSOC); |
||
217 | $this->resultArray = $dados; |
||
218 | return $dados; |
||
219 | } catch (PDOException $e) { |
||
220 | $this->setError($e); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param $prepare |
||
226 | * @return array|false |
||
227 | */ |
||
228 | protected function fetchArrayObj(PDOStatement $prepare = null): ?array |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param $prepare |
||
242 | * @param String|null $class |
||
243 | * @return array|false |
||
244 | */ |
||
245 | protected function fetchArrayClass(PDOStatement $prepare = null, string $class = null): ?array |
||
246 | { |
||
247 | try { |
||
248 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
249 | $class = empty($class) ? $this->classModel : $class; |
||
250 | $dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $class); |
||
251 | $this->resultArray = $dados; |
||
252 | return $dados; |
||
253 | } catch (PDOException $e) { |
||
254 | $this->setError($e); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param $prepare |
||
260 | * @return array|false |
||
261 | */ |
||
262 | protected function fetchOneAssoc(PDOStatement $prepare = null): ?array |
||
263 | { |
||
264 | try { |
||
265 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
266 | $dados = $prepare->fetch(PDO::FETCH_ASSOC); |
||
267 | return $dados; |
||
268 | } catch (PDOException $e) { |
||
269 | $this->setError($e); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param $prepare |
||
275 | * @return stdClass|false |
||
276 | */ |
||
277 | protected function fetchOneObj(PDOStatement $prepare = null): ?stdClass |
||
278 | { |
||
279 | try { |
||
280 | $prepare = empty($prepare) ? $this->prepare : $prepare; |
||
281 | $dados = $prepare->fetch(PDO::FETCH_OBJ); |
||
282 | return $dados; |
||
283 | } catch (PDOException $e) { |
||
284 | $this->setError($e); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param $prepare |
||
290 | * @param String|null $class |
||
291 | * @return array|false |
||
292 | */ |
||
293 | protected function fetchOneClass(PDOStatement $prepare = null, string $class = null): ?object |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @return bool |
||
307 | */ |
||
308 | protected function beginTrasaction(): ?bool |
||
315 | } |
||
316 | |||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @return bool|null |
||
321 | */ |
||
322 | protected function commitTransaction(): ?bool |
||
323 | { |
||
324 | try { |
||
325 | $this->getInstance()->commit(); |
||
326 | return true; |
||
327 | } catch (PDOException $e) { |
||
328 | $this->setError($e); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @return bool|null |
||
334 | * |
||
335 | */ |
||
336 | protected function rollBackTransaction(): ?bool |
||
344 | } |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return string|null |
||
349 | * */ |
||
350 | private function lastId(): ?string |
||
351 | { |
||
352 | try { |
||
353 | $ultimo = $this->getInstance()->lastInsertId(); |
||
354 | return $ultimo; |
||
355 | } catch (PDOException $e) { |
||
356 | $this->setError($e); |
||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param $sql_string |
||
362 | * @param array|null $params |
||
363 | * @return void |
||
364 | */ |
||
365 | private function setLogSQL($sql_string, ?array $params = null) |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param PDOException $e |
||
399 | * @param string $sql |
||
400 | * @return void |
||
401 | */ |
||
402 | private function setError(PDOException $e) |
||
406 | |||
407 | } |
||
409 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.