Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DAO 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DAO, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class DAO implements DAOInterface { |
||
17 | |||
18 | /** @var PDO */ |
||
19 | protected $pdo; |
||
20 | |||
21 | /** @var string */ |
||
22 | protected $primaryKey = null; |
||
23 | |||
24 | /** @var string[] */ |
||
25 | protected $selectCollumns = ['*']; |
||
26 | |||
27 | /** @var string[] */ |
||
28 | protected $joins = []; |
||
29 | |||
30 | /** @var Where */ |
||
31 | protected $where; |
||
32 | |||
33 | /** @var Option */ |
||
34 | protected $option; |
||
35 | |||
36 | /** @var Pagination */ |
||
37 | protected $pagination; |
||
38 | |||
39 | /** @var boolean */ |
||
40 | protected static $debug = false; |
||
41 | protected static $instance = []; |
||
42 | |||
43 | /** |
||
44 | * Valida os campos retornando string de Erro ou Null |
||
45 | * @return string|null |
||
46 | */ |
||
47 | abstract protected function validate(); |
||
48 | |||
49 | /** |
||
50 | * Retorna a instancia do DAO |
||
51 | * @return static |
||
52 | */ |
||
53 | View Code Duplication | public static function instance() { |
|
60 | |||
61 | /** Inicia o DAO */ |
||
62 | final public function __construct() { |
||
69 | |||
70 | public function pagination() { |
||
73 | |||
74 | /** |
||
75 | * Define uma conexão manualmente |
||
76 | * @param PDO $pdo |
||
77 | */ |
||
78 | public function setPDO($pdo) { |
||
81 | |||
82 | /** |
||
83 | * Define quais colunas serão consultadas no SELECT |
||
84 | * @param string[] $collumns |
||
85 | */ |
||
86 | public function setSelectCollumns(array $collumns) { |
||
89 | |||
90 | /** |
||
91 | * Adiciona nova coluna no SELECT |
||
92 | * @param string $collumn |
||
93 | */ |
||
94 | public function addSelectCollumn($collumn) { |
||
99 | |||
100 | /** |
||
101 | * Salva o registro |
||
102 | * @param object $obj |
||
103 | * @return string|null |
||
104 | */ |
||
105 | public function save($obj) { |
||
129 | |||
130 | /** Insere o registro */ |
||
131 | protected function insert() { |
||
140 | |||
141 | /** Atualiza o registro */ |
||
142 | protected function update() { |
||
157 | |||
158 | /** |
||
159 | * Exclui o registro |
||
160 | * @param object $obj |
||
161 | * @param mixed[] $filters |
||
162 | */ |
||
163 | public function delete($obj) { |
||
171 | |||
172 | /** |
||
173 | * Exclui o registro por id |
||
174 | * @param int $id |
||
175 | */ |
||
176 | public function deleteById($id) { |
||
179 | |||
180 | /** |
||
181 | * Exclui o registro por id |
||
182 | * @param string $name |
||
183 | * * @param mixed $value |
||
184 | */ |
||
185 | public function deleteByField($name, $value) { |
||
188 | |||
189 | /** |
||
190 | * Exclui todos os registros |
||
191 | * @param mixed[] $filters |
||
192 | */ |
||
193 | public function deleteAll($filters = []) { |
||
204 | |||
205 | /** |
||
206 | * Executa SQL via PDO |
||
207 | * @param string $sql |
||
208 | * @param mixed[] $values |
||
209 | * @return PDOStatement |
||
210 | */ |
||
211 | protected function exec($sql, $values) { |
||
225 | |||
226 | /** |
||
227 | * @param $stmt \PDOStatement |
||
228 | * @param PDOException|null $e |
||
229 | * @return string erro |
||
230 | */ |
||
231 | protected function errorSql(PDOStatement $stmt, PDOException $e = null) { |
||
243 | |||
244 | /** |
||
245 | * Busca o objeto pelo id |
||
246 | * @param int $id |
||
247 | */ |
||
248 | public function fetchById($id) { |
||
251 | |||
252 | /** |
||
253 | * Busca o objeto por um campo/atributo específico |
||
254 | * @param string $name Nome do atributo |
||
255 | * @param mixed $value Valor do atributo |
||
256 | */ |
||
257 | public function fetchByField($name, $value) { |
||
260 | |||
261 | /** |
||
262 | * Busca o objeto |
||
263 | * @param string[] $filters Array de filtros |
||
264 | */ |
||
265 | public function fetch($filters) { |
||
278 | |||
279 | /** |
||
280 | * Retorna todos os registros |
||
281 | * |
||
282 | * <code> |
||
283 | * $dao->fetchAll( ['id = ?' => 10]); |
||
284 | * </code> |
||
285 | * @param string[] $filters Array de filtros |
||
286 | */ |
||
287 | public function fetchAll($filters = []) { |
||
306 | |||
307 | /** |
||
308 | * Retorna comando SELECT |
||
309 | * @param string $selectCollumns |
||
310 | * @return string |
||
311 | * @example "SELECT * FROM user" |
||
312 | */ |
||
313 | protected function selectSQL($selectCollumns = ['*']) { |
||
316 | |||
317 | /** |
||
318 | * Adiciona o array de filtros |
||
319 | * @param mixed[] $filters |
||
320 | */ |
||
321 | private function addFilters($filters = []) { |
||
324 | |||
325 | /** |
||
326 | * Filtra a proxima busca |
||
327 | * @param string $condition |
||
328 | * @param mixed[] $values |
||
329 | */ |
||
330 | public function filter($condition, $values) { |
||
333 | |||
334 | /** |
||
335 | * Adiciona opções de busca |
||
336 | * @param string $option |
||
337 | */ |
||
338 | public function option($option) { |
||
341 | |||
342 | /** |
||
343 | * Define a paginação dos itens |
||
344 | * @param int $totalPerPage |
||
345 | * @param int $currentPage |
||
346 | */ |
||
347 | public function paginate($totalPerPage, $currentPage = null) { |
||
350 | |||
351 | |||
352 | /** |
||
353 | * Retorna o total de registros |
||
354 | * @param string[] $filters Array de filtros |
||
355 | * @return int |
||
356 | */ |
||
357 | public function numRows($filters = []) { |
||
371 | |||
372 | /** |
||
373 | * Retorna True se objeto existir |
||
374 | * @param mixed $obj |
||
375 | * @return boolean |
||
376 | */ |
||
377 | public function objExists($obj) { |
||
380 | |||
381 | /** Define como Página 404 se o objeto não existir */ |
||
382 | public function checkFoundRegistry($obj) { |
||
388 | |||
389 | private function validateArray($filters) { |
||
394 | |||
395 | /** Habilita o modo debug */ |
||
396 | final public static function debug() { |
||
399 | |||
400 | /** |
||
401 | * Exibe comando SQL, se debug está habilitado |
||
402 | * @param string $sql |
||
403 | * @param mixed[] $values |
||
404 | */ |
||
405 | protected function showDebug($sql, $values) { |
||
417 | |||
418 | protected function beforeSave() { |
||
421 | |||
422 | protected function afterSave() { |
||
425 | |||
426 | protected function onDelete() { |
||
429 | |||
430 | } |
||
431 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.