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 |
||
| 11 | abstract class DAO implements DAOInterface { |
||
| 12 | |||
| 13 | /** @var \PDO */ |
||
| 14 | protected $pdo; |
||
| 15 | |||
| 16 | const JOIN = ''; |
||
| 17 | |||
| 18 | protected $fixedFilter = []; |
||
| 19 | |||
| 20 | /** @var string[] */ |
||
| 21 | protected $selectCollumns = ['*']; |
||
| 22 | |||
| 23 | /** @var boolean */ |
||
| 24 | public static $debug = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Retorna um objeto a partir da linha da tabela |
||
| 28 | * @param array[] $row |
||
| 29 | */ |
||
| 30 | abstract protected function mapObject($row); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Retorna a linha da tabela a partir de um objeto |
||
| 34 | * @param object $obj |
||
| 35 | */ |
||
| 36 | abstract protected function mapRow($obj); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Valida os campos retornando string de Erro ou Null |
||
| 40 | * @return string|null |
||
| 41 | */ |
||
| 42 | abstract protected function validate(); |
||
| 43 | |||
| 44 | /** Inicia o DAO */ |
||
| 45 | public function __construct() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Define uma conexão manualmente |
||
| 51 | * @param \PDO $pdo |
||
| 52 | */ |
||
| 53 | public function setPDO($pdo) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Define quais colunas serão consultadas no SELECT |
||
| 59 | * @param string[] $collumns |
||
| 60 | */ |
||
| 61 | public function setSelectCollumns(array $collumns) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Adiciona nova coluna no SELECT |
||
| 67 | * @param string $collumn |
||
| 68 | */ |
||
| 69 | public function addSelectCollumn($collumn) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Salva o registro |
||
| 77 | * @param object $obj |
||
| 78 | * @return string|null |
||
| 79 | */ |
||
| 80 | public function save($obj) { |
||
| 97 | |||
| 98 | /** Insere o registro */ |
||
| 99 | protected function insert() { |
||
| 113 | |||
| 114 | /** Atualiza o registro */ |
||
| 115 | protected function update() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param $stmt \PDOStatement |
||
| 136 | * @return string erro |
||
| 137 | */ |
||
| 138 | protected function error(\PDOStatement $stmt) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Exclui o registro |
||
| 151 | * @param object $obj |
||
| 152 | */ |
||
| 153 | public function delete($obj) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Exclui o registro por id |
||
| 159 | * @param int $id |
||
| 160 | */ |
||
| 161 | public function deleteById($id) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Exclui o registro por id |
||
| 167 | * @param string $name |
||
| 168 | * * @param mixed $value |
||
| 169 | */ |
||
| 170 | public function deleteByField($name, $value) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Busca o objeto pelo id |
||
| 182 | * @param int $id |
||
| 183 | */ |
||
| 184 | public function fetchById($id) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Busca o objeto por um campo/atributo específico |
||
| 190 | * @param string $name Nome do atributo |
||
| 191 | * @param mixed $value Valor do atributo |
||
| 192 | */ |
||
| 193 | public function fetchByField($name, $value) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Busca o objeto |
||
| 199 | * @param string[] $filters Array de filtros |
||
| 200 | * @param string $option [Order by, Limit, etc] |
||
| 201 | */ |
||
| 202 | public function fetch($filters, $option = 'ORDER BY 1 DESC') { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Retorna todos os registros |
||
| 220 | * |
||
| 221 | * <code> |
||
| 222 | * $dao->fetchAll( ['id = ?' => 10]); |
||
| 223 | * </code> |
||
| 224 | * @param string[] $filters Array de filtros |
||
| 225 | * @param string $option [Order by, Limit, etc] |
||
| 226 | */ |
||
| 227 | public function fetchAll($filters = [], $option = 'ORDER BY 1 DESC') { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Retorna comando SELECT |
||
| 249 | * @param string $selectCollumns |
||
| 250 | * @return string |
||
| 251 | * @example "SELECT * FROM user" |
||
| 252 | */ |
||
| 253 | protected function selectSQL($selectCollumns = ['*']) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Retorna comando WHERE |
||
| 259 | * @param string[] $filters |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function whereSQL(&$filters) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Retorna o total de registros |
||
| 269 | * @param string[] $filters Array de filtros |
||
| 270 | * @param string $option |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | public function numRows($filters = [], $option = 'ORDER BY 1 DESC') { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Retorna True se objeto existir |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | protected function objExists($obj) { |
||
| 299 | |||
| 300 | /** Define como Página 404 se o objeto não existir */ |
||
| 301 | public function checkFoundRegistry($obj) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Exibe comando SQL, se debug está habilitado |
||
| 310 | * @param string $sql |
||
| 311 | * @param mixed[] $values |
||
| 312 | */ |
||
| 313 | protected function debug($sql, $values = []) { |
||
| 320 | |||
| 321 | private function getFilterValues($filters) { |
||
| 324 | |||
| 325 | protected function beforeSave() { |
||
| 328 | |||
| 329 | protected function afterSave() { |
||
| 332 | |||
| 333 | } |
||
| 334 |
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: