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 |
||
11 | abstract class DAO implements DAOInterface { |
||
12 | |||
13 | /** @var \PDO */ |
||
14 | protected $pdo; |
||
15 | |||
16 | /** @var string[] */ |
||
17 | protected $selectCollumns = ['*']; |
||
18 | protected $joins = []; |
||
19 | protected $fixedFilters = []; |
||
20 | |||
21 | /** @var string */ |
||
22 | protected $primaryKey = null; |
||
23 | |||
24 | /** @var int */ |
||
25 | protected $totalPerPage = null; |
||
26 | |||
27 | /** @var boolean */ |
||
28 | public static $debug = false; |
||
29 | |||
30 | /** |
||
31 | * Valida os campos retornando string de Erro ou Null |
||
32 | * @return string|null |
||
33 | */ |
||
34 | abstract protected function validate(); |
||
35 | |||
36 | /** Inicia o DAO */ |
||
37 | public function __construct() { |
||
40 | |||
41 | /** |
||
42 | * Define uma conexão manualmente |
||
43 | * @param \PDO $pdo |
||
44 | */ |
||
45 | public function setPDO($pdo) { |
||
48 | |||
49 | /** |
||
50 | * Define quais colunas serão consultadas no SELECT |
||
51 | * @param string[] $collumns |
||
52 | */ |
||
53 | public function setSelectCollumns(array $collumns) { |
||
56 | |||
57 | /** |
||
58 | * Adiciona nova coluna no SELECT |
||
59 | * @param string $collumn |
||
60 | */ |
||
61 | public function addSelectCollumn($collumn) { |
||
66 | |||
67 | /** |
||
68 | * Salva o registro |
||
69 | * @param object $obj |
||
70 | * @return string|null |
||
71 | */ |
||
72 | public function save($obj) { |
||
96 | |||
97 | /** |
||
98 | * Executa SQL via PDO |
||
99 | * @param string $sql |
||
100 | * @param mixed[] $values |
||
101 | * @return \PDOStatement |
||
102 | */ |
||
103 | protected function execSql($sql, $values) { |
||
111 | |||
112 | /** Insere o registro */ |
||
113 | protected function insert() { |
||
122 | |||
123 | /** Atualiza o registro */ |
||
124 | protected function update() { |
||
139 | |||
140 | /** |
||
141 | * @param $stmt \PDOStatement |
||
142 | * @return string erro |
||
143 | */ |
||
144 | protected function error(\PDOStatement $stmt) { |
||
154 | |||
155 | /** |
||
156 | * Exclui o registro |
||
157 | * @param object $obj |
||
158 | * @param mixed[] $filters |
||
159 | */ |
||
160 | public function delete($obj) { |
||
167 | |||
168 | /** |
||
169 | * Exclui o registro por id |
||
170 | * @param int $id |
||
171 | */ |
||
172 | public function deleteById($id) { |
||
175 | |||
176 | /** |
||
177 | * Exclui o registro por id |
||
178 | * @param string $name |
||
179 | * * @param mixed $value |
||
180 | */ |
||
181 | public function deleteByField($name, $value) { |
||
184 | |||
185 | /** |
||
186 | * Exclui todos os registros |
||
187 | * @param mixed[] $filters |
||
188 | */ |
||
189 | public function deleteAll($filters = []) { |
||
200 | |||
201 | /** |
||
202 | * Busca o objeto pelo id |
||
203 | * @param int $id |
||
204 | */ |
||
205 | public function fetchById($id) { |
||
208 | |||
209 | /** |
||
210 | * Busca o objeto por um campo/atributo específico |
||
211 | * @param string $name Nome do atributo |
||
212 | * @param mixed $value Valor do atributo |
||
213 | */ |
||
214 | public function fetchByField($name, $value) { |
||
217 | |||
218 | /** |
||
219 | * Busca o objeto |
||
220 | * @param string[] $filters Array de filtros |
||
221 | * @param string $option [Order by, Group by, etc] |
||
222 | */ |
||
223 | public function fetch($filters, $option = 'ORDER BY 1 DESC') { |
||
235 | |||
236 | /** |
||
237 | * Retorna todos os registros |
||
238 | * |
||
239 | * <code> |
||
240 | * $dao->fetchAll( ['id = ?' => 10]); |
||
241 | * </code> |
||
242 | * @param string[] $filters Array de filtros |
||
243 | * @param string $option [Order by, Group by, etc] |
||
244 | */ |
||
245 | public function fetchAll($filters = [], $option = 'ORDER BY 1 DESC') { |
||
263 | |||
264 | /** |
||
265 | * Retorna comando SELECT |
||
266 | * @param string $selectCollumns |
||
267 | * @return string |
||
268 | * @example "SELECT * FROM user" |
||
269 | */ |
||
270 | protected function selectSQL($selectCollumns = ['*']) { |
||
273 | |||
274 | /** |
||
275 | * Retorna comando WHERE |
||
276 | * @param string[] $filters |
||
277 | * @return string |
||
278 | */ |
||
279 | protected function whereSQL(&$filters) { |
||
283 | |||
284 | /** |
||
285 | * Retorna o total de registros |
||
286 | * @param string[] $filters Array de filtros |
||
287 | * @param string $option |
||
288 | * @return int |
||
289 | */ |
||
290 | public function numRows($filters = [], $option = 'ORDER BY 1 DESC') { |
||
305 | |||
306 | /** |
||
307 | * Retorna True se objeto existir |
||
308 | * @param mixed $obj |
||
309 | * @return boolean |
||
310 | */ |
||
311 | public function objExists($obj) { |
||
314 | |||
315 | /** Define como Página 404 se o objeto não existir */ |
||
316 | public function checkFoundRegistry($obj) { |
||
322 | |||
323 | /** |
||
324 | * Exibe comando SQL, se debug está habilitado |
||
325 | * @param string $sql |
||
326 | * @param mixed[] $values |
||
327 | */ |
||
328 | protected function debug($sql, $values = []) { |
||
340 | |||
341 | private function getFilterValues($filters) { |
||
344 | |||
345 | protected function beforeSave() { |
||
348 | |||
349 | protected function afterSave() { |
||
352 | |||
353 | protected function onDelete() { |
||
356 | |||
357 | /** @return string Retorna o nome da PK */ |
||
358 | private function getPrimaryKey() { |
||
364 | |||
365 | /** |
||
366 | * Define a paginação dos itens |
||
367 | * @param int totalPerPage |
||
368 | */ |
||
369 | public function paginate($totalPerPage) { |
||
372 | |||
373 | /** |
||
374 | * Busca os registros baseado na paginação definida |
||
375 | * @return string |
||
376 | */ |
||
377 | protected function limitSQL() { |
||
384 | |||
385 | /** @return int Paginação atual */ |
||
386 | private function getCurrentPage() { |
||
389 | |||
390 | } |
||
391 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: