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 PhiberPersistence 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 PhiberPersistence, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class PhiberPersistence extends PhiberPersistenceFactory |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Variável da configuração do Phiber |
||
| 26 | * |
||
| 27 | * @var JsonReader |
||
| 28 | */ |
||
| 29 | private $phiberConfig; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Variável da tabela do objeto trabalhado |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $table = ""; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Campos/colunas do objeto trabalhado |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $fields = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Valores/colunas dos campos |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $fieldsValues = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Informações para a criação da SQL. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $infos = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Informações mergidas |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $infosMergeds = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Informações mergidas |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $rowCount = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $sql = ""; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Array que vai ter os joins. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $joins = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Variável que instancia PDO |
||
| 87 | * |
||
| 88 | * @var PDOStatement |
||
| 89 | */ |
||
| 90 | private $PDO = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Restrictions |
||
| 94 | */ |
||
| 95 | public $restrictions; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | private $returnSelectWithArray = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @deprecated use restriction sem o parênteses. |
||
| 104 | * @return Restrictions |
||
| 105 | */ |
||
| 106 | public function restrictions() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param bool $isArray |
||
| 113 | */ |
||
| 114 | public function returnArray(bool $isArray = false) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Seleciona a tabela manualmente para a escrita da SQL |
||
| 121 | * |
||
| 122 | * @param string $table |
||
| 123 | */ |
||
| 124 | public function setTable(string $table) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Seleciona os campos manualmente para a escrita da SQL |
||
| 131 | * |
||
| 132 | * @param array $fields |
||
| 133 | */ |
||
| 134 | public function setFields(array $fields) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Seleciona os valores dos campos manualmente para o binding após a escrita da SQL |
||
| 141 | * |
||
| 142 | * @param array $fieldsValues |
||
| 143 | */ |
||
| 144 | public function setValues(array $fieldsValues) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Faz o rowCount (contagem de linhas) objeto especificado, se caso a opção execute_queries |
||
| 151 | * estiver habilitada |
||
| 152 | * |
||
| 153 | * @return int|mixed |
||
| 154 | * @internal param null $infos |
||
| 155 | * @internal param Object $obj |
||
| 156 | * @internal param array $condicoes |
||
| 157 | * @internal param array $conjuncoes |
||
| 158 | */ |
||
| 159 | public function rowCount() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * PhiberPersistence constructor. |
||
| 166 | * |
||
| 167 | * @param $obj |
||
| 168 | */ |
||
| 169 | public function __construct($obj = "") |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Faz a criação do objeto especificado no banco de dados, caso a opção |
||
| 186 | * execute_queries na configuração esteja habilitada. |
||
| 187 | * |
||
| 188 | * @return bool|mixed |
||
| 189 | */ |
||
| 190 | public function create() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Faz o update no banco do objeto especificado, se caso a opção execute_queries |
||
| 216 | * estiver habilitada |
||
| 217 | * |
||
| 218 | * @return mixed |
||
| 219 | * @internal param array $conditions |
||
| 220 | * @internal param array $conjunctions |
||
| 221 | */ |
||
| 222 | public function update() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Faz o delete no banco do objeto especificado, se caso a opção execute_queries |
||
| 269 | * estiver habilitada |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | * @internal param $ <T> $obj |
||
| 273 | * @internal param null $infos |
||
| 274 | */ |
||
| 275 | public function delete() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Faz a seleção no banco do objeto especificado, se caso a opção execute_queries |
||
| 315 | * estiver habilitada |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | * @internal param null $infos |
||
| 319 | */ |
||
| 320 | public function select() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Adiciona parâmetros da classe restriction nas informações para buildar o SQL. |
||
| 373 | * |
||
| 374 | * @param $infos |
||
| 375 | */ |
||
| 376 | public function add($infos) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Função responsável por mostrar o string da SQL gerada a partir do objeto |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function show() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Função para escrever SQL manualmente |
||
| 403 | * |
||
| 404 | * @param string $sql |
||
| 405 | */ |
||
| 406 | public function writeSQL($sql) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param $parameter |
||
| 414 | * @param $value |
||
| 415 | * @param int $data_type |
||
| 416 | */ |
||
| 417 | public function bindValue($parameter, $value, $data_type = PDO::PARAM_STR) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Responsável por executar a query. |
||
| 424 | * |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | public function execute() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Responsável por retornar todos os dados da tabela. |
||
| 435 | * |
||
| 436 | * @param int $fetch_style |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | public function fetchAll($fetch_style = PDO::FETCH_ASSOC) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Responsável por retornar um range de dados desejados. |
||
| 446 | * |
||
| 447 | * @param null $fetch_style |
||
| 448 | * @param int $cursor_orientation |
||
| 449 | * @param int $cursor_offset |
||
| 450 | * @return mixed |
||
| 451 | */ |
||
| 452 | public function fetch($fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Função utilizada para mergir informações novas com as antigas da Restrictions |
||
| 459 | * |
||
| 460 | * @return void |
||
| 461 | */ |
||
| 462 | private function mergeSqlInformation() |
||
| 475 | } |
||
| 476 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..