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 ValidateHelper 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 ValidateHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class ValidateHelper |
||
| 9 | { |
||
| 10 | use traits\Errorable; |
||
| 11 | |||
| 12 | private $objPartitaIVA = null; |
||
| 13 | private $objCFChecker = null; |
||
| 14 | |||
| 15 | /**+ |
||
| 16 | * ValidateHelper constructor. |
||
| 17 | * |
||
| 18 | * @param \fdisotto\PartitaIVA $objPartitaIVA |
||
| 19 | * @param \CodiceFiscale\Checker $objCFChecker |
||
| 20 | */ |
||
| 21 | 6 | public function __construct(\fdisotto\PartitaIVA $objPartitaIVA, \CodiceFiscale\Checker $objCFChecker) |
|
| 27 | |||
| 28 | /** |
||
| 29 | * @param $codice |
||
| 30 | * @param $codice_nome |
||
| 31 | * @param array $arrCodiciValidi |
||
| 32 | * @param $codice_len |
||
| 33 | */ |
||
| 34 | 3 | public function checkCodice($codice, $codice_nome, array $arrCodiciValidi, $codice_len) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param $codiceRegione |
||
| 51 | */ |
||
| 52 | 3 | public function checkCodiceRegione($codiceRegione) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param $codiceSSA |
||
| 59 | */ |
||
| 60 | public function checkCodiceSSA($codiceSSA) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param $dateStr |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | 3 | public function isoDateValidate($dateStr) |
|
| 78 | |||
| 79 | /**+ |
||
| 80 | * @param $arrVociSpesa |
||
| 81 | */ |
||
| 82 | public function checkArrVociSpesa($arrVociSpesa) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $arrSpesa |
||
| 110 | */ |
||
| 111 | public function checkArrSpesa($arrSpesa) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param $pIva |
||
| 133 | */ |
||
| 134 | public function checkPIva($pIva) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $cfProprietario |
||
| 148 | */ |
||
| 149 | public function checkCfProprietario($cfProprietario) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $codiceAsl |
||
| 163 | */ |
||
| 164 | public function checkCodiceAsl($codiceAsl) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param $campo |
||
| 171 | * @param $valore |
||
| 172 | */ |
||
| 173 | private function checkDataValida($campo, $valore) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param $valore |
||
| 186 | * @param $arrTipiSpesaPermessi |
||
| 187 | */ |
||
| 188 | private function checkTipoSpesa($valore, $arrTipiSpesaPermessi) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $valore |
||
| 197 | */ |
||
| 198 | private function checkImporto($valore) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param $valore |
||
| 207 | * @param $campo |
||
| 208 | */ |
||
| 209 | private function checkRequiredField($valore, $campo) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param $campo |
||
| 218 | * @param $valore |
||
| 219 | */ |
||
| 220 | private function checkDataEmissione($campo, $valore) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param $valore |
||
| 227 | * @param $arrFlagOperazione |
||
| 228 | */ |
||
| 229 | private function checkFlagOperazione($valore, $arrFlagOperazione) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $valore |
||
| 238 | */ |
||
| 239 | private function checkCfCittadino($valore) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param $valore |
||
| 248 | */ |
||
| 249 | private function checkDispositivo($valore) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $valore |
||
| 258 | */ |
||
| 259 | private function checkNumDocumento($valore) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param $rigaSpesa |
||
| 268 | * @param $arrFlagOperazione |
||
| 269 | */ |
||
| 270 | private function checkRigaSPesa($rigaSpesa, $arrFlagOperazione) |
||
| 294 | |||
| 295 | } |
||
| 296 |