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 $codiceRegione |
||
| 30 | */ |
||
| 31 | 3 | public function checkCodiceRegione($codiceRegione) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @param $codiceSSA |
||
| 40 | */ |
||
| 41 | public function checkCodiceSSA($codiceSSA) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param $dateStr |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | 3 | public function isoDateValidate($dateStr) |
|
| 61 | |||
| 62 | /**+ |
||
| 63 | * @param $arrVociSpesa |
||
| 64 | */ |
||
| 65 | public function checkArrVociSpesa($arrVociSpesa) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param $arrSpesa |
||
| 91 | */ |
||
| 92 | public function checkArrSpesa($arrSpesa) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param $pIva |
||
| 108 | */ |
||
| 109 | public function checkPIva($pIva) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param $cfProprietario |
||
| 123 | */ |
||
| 124 | public function checkCfProprietario($cfProprietario) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param $codiceAsl |
||
| 138 | */ |
||
| 139 | public function checkCodiceAsl($codiceAsl) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param $campo |
||
| 148 | * @param $valore |
||
| 149 | */ |
||
| 150 | private function checkDataValida($campo, $valore) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $valore |
||
| 163 | */ |
||
| 164 | private function checkTipoSpesa($valore) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $valore |
||
| 173 | */ |
||
| 174 | private function checkImporto($valore) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $valore |
||
| 183 | * @param $campo |
||
| 184 | */ |
||
| 185 | private function checkRequiredField($valore, $campo) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param $campo |
||
| 194 | * @param $valore |
||
| 195 | */ |
||
| 196 | private function checkDataEmissione($campo, $valore) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param $valore |
||
| 203 | */ |
||
| 204 | private function checkFlagOperazione($valore) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param $valore |
||
| 213 | */ |
||
| 214 | private function checkCfCittadino($valore) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param $valore |
||
| 223 | */ |
||
| 224 | private function checkDispositivo($valore) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param $valore |
||
| 233 | */ |
||
| 234 | private function checkNumDocumento($valore) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $rigaSpesa |
||
| 243 | */ |
||
| 244 | private function checkRigaSpesa($rigaSpesa) |
||
| 272 | } |
||
| 273 |