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 | 54 | 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 | 3 | public function checkCodiceSSA($codiceSSA) |
|
42 | { |
||
43 | 3 | if (!CodiceSSA::isValidValue($codiceSSA)) { |
|
44 | 3 | $this->addError("<b>".$codiceSSA."</b> - Codice SSA (codiceSSA) non valido. Codici validi: ".CodiceSSA::getCostantsValues()); |
|
45 | 3 | } |
|
46 | 3 | } |
|
47 | |||
48 | /** |
||
49 | * @param $dateStr |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 9 | 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 | 3 | public function checkPIva($pIva) |
|
120 | |||
121 | /** |
||
122 | * @param $cfProprietario |
||
123 | */ |
||
124 | 3 | public function checkCfProprietario($cfProprietario) |
|
135 | |||
136 | /** |
||
137 | * @param $codiceAsl |
||
138 | */ |
||
139 | 3 | public function checkCodiceAsl($codiceAsl) |
|
145 | |||
146 | /** |
||
147 | * @param $campo |
||
148 | * @param $valore |
||
149 | */ |
||
150 | 6 | private function checkDataValida($campo, $valore) |
|
160 | |||
161 | /** |
||
162 | * @param $valore |
||
163 | */ |
||
164 | 3 | public function checkTipoSpesa($valore) |
|
170 | |||
171 | /** |
||
172 | * @param $valore |
||
173 | */ |
||
174 | 3 | private function checkImporto($valore) |
|
180 | |||
181 | /** |
||
182 | * @param $valore |
||
183 | * @param $campo |
||
184 | */ |
||
185 | 3 | private function checkRequiredField($valore, $campo) |
|
191 | |||
192 | /** |
||
193 | * @param $campo |
||
194 | * @param $valore |
||
195 | */ |
||
196 | 3 | private function checkDataEmissione($campo, $valore) |
|
200 | |||
201 | /** |
||
202 | * @param $valore |
||
203 | */ |
||
204 | 3 | public function checkFlagOperazione($valore) |
|
210 | |||
211 | /** |
||
212 | * @param $valore |
||
213 | */ |
||
214 | 3 | private function checkCfCittadino($valore) |
|
220 | |||
221 | /** |
||
222 | * @param $valore |
||
223 | */ |
||
224 | 3 | public function checkDispositivo($valore) |
|
230 | |||
231 | /** |
||
232 | * @param $valore |
||
233 | */ |
||
234 | 3 | public function checkNumDocumento($valore) |
|
240 | |||
241 | /** |
||
242 | * @param $rigaSpesa |
||
243 | */ |
||
244 | private function checkRigaSpesa($rigaSpesa) |
||
275 | |||
276 | /** |
||
277 | * @param $valore |
||
278 | * @param int $maxLen |
||
279 | * @param bool|false $zeroFilled |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | 9 | public function checkNumericField($valore, $maxLen=0, $zeroFilled=false) |
|
302 | } |
||
303 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.